'Object has been created', 'update' => 'Object data has been updated', 'delete' => 'Object has been deleted' ); protected $_errorMessages = array( 'create' => 'Object has not been created', 'update' => 'Object data has not been updated', 'none' => 'Wrong parameter: $id', 'status' => 'Object status mast be "draft"' ); public function behaviors() { return [ 'access' => [ 'class' => AccessControlBehavior::className(), 'rules' => [ [ 'allow' => Yii::$app->user->checkAccess('editObject'), 'actions' => ['list', 'status-list', 'view', 'read', 'create', 'save', 'update', 'delete', 'recommend-geo', 'link-geo', 'unlink-geo', 'recommend-tag', 'link-tag', 'unlink-tag', 'set-status', 'save-geo-attribute', 'save-attribute', 'image-list', 'image-reorder', 'image-delete', 'image-save', 'upload' ], 'roles' => ['@'] ], [ 'allow' => true, 'actions' => [ 'recommend-user', 'link-user', 'unlink-user', ], 'roles' => ['manager', 'admin'] ], // deny all [ 'allow' => false ] ] ] ]; } /** * @param $data * * @return \app\models\Object|\app\models\AbstractObject * @throws \yii\base\InvalidConfigException */ protected function _findModel($data) { if (!isset($data['id'])) { return new Object(); } else if(($model = Object::find($data['id'])) == null) { $result = array( 'status' => false, 'message' => Yii::t('api', $this->_errorMessages['none']), ); $this->displayJson($result); } return $model; } /** * Recommend available links for object * INPUT: string id */ public function actionRecommendTag() { //$request = $this->getInputJson(); $request = $this->getRequestGetParams(); $model = $this->_findModel(array()); $result = [ 'status' => true, 'data' => [ 'list' => $model->getRecommendAmenitySubmissionTag((int) $request['id']) ] ]; $this->displayJson($result); } public function actionStatusList() { $result = [ 'status' => true, 'data' => [ 'statusList' => Object::getStatusList(), ] ]; $this->displayJson($result); } /** * Creates object and returns its data * Return object data */ public function actionCreate() { $model = $this->_findModel([]); $id = $model->createEmptyTicket(); $result = [ 'status' => true, 'data' => [ // оголошення користувача (objectT) 'ticket' => $model->getAttributes(), 'links' => [], // вказує на можливість мати гео мітки, рекомендовані атрибути 'recommend' => $model->getRecommendationInfo($id), ] ]; $this->displayJson($result); } /** * Read object data */ public function actionRead() { //$request = $this->getInputJson(); $request = $this->getRequestGetParams(); $id = (int) $request['id']; $model = $this->_findModel(array('id' => $id)); $result = [ 'status' => true, // TicketFullInfo 'data' => [ // оголошення користувача (objectT) 'ticket' => $model->getAttributes(), // звязки оголошення 'links' => [ 'geo' => $model->getLinkedGeoTag($id), 'geoAttr' => $model->getLinkedGeoAttributes($id), 'tag' => $model->getTagGrouped($id), 'user' => $model->getTicketUsers($id), ], // вказує на можливість мати гео мітки, рекомендовані атрибути 'recommend' => $model->getRecommendationInfo($id), ] ]; $this->displayJson($result); } /** * Save user ticket data * INPUT: string id * INPUT: string description */ public function actionUpdate() { $request = $this->getInputJson(); $request = $this->_prepareData($request); $result = $this->_update($request); $this->displayJson($result); } /** * Recommend available links for object * INPUT: string id * INPUT: string part */ public function actionRecommendGeo() { //$request = $this->getInputJson(); $request = $this->getRequestGetParams(); $model = $this->_findModel(array()); $result = [ 'status' => true, 'data' => [ 'list' => $model->getRecommendGeoTag((int) $request['id'], $request['part']) ] ]; $this->displayJson($result); } /** * Link object to geo tag * INPUT: string id * INPUT: string tagId */ public function actionLinkGeo() { $request = $this->getInputJson(); $id = (int) $request['id']; $tagId = (int) $request['tagId']; $model = $this->_findModel([]); $model->linkGeoTag($tagId, $id); $result = [ 'status' => true, 'data' => [ 'ticket' => ['address' => $this->_findModel(['id' => $id])->address], 'links' => [ 'geo' => $model->getLinkedGeoTag($id), 'geoAttr' => $model->getLinkedGeoAttributes($id), ], 'recommend' => $model->getRecommendationInfo($id), ] ]; $this->displayJson($result); } /** * Unlink object from geo tag * INPUT: string id * INPUT: string tagId */ public function actionUnlinkGeo() { $request = $this->getInputJson(); $id = (int) $request['id']; $tagId = (int) $request['tagId']; $model = $this->_findModel([]); $model->unlinkGeoTag($tagId, $id); $result = [ 'status' => true, 'data' => [ 'ticket' => ['address' => $this->_findModel(['id' => $id])->address], 'links' => [ 'geo' => $model->getLinkedGeoTag($id), 'geoAttr' => $model->getLinkedGeoAttributes($id), ], 'recommend' => $model->getRecommendationInfo($id), ] ]; $this->displayJson($result); } /** * Link object to tag * INPUT: string id */ public function actionLinkTag() { $request = $this->getInputJson(); $id = (int) $request['id']; $tagId = (int) $request['tagId']; $model = $this->_findModel(array()); $model->linkTag($tagId, $id); $result = [ 'status' => true, 'data' => [ 'links' => [ 'tag' => $model->getTagGrouped($id), ], ] ]; $this->displayJson($result); } /** * Unlink object from tag * INPUT: string id * INPUT: string tagId */ public function actionUnlinkTag() { $request = $this->getInputJson(); $id = (int) $request['id']; $tagId = (int) $request['tagId']; $model = $this->_findModel(array()); $model->unlinkTag($tagId, $id); $result = [ 'status' => true, 'data' => [ 'links' => [ 'tag' => $model->getTagGrouped($id), ], ] ]; $this->displayJson($result); } /** * Change ticket status * INPUT: string id * INPUT: string status */ public function actionSetStatus() { $request = $this->_prepareData($this->getInputJson()); $id = (int) $request['id']; $status = $request['status']; $model = $this->_findModel(['id' => $id]); $model->scenario = 'update'; $result = $model->setStatus($status); $this->displayJson($this->_getStatusResult($result, $model)); } public function actionSaveGeoAttribute() { $request = $this->getInputJson(); $id = (int) $request['id']; $tagId = (int) $request['tagId']; $value = $request['value']; $model = $this->_findModel([]); $model->attributeSetValue($id, $tagId, $value); $result = [ 'status' => true, 'data' => [ 'ticket' => ['address' => $this->_findModel(['id' => $id])->address], 'links' => [ 'geo' => $model->getLinkedGeoTag($id), 'geoAttr' => $model->getLinkedGeoAttributes($id), ], 'recommend' => $model->getRecommendationInfo($id), ] ]; $this->displayJson($result); } public function actionSaveAttribute() { $request = $this->getInputJson(); $id = (int) $request['id']; $tagId = (int) $request['tagId']; $value = $request['value']; $model = $this->_findModel([]); $model->attributeSetValue($id, $tagId, $value); $result = [ 'status' => true, 'data' => [ // 'tag' => $model->getLinkedAttribute($id, $tagId), ] ]; $this->displayJson($result); } protected function _getStatusResult($result, AbstractObject $model) { if ($result) { $result = [ 'status' => true, 'message' => Yii::t('api', $this->_successMessages['update']), ]; } else { $result = [ 'status' => false, 'message' => Yii::t('api', $this->_errorMessages['update']), ]; } return $result; } /** * Recommend available users for object * INPUT: string id * INPUT: string part */ public function actionRecommendUser() { $request = $this->getRequestGetParams(); $model = $this->_findModel(array()); $result = [ 'data' => [ 'list' => $model->getRecommendUsers((int) $request['id'], $request['part']) ] ]; $this->displayJson($result); } /** * Link object to user * INPUT: string id * INPUT: string tagId user ID */ public function actionLinkUser() { $request = $this->getInputJson(); $id = (int) $request['id']; $userId = (int) $request['tagId']; $model = $this->_findModel([]); $model->linkUser($userId, $id); $result = [ 'data' => [ 'links' => [ 'user' => $model->getTicketUsers($id), ], ] ]; $this->displayJson($result); } /** * Unlink object from user tag * INPUT: string id * INPUT: string tagId user ID */ public function actionUnlinkUser() { $request = $this->getInputJson(); $id = (int) $request['id']; $userId = (int) $request['tagId']; $model = $this->_findModel([]); $model->unlinkUser($userId, $id); $result = [ 'data' => [ 'links' => [ 'user' => $model->getTicketUsers($id), ], ] ]; $this->displayJson($result); } }