false, 'message' => Yii::t('api', $this->_errorMessages['none']), ); $this->displayJson($result); } return $model; } protected function _getSearchModel() { return new ObjectSearch(); } /** * POST int $id object id */ public function actionImageList() { //$data = $this->getInputJson(); $data = $this->getRequestGetParams(); if (empty($data['id']) || !is_numeric($data['id'])) { throw new Exception('Wrong request params'); } $nodeId = (int) $data['id']; $result = [ 'status' => true, 'data' => [ 'id' => $nodeId, 'imageList' => Object::getImageList($nodeId) ] ]; $this->displayJson($result); } public function actionUpload() { $request = \Yii::$app->request; $id = $request ? $request->getBodyParam('id') : null; if (empty($id)) { throw new Exception('wrong request due to missing required option $id'); } if (empty($_FILES)) { throw new UploadException('no file received'); } if (UPLOAD_ERR_OK !== $_FILES['file']['error']) { throw new UploadException($_FILES['file']['error']); } if (false === is_uploaded_file($_FILES['file']['tmp_name'])) { throw new UploadException('file is not uploaded'); } $model = $this->_findModel(['id' => $id]); /** @var UploadedFile $image */ $image = UploadedFile::getInstanceByName('file'); $fileId = $image->saveImageAs($image, [ 'nodeId' => $model->getId(), ]); if ($fileId) { $result = ['status' => true, 'fileId' => $fileId]; } else { $result = ['status' => false, 'message' => $image->getErrors()]; } return $result; } /** * Delete uploaded tag image * POST int $id tag id * POST int $imageId image id */ public function actionImageDelete() { $data = $this->getInputJson(); $image = Image::find(['imageId' => $data['imageId'], 'nodeId' => $data['id']]); $status = (bool) $image->delete(); $result = [ 'status' => $status, ]; $this->displayJson($result); } /** * Update order for images * POST int $id tag id * POST array $imageIds list of reordered image ids */ public function actionImageReorder() { $data = $this->getInputJson(); if (empty($data['id']) || empty($data['imageIds'])) { throw new Exception('Wrong request'); } $image = new Image(); $status = (bool) $image->updateSorter($data['imageIds']); $result = [ 'status' => $status, ]; $this->displayJson($result); } /** * Update file meta * POST int $id tag id * POST array $data file meta data */ public function actionImageSave() { $data = $this->getInputJson(); if (empty($data['id']) || empty($data['data']) || empty($data['data']['imageId'])) { throw new Exception('Wrong request'); } $fileMeta = $data['data']; /** @var Image $model */ $model = Image::find(['imageId' => $fileMeta['imageId'], 'nodeId' => $data['id']]); if (null == $model) { $this->displayJson(['status' => false, 'message' => Yii::t('api', 'Specified image not found')]); exit; } unset($fileMeta['imageId']); $model->scenario = 'update'; if ($model->load([$model->formName() => $fileMeta]) && $model->update()) { $result = ['status' => true]; } else { $result = ['status' => false, 'errors' => $model->errors, 'message' => Yii::t('api', 'File has not been updated')]; } $this->displayJson($result); } }