Exemplo n.º 1
0
 public function execute()
 {
     $person_name = $this->getInput()->getString('person_name');
     $personModel = new PeopleModel();
     $existingPerson = $personModel->checkPersonName($person_name);
     if ($existingPerson != "") {
         echo json_encode(array('success' => true, 'person_id' => $existingPerson, 'message' => ""));
     } else {
         echo json_encode(array('success' => true, 'message' => ucwords(TextHelper::_('COBALT_PERSON_WILL_BE_CREATED'))));
     }
 }
Exemplo n.º 2
0
 /**
  * Method to store a record
  *
  * @return boolean True on success
  */
 public function store($data = null, $returnRow = false)
 {
     //Load Tables
     $row = new DealTable();
     $oldRow = new DealTable();
     if ($data == null) {
         $data = $this->app->input->post->getArray();
     }
     //date generation
     $date = DateHelper::formatDBDate(date('Y-m-d H:i:s'));
     //assign the creation date
     if (!array_key_exists('id', $data) || array_key_exists('id', $data) && $data['id'] <= 0) {
         $data['created'] = $date;
         $status = "created";
         //assign the owner id
         $data['owner_id'] = array_key_exists('owner_id', $data) ? $data['owner_id'] : $this->app->getUser()->get('id');
     } else {
         $row->load($data['id']);
         $oldRow->load($data['id']);
         $status = "updated";
     }
     //update our modified date
     $data['modified'] = $date;
     //generate custom field string
     $customArray = array();
     foreach ($data as $name => $value) {
         if (strstr($name, 'custom_') && !strstr($name, '_input') && !strstr($name, "_hidden")) {
             $id = str_replace('custom_', '', $name);
             $customArray[] = array('custom_field_id' => $id, 'custom_field_value' => $value);
             unset($data[$name]);
         }
     }
     if (array_key_exists('expected_close', $data)) {
         if (strpos($data['expected_close'], '/')) {
             $dateParts = explode('/', $data['expected_close']);
             $expected_close = new \JDate(sprintf('%s-%s-%s 00:00:00', $dateParts[1], $dateParts[0], '20' . $dateParts[2]));
         } else {
             $expected_close = new \JDate($data['expected_close']);
         }
         $data['expected_close'] = $expected_close->toSql();
     }
     if (array_key_exists('company_name', $data) && $data['company_name'] != "" || array_key_exists('company', $data) && $data['company'] != "") {
         $company_name = array_key_exists('company_name', $data) ? $data['company_name'] : $data['company'];
         $companyModel = new Company();
         $existingCompany = $companyModel->checkCompanyName($company_name);
         if ($existingCompany == "") {
             $cdata = array();
             $cdata['name'] = $company_name;
             $data['company_id'] = $companyModel->store($cdata);
         } else {
             $data['company_id'] = $existingCompany;
         }
     }
     if (array_key_exists('company_id', $data) && is_array($data['company_id'])) {
         $company_name = $data['company_id']['value'];
         $companyModel = new Company();
         $existingCompany = $companyModel->checkCompanyName($company_name);
         if ($existingCompany == "") {
             $cdata = array();
             $cdata['name'] = $company_name;
             $data['company_id'] = $companyModel->store($cdata)->id;
         } else {
             $data['company_id'] = $existingCompany;
         }
     }
     //deal was closed
     $closedStages = $this->getClosedStages();
     if (array_key_exists('stage_id', $data) && in_array($data['stage_id'], $closedStages)) {
         $data['actual_close'] = $date;
     }
     /** check for and automatically associate and create primary contacts or people **/
     if (array_key_exists('person_name', $data) && $data['person_name'] != "") {
         $peopleModel = new People();
         $existingPerson = $peopleModel->checkPersonName($data['person_name']);
         if ($existingPerson == "") {
             $pdata = array();
             $name = explode(" ", $data['person_name']);
             $pdata['first_name'] = $name[0];
             $pdata['last_name'] = array_key_exists(1, $name) ? $name[1] : "";
             if (array_key_exists('company_id', $data)) {
                 $pdata['company_id'] = $data['company_id'];
             }
             $data['person_id'] = $peopleModel->store($pdata);
         } else {
             $data['person_id'] = $existingPerson;
         }
     }
     if (array_key_exists('primary_contact_name', $data) && $data['primary_contact_name'] != "") {
         $peopleModel = new People();
         $existingPerson = $peopleModel->checkPersonName($data['primary_contact_name']);
         if ($existingPerson == "") {
             $pdata = array();
             $name = explode(" ", $data['primary_contact_name']);
             $pdata['first_name'] = $name[0];
             $pdata['last_name'] = array_key_exists(1, $name) ? $name[1] : "";
             if (array_key_exists('company_id', $data)) {
                 $pdata['company_id'] = $data['company_id'];
             }
             $data['primary_contact_id'] = $peopleModel->store($pdata);
         } else {
             $data['primary_contact_id'] = $existingPerson;
         }
     }
     // Bind the form fields to the table
     if (!$row->bind($data)) {
         $this->setError($this->db->getErrorMsg());
         return false;
     }
     //$this->app->triggerEvent('onBeforeDealSave', array(&$row));
     // Make sure the record is valid
     if (!$row->check()) {
         $this->setError($this->db->getErrorMsg());
         return false;
     }
     // Store the web link table to the database
     if (!$row->store()) {
         $this->setError($this->db->getErrorMsg());
         return false;
     }
     $deal_id = array_key_exists('id', $data) && $data['id'] > 0 ? $data['id'] : $row->id;
     ActivityHelper::saveActivity($oldRow, $row, 'deal', $status);
     //if we receive no custom post data do not modify the custom fields
     if (count($customArray) > 0) {
         CobaltHelper::storeCustomCf($deal_id, $customArray, 'deal');
     }
     if (!empty($data['primary_contact_id']) || !empty($data['person_id'])) {
         $contactId = array_key_exists('primary_contact_id', $data) ? $data['primary_contact_id'] : $data['person_id'];
         $this->storeContact($deal_id, $contactId);
     }
     $closed_stages = DealHelper::getClosedStages();
     $row->closed = in_array($row->stage_id, $closed_stages) ? TRUE : FALSE;
     $row->actual_close_formatted = isset($row->actual_close) ? DateHelper::formatDate($row->actual_close) : DateHelper::formatDate(date("Y-m-d"));
     $row->expected_close_formatted = isset($row->expected_close) ? DateHelper::formatDate($row->expected_close) : DateHelper::formatDate(date("Y-m-d"));
     //$this->app->triggerEvent('onAfterDealSave', array(&$row));
     //return success
     if ($returnRow) {
         return $row;
     } else {
         return $deal_id;
     }
 }
