/** * 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; } }
/** * Method to store a record * * @return boolean True on success */ public function store($data = null) { //Load Tables $row = new PeopleTable(); $oldRow = new PeopleTable(); if ($data == null) { $data = $this->app->input->getArray(array('id' => 'int', 'first_name' => 'string', 'last_name' => 'string', 'company' => 'string', 'company_id' => 'int', 'position' => 'string', 'phone' => 'string', 'email' => 'email', 'source_id' => 'int', 'status_id' => 'int', 'deal_id' => 'int', 'type' => 'string', 'home_address_1' => 'string', 'home_address_2' => 'string', 'home_city' => 'string', 'home_state' => 'string', 'home_zip' => 'string', 'home_country' => 'string', 'work_address_1' => 'string', 'work_address_2' => 'string', 'work_city' => 'string', 'work_country' => 'string', 'work_state' => 'string', 'work_zip' => 'string', 'assignee_name' => 'string', 'assignee_id' => 'int', 'assignment_note' => 'string', 'mobile_phone' => 'string', 'home_email' => 'email', 'other_email' => 'email', 'home_phone' => 'string', 'fax' => 'string', 'website' => 'string', 'facebook_url' => 'string', 'twitter_user' => 'string', 'linkedin_url' => 'string', 'aim' => 'string')); } //date generation $date = DateHelper::formatDBDate(date('Y-m-d H:i:s')); if (!array_key_exists('id', $data) || array_key_exists('id', $data) && $data['id'] <= 0) { $data['created'] = $date; $data['owner_id'] = array_key_exists('owner_id', $data) ? $data['owner_id'] : UsersHelper::getUserId(); $status = "created"; } else { $row->load($data['id']); $oldRow->load($data['id']); $status = "updated"; } $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('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; } } /** retrieving joomla user id **/ if (array_key_exists('email', $data)) { $data['id'] = self::associateJoomlaUser($data['email']); } // Bind the form fields to the table if (!$row->bind($data)) { $this->setError($this->db->getErrorMsg()); return false; } $app = \Cobalt\Container::fetch('app'); //$app->triggerEvent('onBeforePersonSave', 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; } $person_id = isset($data['id']) ? $data['id'] : $this->db->insertId(); /** Updating the joomla user **/ if (array_key_exists('id', $data) && intval($data['id']) && array_key_exists('email', $data) && array_key_exists('first_name', $data) && array_key_exists('last_name', $data)) { self::updateJoomlaUser($data); } ActivityHelper::saveActivity($oldRow, $row, 'person', $status); //if we receive no custom post data do not modify the custom fields if (count($customArray) > 0) { CobaltHelper::storeCustomCf($person_id, $customArray, 'people'); } //bind to cf tables for deal & person association if (isset($data['deal_id']) && $data['deal_id']) { $deal = array('association_id = ' . $data['deal_id'], 'association_type="deal"', 'person_id = ' . $row->id, "created = '{$date}'"); if (!$this->dealsPeople($deal)) { return false; } } //Pass Status to plugin & form ID if available $row->status = $status; if (isset($data) && is_array($data) && array_key_exists('form_id', $data)) { $row->form_id = $data['form_id']; } else { $row->form_id = ''; } //$app->triggerEvent('onAfterPersonSave', array(&$row)); return $person_id; }