/**
  * Updates a particular model.
  * If update is successful, the browser will be redirected to the 'view' page.
  * @param integer $id the ID of the model to be updated
  */
 public function actionUpdate($id)
 {
     $model = $this->loadModel($id);
     $users = User::getNames();
     $fields = Fields::model()->findAllByAttributes(array('modelName' => "Products"));
     foreach ($fields as $field) {
         if ($field->type == 'link') {
             $fieldName = $field->fieldName;
             $type = ucfirst($field->linkType);
             if (is_numeric($model->{$fieldName}) && $model->{$fieldName} != 0) {
                 eval("\$lookupModel={$type}::model()->findByPk(" . $model->{$fieldName} . ");");
                 if (isset($lookupModel)) {
                     $model->{$fieldName} = $lookupModel->name;
                 }
             }
         }
     }
     if (isset($_POST['Product'])) {
         $temp = $model->attributes;
         foreach ($_POST['Product'] as $name => $value) {
             if ($value == $model->getAttributeLabel($name)) {
                 $_POST['Product'][$name] = '';
             }
         }
         foreach ($_POST as $key => $arr) {
             $pieces = explode("_", $key);
             if (isset($pieces[0]) && $pieces[0] == 'autoselect') {
                 $newKey = $pieces[1];
                 if (isset($_POST[$newKey . "_id"]) && $_POST[$newKey . "_id"] != "") {
                     $val = $_POST[$newKey . "_id"];
                 } else {
                     $field = Fields::model()->findByAttributes(array('fieldName' => $newKey));
                     if (isset($field)) {
                         $type = ucfirst($field->linkType);
                         if ($type != "Contacts") {
                             eval("\$lookupModel={$type}::model()->findByAttributes(array('name'=>'{$arr}'));");
                         } else {
                             $names = explode(" ", $arr);
                             $lookupModel = Contacts::model()->findByAttributes(array('firstName' => $names[0], 'lastName' => $names[1]));
                         }
                         if (isset($lookupModel)) {
                             $val = $lookupModel->id;
                         } else {
                             $val = $arr;
                         }
                     }
                 }
                 $model->{$newKey} = $val;
             }
         }
         foreach (array_keys($model->attributes) as $field) {
             if (isset($_POST['Product'][$field])) {
                 $model->{$field} = $_POST['Product'][$field];
                 $fieldData = Fields::model()->findByAttributes(array('modelName' => 'Products', 'fieldName' => $field));
                 if ($fieldData->type == 'assignment' && $fieldData->linkType == 'multiple') {
                     $model->{$field} = Accounts::parseUsers($model->{$field});
                 } elseif ($fieldData->type == 'date') {
                     $model->{$field} = strtotime($model->{$field});
                 }
             }
         }
         // generate history
         $action = new Actions();
         $action->associationType = 'product';
         $action->associationId = $model->id;
         $action->associationName = $model->name;
         $action->assignedTo = Yii::app()->user->getName();
         $action->completedBy = Yii::app()->user->getName();
         $action->dueDate = time();
         $action->completeDate = time();
         $action->visibility = 1;
         $action->complete = 'Yes';
         $action->actionDescription = "Update: <b>{$model->name}</b>\n\t\t\t\tType: <b>{$model->type}</b>\n\t\t\t\tPrice: <b>{$model->price}</b>\n\t\t\t\tCurrency: <b>{$model->currency}</b>\n\t\t\t\tInventory: <b>{$model->inventory}</b>";
         $action->save();
         parent::update($model, $temp, '0');
     }
     $this->render('update', array('model' => $model, 'users' => $users));
 }
 public function actionChangePermissions($id)
 {
     $model = $this->loadModel($id);
     if (Yii::app()->user->getName() == 'admin' || Yii::app()->user->getName() == $model->createdBy) {
         $users = User::getNames();
         unset($users['admin']);
         unset($users['Anyone']);
         $str = $model->editPermissions;
         $pieces = explode(", ", $str);
         $model->editPermissions = $pieces;
         if (isset($_POST['DocChild'])) {
             $model->attributes = $_POST['DocChild'];
             $arr = $model->editPermissions;
             $model->editPermissions = Accounts::parseUsers($arr);
             if ($model->save()) {
                 $this->redirect(array('view', 'id' => $id));
             }
         }
         $this->render('editPermissions', array('model' => $model, 'users' => $users));
     } else {
         $this->redirect(array('view', 'id' => $id));
     }
 }
