/** * Add a record to record relationship * * A record can be a contact, opportunity, or account. This function is * called via ajax from the Relationships Widget. */ public function actionAddRelationship() { //check if relationship already exits if (isset($_POST['ModelName']) && isset($_POST['ModelId']) && isset($_POST['RelationshipModelName']) && isset($_POST['RelationshipModelId'])) { $modelName = $_POST['ModelName']; $modelId = $_POST['ModelId']; $relationshipModelName = $_POST['RelationshipModelName']; $relationshipModelId = $_POST['RelationshipModelId']; $model = $this->getModelFromTypeAndId($modelName, $modelId); if (!Yii::app()->controller->checkPermissions($model, 'edit')) { $this->denied(); } $relationshipModel = $this->getModelFromTypeAndId($relationshipModelName, $relationshipModelId); if (!Yii::app()->controller->checkPermissions($relationshipModel, 'view')) { $this->denied(); } if (isset($_POST['mutual']) && $_POST['mutual'] == 'true') { $_POST['secondLabel'] = $_POST['firstLabel']; } $relationship = new Relationships(); $relationship->firstType = $_POST['ModelName']; $relationship->firstId = $_POST['ModelId']; $relationship->firstLabel = $_POST['firstLabel']; $relationship->secondType = $_POST['RelationshipModelName']; $relationship->secondId = $_POST['RelationshipModelId']; $relationship->secondLabel = $_POST['secondLabel']; if ($relationship->hasDuplicates()) { echo 'duplicate'; Yii::app()->end(); } if ($relationship->save()) { echo 'success'; Yii::app()->end(); } else { echo 'failure'; Yii::app()->end(); } } else { throw new CHttpException(400, Yii::t('app', 'Bad Request')); } }