Skip to content

Commit

Permalink
Update projects create endpoint to fix issue with overwrite option + …
Browse files Browse the repository at this point in the history
…allow setting collections
  • Loading branch information
mah0001 committed Jun 5, 2024
1 parent e37fde6 commit adb61af
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
47 changes: 45 additions & 2 deletions application/controllers/api/Editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,31 @@ function check_idno_get($idno=null)
}


private function get_collection_options(&$options)
{
$collections=array();
if (isset($options['collection_ids'])){
//$collections['id_list']=$options['collection_ids'];
$id_list=$options['collection_ids'];
unset($options['collection_ids']);
return $id_list;
}
if (isset($options['collection_names'])){
//$collections['names']=$options['collection_names'];
$names=$options['collection_names'];
unset($options['collection_names']);

foreach($names as $collection_name){
$collection_id=$this->Collection_model->get_collection_id_by_name($collection_name);
if ($collection_id){
$collections[]=$collection_id;
}
}

}
return $collections;
}


/**
*
Expand All @@ -191,7 +216,7 @@ function check_idno_get($idno=null)
*/
function create_post($type=null)
{
try{
try{
$user_id=$this->get_api_user_id();
$project_options=$this->raw_json_input();

Expand All @@ -202,6 +227,18 @@ function create_post($type=null)
$idno=$this->Editor_model->generate_uuid();
}

//overwrite
if (isset($project_options['overwrite'])
&& ($project_options['overwrite']==1
|| strtolower($project_options['overwrite'])=='true')){

$sid=$this->Editor_model->get_project_id_by_idno($idno);

if ($sid){
return $this->update_post($type,$sid);
}
}

$this->validate_project_idno($idno);

$options=array(
Expand Down Expand Up @@ -274,7 +311,8 @@ function update_post($type=null,$id=null,$validate=false)
$user=$this->api_user();
$user_id=$this->get_api_user_id();
$id=$this->get_sid($id);

$collections=$this->get_collection_options($options);

//check project exists and is of correct type
$exists=$this->Editor_model->check_id_exists($id,$type);

Expand All @@ -292,6 +330,11 @@ function update_post($type=null,$id=null,$validate=false)
$this->Editor_model->update_project($type,$id,$options,$validate);
$this->Editor_model->create_project_folder($id);

//add to collections
if (is_array($collections) && count($collections)>0){
$this->Collection_model->add_batch_projects($collections, array($id));
}

$response=array(
'status'=>'success'
);
Expand Down
13 changes: 13 additions & 0 deletions application/models/Collection_model.php
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,19 @@ function get_collection_by_user($user_id)
}


function get_collection_id_by_name($name)
{
$this->db->select('id');
$this->db->where('title',$name);
$this->db->where('pid',null);
$result=$this->db->get('editor_collections')->row_array();

if ($result){
return $result['id'];
}
}



/**
* Get collections by projects
Expand Down

0 comments on commit adb61af

Please sign in to comment.