public function actionCreate() { $model = new Clients(); $account = new Accounts(); $preference = new Preferences(); if (isset($_POST['Clients'], $_POST['Accounts'], $_POST['Preferences'])) { $model->attributes = $_POST['Clients']; $account->attributes = $_POST['Accounts']; $preference->attributes = $_POST['Preferences']; $model->login_name = $account->login_name; $model->client_code = RandomGenerator::generateString(AdminGlobals::CLIENT_CODE_LENGTH); $model->subscription_status = "active"; $preference->prepaid_passwd = RandomGenerator::generateString(AdminGlobals::PREPAID_PASSWD_LENGTH); $account->account_name = $model->client_name; $account->email = $model->email; $account->is_visible = 0; $valid = $model->validate(); $valid = $account->validate() && $valid; $valid = $preference->validate() && $valid; if ($valid) { $transaction = Yii::app()->db->beginTransaction(); $success = $model->save(false); if ($success) { $account->client_id = $preference->client_id = $model->id; } $success = $success && $account->save(false); $success = $success && $preference->save(false); if ($success) { $transaction->commit(); if (Yii::app()->request->isAjaxRequest) { $this->renderPartial('view', array('model' => $this->loadModel($model->id), 'account' => $account, 'preference' => $preference), false, true); Yii::app()->end(); } $this->redirect(array('view', 'id' => $model->id)); } $transaction->rollBack(); } } if (Yii::app()->request->isAjaxRequest) { $this->renderPartial('create', array('model' => $model, 'account' => $account, 'preference' => $preference), false, true); Yii::app()->end(); } $this->render('create', array('model' => $model, 'account' => $account, 'preference' => $preference)); }
public function actionAdduser() { $model = new Accounts(); // uncomment the following code to enable ajax-based validation /* if(isset($_POST['ajax']) && $_POST['ajax']==='accounts-edituser-form') { echo CActiveForm::validate($model); Yii::app()->end(); } */ if (isset($_POST['Accounts'])) { $model->attributes = $_POST['Accounts']; if ($model->validate()) { $newSalt = $this->generateSalt(10); $newPass = md5($newSalt . $model->password); $model->salt = $newSalt; $model->password = $newPass; $model->save(); $this->redirect(array('index')); } } $this->render('adduser', array('model' => $model)); }
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)); }