Exemplo n.º 3
0
 /**
  * Method to store a record
  *
  * @return boolean True on success
  */
 public function store($data = null)
 {
     $app = \Cobalt\Container::fetch('app');
     //Load Tables
     $row = new NoteTable();
     $oldRow = new NoteTable();
     if ($data == null) {
         $data = $app->input->getArray(array('note' => 'string', 'deal_id' => 'int', 'person_id' => 'int', 'name' => 'string', 'category_id' => 'int', 'company_id' => 'int', 'note_id' => 'int', 'event_id' => 'int'));
     }
     if (array_key_exists('note_id', $data)) {
         $data['id'] = $data['note_id'];
     }
     if (array_key_exists('is_email', $data)) {
         $model = new Mail();
         $email = $model->getEmail($data['email_id']);
         $data['note'] = $email;
     }
     /** check for and automatically associate and create primary contacts or people **/
     if (array_key_exists('person_name', $data) && $data['person_name'] != "") {
         $peopleModel = new People();
         $existingPerson = $peopleModel->checkPersonName($data['person_name']);
         if ($existingPerson == "") {
             $pdata = array();
             $name = explode(" ", $data['person_name']);
             $pdata['first_name'] = array_key_exists(0, $name) ? $name[0] : "";
             $pdata['last_name'] = array_key_exists(1, $name) ? $name[1] : "";
             $data['person_id'] = $peopleModel->store($pdata);
         } else {
             $data['person_id'] = $existingPerson;
         }
     }
     /** check for and automatically associate and create deals **/
     if (array_key_exists('deal_name', $data) && $data['deal_name'] != "" && (!array_key_exists('deal_id', $data) || empty($data['deal_id']) || $data['deal_id'] == 0)) {
         $dealModel = new Deal();
         $existingDeal = $dealModel->checkDealName($data['deal_name']);
         if ($existingDeal == "") {
             $pdata = array();
             $pdata['name'] = $data['deal_name'];
             $data['deal_id'] = $dealModel->store($pdata);
         } else {
             $data['deal_id'] = $existingDeal;
         }
     }
     //date generation
     $date = DateHelper::formatDBDate(date('Y-m-d H:i:s'));
     if (!array_key_exists('id', $data)) {
         $data['created'] = $date;
         $status = "created";
     } else {
         $row->load($data['id']);
         $oldRow->load($data['id']);
         $status = "updated";
     }
     $data['modified'] = $date;
     $data['owner_id'] = UsersHelper::getUserId();
     // Bind the form fields to the table
     if (!$row->bind($data)) {
         $this->setError($this->db->getErrorMsg());
         return false;
     }
     //$app->triggerEvent('onBeforeNoteSave', array(&$row));
     // Make sure the record is valid
     if (!$row->check()) {
         $this->setError($this->db->getErrorMsg());
         return false;
     }
     // Store the web link table to the database
     if (!$row->store()) {
         $this->setError($this->db->getErrorMsg());
         return false;
     }
     if (array_key_exists('id', $data) && intval($data['id'])) {
         $id = $data['id'];
     } else {
         $id = $this->db->insertId();
     }
     ActivityHelper::saveActivity($oldRow, $row, 'note', $status);
     //Store email attachments
     if (array_key_exists('is_email', $data)) {
         $model = new CobaltModelMail();
         $model->storeAttachments($data['email_id'], $data['person_id']);
     }
     //$app->triggerEvent('onAfterNoteSave', array(&$row));
     return $id;
 }