diff --git a/application/controllers/api/Collections.php b/application/controllers/api/Collections.php index 51bdae0d..d911d786 100644 --- a/application/controllers/api/Collections.php +++ b/application/controllers/api/Collections.php @@ -9,6 +9,7 @@ public function __construct() parent::__construct(); $this->load->helper("date"); $this->load->model("Collection_model"); + $this->load->model("Editor_model"); $this->load->model("Collection_access_model"); $this->load->library("Form_validation"); @@ -487,6 +488,84 @@ function tree_list_get($parent_id=null) } } + + /** + * + * + * Set project template by collection and project type + * + * post @options { + * collection_id: int, + * project_type: string, //project types: survey, timeseries, geospatial, document, table + * template_uid: string + * } + * + */ + function template_post() + { + try{ + $this->has_dataset_access('edit'); + + $options=$this->raw_json_input(); + + $required_fields=array('collection_id','project_type','template_uid'); + + foreach($required_fields as $field){ + if (!isset($options[$field])){ + throw new Exception("Missing parameter: $field"); + } + } + + $collection_id=$options['collection_id']; + $project_type=$options['project_type']; + $template_uid=$options['template_uid']; + + + $user=$this->api_user(); + $user_id=$this->get_api_user_id(); + + $this->has_access($resource_='collection',$privilege='edit'); + + //get all projects in collection + $projects=$this->Collection_model->get_projects($collection_id,$project_type); + + $result=array(); + + foreach($projects as $project) + { + if (!isset($project['sid'])){ + $result['skipped'][]=array( + 'sid'=>$project['sid'], + 'type'=>$project['type'] + ); + continue; + } + + $sid=$project['sid']; + $this->Editor_model->set_project_template($sid,$template_uid); + + $result['updated'][]=array( + 'sid'=>$sid, + 'type'=>$project['type'] + ); + } + + $response=array( + 'status'=>'success', + 'result'=>$result + ); + + $this->set_response($response, REST_Controller::HTTP_OK); + } + catch(Exception $e){ + $error_output=array( + 'status'=>'failed', + 'message'=>$e->getMessage() + ); + $this->set_response($error_output, REST_Controller::HTTP_BAD_REQUEST); + } + } + } diff --git a/application/models/Collection_model.php b/application/models/Collection_model.php index 4def9204..82433c38 100644 --- a/application/models/Collection_model.php +++ b/application/models/Collection_model.php @@ -229,8 +229,18 @@ function remove_projects($collection_id,$sids) * Get collection projects * */ - function get_projects($collection_id) + function get_projects($collection_id, $project_type=null) { + + if ($project_type) + { + $this->db->select('editor_projects.type,editor_collection_projects.sid'); + $this->db->join('editor_projects','editor_projects.id=editor_collection_projects.sid'); + $this->db->where('editor_collection_projects.collection_id',$collection_id); + $this->db->where('editor_projects.type',$project_type); + return $this->db->get('editor_collection_projects')->result_array(); + } + $this->db->select('sid'); $this->db->where('collection_id',$collection_id); return $this->db->get('editor_collection_projects')->result_array(); diff --git a/application/models/Editor_model.php b/application/models/Editor_model.php index 545cd200..455d48b8 100644 --- a/application/models/Editor_model.php +++ b/application/models/Editor_model.php @@ -436,6 +436,7 @@ function set_project_template($sid,$template_uid) $this->db->where('id',$sid); $this->db->update('editor_projects',$options); + return true; }