public function execute(&$params) { // make sure this is a valid model type if (!is_subclass_of($this->config['modelClass'], 'X2Model')) { return array(false, ""); } if (!isset($this->config['attributes']) || empty($this->config['attributes'])) { return array(false, ""); } // verify that if create relationship option was set, that a relationship can be made if ($this->parseOption('createRelationship', $params)) { $acceptedModelTypes = X2Model::getModelTypesWhichSupportRelationships(); if (!in_array($this->config['modelClass'], $acceptedModelTypes)) { return array(false, Yii::t('x2flow', 'Relationships cannot be made with records ' . 'of type {type}.', array('{type}' => $this->config['modelClass']))); } if (!isset($params['model'])) { // no model passed to trigger return array(false, ''); } if (!in_array(get_class($params['model']), $acceptedModelTypes)) { return array(false, Yii::t('x2flow', 'Relationships cannot be made with records ' . 'of type {type}.', array('{type}' => get_class($params['model'])))); } } $model = new $this->config['modelClass'](); $model->setScenario('X2FlowCreateAction'); if ($this->setModelAttributes($model, $this->config['attributes'], $params) && $model->save()) { if ($this->parseOption('createRelationship', $params)) { Relationships::create(get_class($params['model']), $params['model']->id, get_class($model), $model->id); } return array(true, Yii::t('studio', 'View created record: ') . $model->getLink()); } else { $errors = $model->getErrors(); return array(false, array_shift($errors)); } }
/** * Creates a new lead and associates it with the contact * @param Contacts $contact * @param null|string $leadSource */ private static function generateLead(Contacts $contact, $leadSource = null) { $lead = new X2Leads('webForm'); $lead->firstName = $contact->firstName; $lead->lastName = $contact->lastName; $lead->leadSource = $leadSource; // disable validation to prevent saving from failing if leadSource isn't set if ($lead->save(false)) { Relationships::create('X2Leads', $lead->id, 'Contacts', $contact->id); } }
public function actionCreateRecords() { $contact = new Contacts(); $account = new Accounts(); $opportunity = new Opportunity(); $users = User::getNames(); if (isset($_POST['Contacts']) && isset($_POST['Accounts']) && isset($_POST['Opportunity'])) { $contact->setX2Fields($_POST['Contacts']); $account->setX2Fields($_POST['Accounts']); $opportunity->setX2Fields($_POST['Opportunity']); $validAccount = true; if ($account->validate() == false) { $validAccount = false; // validate other models so that the user gets feedback $contact->validate(); $opportunity->validate(); } if ($validAccount) { $allValid = true; $a = $this->createAccount($account, $account->attributes, '1'); // Contact and Opportunity require Account id for lookup field $contact->company = Fields::nameId($account->name, $account->id); if ($contact->validate() == false) { $allValid = false; } $c = $this->createContact($contact, $contact->attributes, '1'); $opportunity->accountName = Fields::nameId($account->name, $account->id); $opportunity->contactName = Fields::nameId($contact->name, $contact->id); if ($opportunity->validate() == false) { $allValid = false; } $o = $this->createOpportunity($opportunity, $opportunity->attributes, '1'); if ($allValid && $c && $a && $o) { // all records created? Relationships::create('Contacts', $contact->id, 'Accounts', $account->id); Relationships::create('Opportunity', $opportunity->id, 'Contacts', $contact->id); Relationships::create('Opportunity', $opportunity->id, 'Accounts', $account->id); if (isset($_GET['ret'])) { if ($_GET['ret'] == 'contacts') { $this->redirect(array("/contacts/contacts/view", 'id' => $contact->id)); } else { if ($_GET['ret'] == 'accounts') { $this->redirect(array("/accounts/accounts/view", 'id' => $account->id)); } else { if ($_GET['ret'] == 'opportunities') { $this->redirect(array("/opportunities/opportunities/view", 'id' => $opportunity->id)); } } } } else { $this->redirect(array("/contacts/contacts/view", $contact->id)); } } else { // otherwise clean up $types = array('account' => 'Accounts', 'contact' => 'Contacts', 'opportunity' => 'Opportunity'); foreach ($types as $model => $type) { if (${$model} && isset(${$model}->id)) { $modelId = ${$model}->id; ${$model}->delete(); // delete all new actions and events from creating/deleting records foreach (array('Actions', 'Events') as $meta) { X2Model::model($meta)->deleteAllByAttributes(array('associationId' => $modelId, 'associationType' => $type)); } } } } } } $this->render('createRecords', array('contact' => $contact, 'account' => $account, 'opportunity' => $opportunity, 'users' => $users)); }
/** * Creates a new relationship and then, based on the value of attributesOfNewRecordToUpdate, * sets values of the second model using values of the first model. * Returns an array of the values that were changed indexed by the attribute name. * @param object $firstModel * @param string $firstModelNamethe class name of the first model * @param string $firstModelId the id of the first model * @param string $secondModelName the class name of the second model * @param string $secondModelId the id of the second model * @return mixed false if the second model isn't updated, the second model otherwise */ private function quickCreateRelationship($firstModel, $firstModelName, $firstModelId, $secondModelName, $secondModelId) { $success = Relationships::create($firstModelName, $firstModelId, $secondModelName, $secondModelId); $attributesToUpdate = isset($this->attributesOfNewRecordToUpdate[$secondModelName]) ? $this->attributesOfNewRecordToUpdate[$secondModelName] : array(); $secondModel = $secondModelName::model()->findByPk($secondModelId); if ($secondModel) { $changed = false; /* Set values of existing record to values of newly created record based on mapping configured in $attributesOfNewRecordToUpdate */ foreach ($attributesToUpdate as $firstModelAttr => $secondModelAttr) { if (isset($firstModel->{$firstModelAttr}) && (!isset($secondModel->{$secondModelAttr}) || $secondModel->{$secondModelAttr} === '')) { $secondModel->{$secondModelAttr} = $firstModel->{$firstModelAttr}; $changed = true; } } if ($changed) { $secondModel->update(); } } if ($secondModel && $changed) { return $secondModel; } else { return false; } }