示例#3
0
 /**
  * Sets attributes using X2Fields
  * @param array &$data array of attributes to be set (eg. $_POST['Contacts'])
  */
 public function setX2Fields(&$data)
 {
     $this->queryFields();
     foreach (self::$_fields[$this->tableName()] as &$_field) {
         // now loop through fields to deal with special types
         $fieldName = $_field->fieldName;
         if (!$_field->readOnly && isset($data[$fieldName])) {
             // skip fields that are read-only or haven't been set
             $value = $data[$fieldName];
             if ($value == $this->getAttributeLabel($fieldName)) {
                 // eliminate placeholder values
                 $value = '';
             }
             if ($_field->type == 'assignment' && $_field->linkType == 'multiple') {
                 $value = Accounts::parseUsers($value);
             } elseif ($_field->type == 'date') {
                 $value = Yii::app()->controller->parseDate($value);
                 if ($value === false) {
                     $value = null;
                 }
             } elseif ($_field->type == 'link' && !empty($_field->linkType)) {
                 if (!empty($value) && isset($data[$fieldName . '_id'])) {
                     // check the ID, if provided
                     $linkId = $data[$fieldName . '_id'];
                     if (!empty($linkId) && CActiveRecord::model($_field->linkType)->countByAttributes(array('id' => $linkId))) {
                         // if the link model actually exists,
                         $value = $linkId;
                     }
                     // then use the ID as the field value
                 }
                 if (!empty($value) && !ctype_digit($value)) {
                     // if the field is sitll text, try to find the ID based on the name
                     if ($_field->linkType == 'Contacts') {
                         $fullname = explode(' ', $value);
                         $firstName = $fullname[0];
                         $lastName = $fullname[1];
                         $linkModel = CActiveRecord::model($_field->linkType)->findByAttributes(array('firstName' => $firstName, 'lastName' => $lastName));
                     } else {
                         $linkModel = CActiveRecord::model($_field->linkType)->findByAttributes(array('name' => $value));
                     }
                     if (isset($linkModel)) {
                         $value = $linkModel->id;
                     }
                 }
             }
             $this->{$fieldName} = $value;
         }
     }
 }
 public function actionQuickUpdate($id)
 {
     $model = $this->loadModel($id);
     foreach (array_keys($model->attributes) as $field) {
         if (isset($_POST['Quote'][$field])) {
             $model->{$field} = $_POST['Quote'][$field];
             $fieldData = Fields::model()->findByAttributes(array('modelName' => 'Quotes', 'fieldName' => $field));
             if ($fieldData->type == 'assignment' && $fieldData->linkType == 'multiple') {
                 $model->{$field} = Accounts::parseUsers($model->{$field});
             } elseif ($fieldData->type == 'date') {
                 $model->{$field} = $this->parseDate($model->{$field});
             }
         }
     }
     $model->save();
     $allProducts = Product::model()->findAll(array('select' => 'id, name, price'));
     $productNames = array(0 => '');
     foreach ($allProducts as $product) {
         $productNames[$product->id] = $product->name;
     }
     $model->lastUpdated = time();
     $model->updatedBy = Yii::app()->user->name;
     // get products
     if (isset($_POST['ExistingProducts'])) {
         $ids = $_POST['ExistingProducts']['id'];
         $prices = $_POST['ExistingProducts']['price'];
         $quantities = $_POST['ExistingProducts']['quantity'];
         $adjustments = $_POST['ExistingProducts']['adjustment'];
         $products = array();
         foreach ($ids as $key => $id) {
             if ($id != 0) {
                 // remove blanks
                 $products[$key]['id'] = $id;
                 $products[$key]['name'] = $productNames[$id];
                 $products[$key]['price'] = $prices[$key];
                 $products[$key]['quantity'] = $quantities[$key];
                 if (strchr($adjustments[$key], '%')) {
                     // percent adjustment
                     $products[$key]['adjustment'] = floatval(str_replace("%", "", $adjustments[$key]));
                     $products[$key]['adjustmentType'] = 'percent';
                 } else {
                     $products[$key]['adjustment'] = $adjustments[$key];
                     $products[$key]['adjustmentType'] = 'linear';
                 }
             }
         }
         // update products
         $orders = QuoteProduct::model()->findAllByAttributes(array('quoteId' => $model->id));
         foreach ($orders as $order) {
             $found = false;
             foreach ($products as $key => $product) {
                 if ($order->productId == $product['id']) {
                     $order->price = $product['price'];
                     $order->quantity = $product['quantity'];
                     $order->adjustment = $product['adjustment'];
                     $order->adjustmentType = $product['adjustmentType'];
                     $order->save();
                     unset($products[$key]);
                     $found = true;
                     break;
                 }
             }
             if (!$found) {
                 $order->delete();
             }
         }
         // tie new products to quote
         foreach ($products as $product) {
             $qp = new QuoteProduct();
             $qp->quoteId = $model->id;
             $qp->productId = $product['id'];
             $qp->name = $product['name'];
             $qp->price = $product['price'];
             $qp->quantity = $product['quantity'];
             $qp->adjustment = $product['adjustment'];
             $qp->adjustmentType = $product['adjustmentType'];
             $qp->save();
         }
     }
     $contact = Contacts::model()->findByPk($_POST['contactId']);
     // generate history
     $action = new Actions();
     $action->associationType = 'contacts';
     $action->type = 'quotes';
     $action->associationId = $contact->id;
     $action->associationName = $contact->name;
     $action->assignedTo = Yii::app()->user->getName();
     $action->completedBy = Yii::app()->user->getName();
     $action->dueDate = time();
     $action->completeDate = time();
     $action->visibility = 1;
     $action->complete = 'Yes';
     $created = Yii::app()->dateFormatter->format(Yii::app()->locale->getDateFormat('long'), $model->createDate);
     $updated = Yii::app()->dateFormatter->format(Yii::app()->locale->getDateFormat('long'), $model->lastUpdated);
     $expires = Yii::app()->dateFormatter->format(Yii::app()->locale->getDateFormat('long'), $model->expirationDate);
     $description = "Updated Quote\n\t\t<span style=\"font-weight: bold; font-size: 1.25em;\">{$model->id}</span> {$model->name} ({$model->status})\n\t\tCreated: <b>{$created}</b>\n\t\tUpdated: <b>{$updated}</b> by <b>{$model->updatedBy}</b>\n\t\tExpires: <b>{$expires}</b>\n\n";
     $table = $model->productTable();
     $table = str_replace("\n", "", $table);
     $table = str_replace("\t", "", $table);
     $description .= $table;
     $action->actionDescription = $description;
     $action->save();
     if (isset($_POST['contactId'])) {
         Yii::app()->clientScript->scriptMap['*.js'] = false;
         $contact = Contacts::model()->findByPk($_POST['contactId']);
         $this->renderPartial('quoteFormWrapper', array('model' => $contact), false, true);
     }
 }
 /**
  * update calendar with id $id
  */
 public function actionUpdate($id)
 {
     $model = $this->loadModel($id);
     if (isset($_POST['X2Calendar'])) {
         // check for empty permissions
         if (!isset($_POST['X2Calendar']['viewPermission'])) {
             $model->viewPermission = '';
         }
         if (!isset($_POST['X2Calendar']['editPermission'])) {
             $model->editPermission = '';
         }
         // copy $_POST data into Calendar model
         foreach (array_keys($model->attributes) as $field) {
             if (isset($_POST['X2Calendar'][$field])) {
                 $model->{$field} = $_POST['X2Calendar'][$field];
                 $fieldData = Fields::model()->findByAttributes(array('modelName' => 'Calendar', 'fieldName' => $field));
                 if ($fieldData->type == 'assignment' && $fieldData->linkType == 'multiple') {
                     $model->{$field} = Accounts::parseUsers($model->{$field});
                 } elseif ($fieldData->type == 'date') {
                     $model->{$field} = strtotime($model->{$field});
                 }
             }
         }
         $model->updatedBy = Yii::app()->user->name;
         $model->lastUpdated = time();
         $model->save();
         $this->redirect(array('view', 'id' => $model->id));
     }
     $admin = Yii::app()->params->admin;
     $googleIntegration = $admin->googleIntegration;
     $this->render('update', array('model' => $model, 'googleIntegration' => $googleIntegration));
 }
 public function actionCreatePage()
 {
     $model = new DocChild();
     $users = User::getNames();
     if (isset($_POST['DocChild'])) {
         $model->attributes = $_POST['DocChild'];
         $arr = $model->editPermissions;
         if (isset($arr)) {
             $model->editPermissions = Accounts::parseUsers($arr);
         }
         $model->createdBy = 'admin';
         $model->createDate = time();
         $model->lastUpdated = time();
         $model->updatedBy = 'admin';
         $module = new Modules();
         $module->adminOnly = 0;
         $module->toggleable = 1;
         $module->custom = 1;
         $module->visible = 1;
         $module->editable = 0;
         $module->searchable = 0;
         $module->menuPosition = Modules::model()->count();
         $module->name = 'document';
         $module->title = $model->title;
         if ($module->save()) {
             if ($model->save()) {
                 $this->redirect(array('viewPage', 'id' => $model->id));
             }
         }
     }
     $this->render('createPage', array('model' => $model, 'users' => $users));
 }
 public function actionQuickContact()
 {
     //exit("ha");
     $model = new Contacts();
     $attributeLabels = $model->attributeLabels();
     // if it is ajax validation request
     // if(isset($_POST['ajax']) && $_POST['ajax']=='quick-contact-form') {
     // echo CActiveForm::validate($model);
     // Yii::app()->end();
     // }
     // collect user input data
     if (isset($_POST['Contacts'])) {
         // clear values that haven't been changed from the default
         foreach ($_POST['Contacts'] as $name => &$value) {
             if ($value == $model->getAttributeLabel($name)) {
                 $value = '';
             }
         }
         foreach ($_POST as $key => $arr) {
             $pieces = explode("_", $key);
             if (isset($pieces[0]) && $pieces[0] == 'autoselect') {
                 $newKey = $pieces[1];
                 if (isset($_POST[$newKey . "_id"]) && $_POST[$newKey . "_id"] != "") {
                     $val = $_POST[$newKey . "_id"];
                 } else {
                     $field = Fields::model()->findByAttributes(array('fieldName' => $newKey));
                     if (isset($field)) {
                         $type = ucfirst($field->linkType);
                         if ($type != "Contacts") {
                             eval("\$lookupModel={$type}::model()->findByAttributes(array('name'=>'{$arr}'));");
                         } else {
                             $names = explode(" ", $arr);
                             if (count($names) > 1) {
                                 $lookupModel = Contacts::model()->findByAttributes(array('firstName' => $names[0], 'lastName' => $names[1]));
                             }
                         }
                         if (isset($lookupModel)) {
                             $val = $lookupModel->id;
                         } else {
                             $val = $arr;
                         }
                     }
                 }
                 $model->{$newKey} = $val;
             }
         }
         $temp = $model->attributes;
         foreach (array_keys($model->attributes) as $field) {
             if (isset($_POST['Contacts'][$field])) {
                 $model->{$field} = $_POST['Contacts'][$field];
                 $fieldData = Fields::model()->findByAttributes(array('modelName' => 'Contacts', 'fieldName' => $field));
                 if ($fieldData->type == 'assignment' && $fieldData->linkType == 'multiple') {
                     $model->{$field} = Accounts::parseUsers($model->{$field});
                 } elseif ($fieldData->type == 'date') {
                     $model->{$field} = strtotime($model->{$field});
                 }
             }
         }
         $model->visibility = 1;
         // validate user input and save contact
         $changes = $this->calculateChanges($temp, $model->attributes, $model);
         $model = $this->updateChangelog($model, 'Create');
         $model->createDate = time();
         if ($model->save()) {
             $this->renderPartial('application.components.views.quickContact', array());
         }
         //else print_r($model->getErrors());
     }
 }
 public function actionAddUser($id)
 {
     $users = User::getNames();
     unset($users['admin']);
     unset($users['']);
     foreach (Groups::model()->findAll() as $group) {
         $users[$group->id] = $group->name;
     }
     $contacts = Contacts::getAllNames();
     $model = $this->loadModel($id);
     $users = Accounts::editUserArray($users, $model);
     // Uncomment the following line if AJAX validation is needed
     // $this->performAjaxValidation($model);
     if (isset($_POST['Accounts'])) {
         $temp = $model->assignedTo;
         $tempArr = $model->attributes;
         $model->attributes = $_POST['Accounts'];
         $arr = $_POST['Accounts']['assignedTo'];
         $model->assignedTo = Accounts::parseUsers($arr);
         if ($temp != "") {
             $temp .= ", " . $model->assignedTo;
         } else {
             $temp = $model->assignedTo;
         }
         $model->assignedTo = $temp;
         $changes = $this->calculateChanges($tempArr, $model->attributes);
         $model = $this->updateChangelog($model, $changes);
         if ($model->save()) {
             $this->redirect(array('view', 'id' => $model->id));
         }
     }
     $this->render('addUser', array('model' => $model, 'users' => $users, 'contacts' => $contacts, 'action' => 'Add'));
 }
 /**
  * Updates a particular model.
  * If update is successful, the browser will be redirected to the 'view' page.
  * @param integer $id the ID of the model to be updated
  */
 public function actionUpdate($id)
 {
     $model = $this->loadModel($id);
     $users = User::getNames();
     $fields = Fields::model()->findAllByAttributes(array('modelName' => "Templates"));
     foreach ($fields as $field) {
         if ($field->type == 'link') {
             $fieldName = $field->fieldName;
             $type = ucfirst($field->linkType);
             if (is_numeric($model->{$fieldName}) && $model->{$fieldName} != 0) {
                 eval("\$lookupModel={$type}::model()->findByPk(" . $model->{$fieldName} . ");");
                 if (isset($lookupModel)) {
                     $model->{$fieldName} = $lookupModel->name;
                 }
             }
         } elseif ($field->type == 'date') {
             $fieldName = $field->fieldName;
             $model->{$fieldName} = date("Y-m-d", $model->{$fieldName});
         }
     }
     if (isset($_POST['Templates'])) {
         $temp = $model->attributes;
         foreach ($_POST['Templates'] as $name => $value) {
             if ($value == $model->getAttributeLabel($name)) {
                 $_POST['Templates'][$name] = '';
             }
         }
         foreach ($_POST as $key => $arr) {
             $pieces = explode("_", $key);
             if (isset($pieces[0]) && $pieces[0] == 'autoselect') {
                 $newKey = $pieces[1];
                 if (isset($_POST[$newKey . "_id"]) && $_POST[$newKey . "_id"] != "") {
                     $val = $_POST[$newKey . "_id"];
                 } else {
                     $field = Fields::model()->findByAttributes(array('fieldName' => $newKey));
                     if (isset($field)) {
                         $type = ucfirst($field->linkType);
                         if ($type != "Contacts") {
                             eval("\$lookupModel={$type}::model()->findByAttributes(array('name'=>'{$arr}'));");
                         } else {
                             $names = explode(" ", $arr);
                             if (count($names) > 1) {
                                 $lookupModel = Contacts::model()->findByAttributes(array('firstName' => $names[0], 'lastName' => $names[1]));
                             }
                         }
                         if (isset($lookupModel)) {
                             $val = $lookupModel->id;
                         } else {
                             $val = $arr;
                         }
                     }
                 }
                 $model->{$newKey} = $val;
             }
         }
         $temp = $model->attributes;
         foreach (array_keys($model->attributes) as $field) {
             if (isset($_POST['Templates'][$field])) {
                 $model->{$field} = $_POST['Templates'][$field];
                 $fieldData = Fields::model()->findByAttributes(array('modelName' => 'Templates', 'fieldName' => $field));
                 if ($fieldData->type == 'assignment' && $fieldData->linkType == 'multiple') {
                     $model->{$field} = Accounts::parseUsers($model->{$field});
                 } elseif ($fieldData->type == 'date') {
                     $model->{$field} = strtotime($model->{$field});
                 }
             }
         }
         parent::update($model, $temp, '0');
     }
     $this->render('update', array('model' => $model, 'users' => $users));
 }