Skip to content

Commit

Permalink
Add endpoints to batch apply templates by collections
Browse files Browse the repository at this point in the history
  • Loading branch information
mah0001 committed May 28, 2024
1 parent 3b8d10e commit 39e5560
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 1 deletion.
79 changes: 79 additions & 0 deletions application/controllers/api/Collections.php
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down Expand Up @@ -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);
}
}



}
12 changes: 11 additions & 1 deletion application/models/Collection_model.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
1 change: 1 addition & 0 deletions application/models/Editor_model.php
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,7 @@ function set_project_template($sid,$template_uid)

$this->db->where('id',$sid);
$this->db->update('editor_projects',$options);
return true;
}


Expand Down

0 comments on commit 39e5560

Please sign in to comment.