diff --git a/app/controller/api.php b/app/controller/api.php index dba5892..826a0d6 100644 --- a/app/controller/api.php +++ b/app/controller/api.php @@ -284,7 +284,7 @@ class ApiController extends BaseController { $external = (bool)$request->params()->query('external', false); if (!$external) { - PlantsModel::editPlantPhoto($plantId, 'photo', 'photo'); + PlantsModel::editPlantPhoto($plantId, 'photo', 'photo', true); } else { $photo = $request->params()->query('photo', null); diff --git a/app/models/PlantsModel.php b/app/models/PlantsModel.php index 1795e21..fe7b61d 100644 --- a/app/models/PlantsModel.php +++ b/app/models/PlantsModel.php @@ -386,15 +386,20 @@ class PlantsModel extends \Asatru\Database\Model { * @param $plantId * @param $attribute * @param $value + * @param $api * @return void * @throws \Exception */ - public static function editPlantPhoto($plantId, $attribute, $value) + public static function editPlantPhoto($plantId, $attribute, $value, $api = false) { try { - $user = UserModel::getAuthUser(); - if (!$user) { - throw new \Exception('Invalid user'); + $user = null; + + if (!$api) { + $user = UserModel::getAuthUser(); + if (!$user) { + throw new \Exception('Invalid user'); + } } if ((!isset($_FILES[$value])) || ($_FILES[$value]['error'] !== UPLOAD_ERR_OK)) { @@ -415,12 +420,14 @@ class PlantsModel extends \Asatru\Database\Model { throw new \Exception('createThumbFile failed'); } - static::raw('UPDATE `@THIS` SET ' . $attribute . ' = ?, last_edited_user = ?, last_edited_date = CURRENT_TIMESTAMP, last_photo_date = CURRENT_TIMESTAMP WHERE id = ?', [$file_name . '_thumb.' . $file_ext, $user->get('id'), $plantId]); + static::raw('UPDATE `@THIS` SET ' . $attribute . ' = ?, last_edited_user = ?, last_edited_date = CURRENT_TIMESTAMP, last_photo_date = CURRENT_TIMESTAMP WHERE id = ?', [$file_name . '_thumb.' . $file_ext, $user?->get('id'), $plantId]); - LogModel::addLog($user->get('id'), $plantId, $attribute, $value, url('/plants/details/' . $plantId)); + if (!$api) { + LogModel::addLog($user->get('id'), $plantId, $attribute, $value, url('/plants/details/' . $plantId)); + } if (app('system_message_plant_log')) { - PlantLogModel::addEntry($plantId, '[System] ' . $attribute . ' = ' . $file_name . '.' . $file_ext); + PlantLogModel::addEntry($plantId, '[System] ' . $attribute . ' = ' . $file_name . '.' . $file_ext, $api); } } catch (\Exception $e) { throw $e;