/** * do conversions (transformations, charset, replacements ...) * * @param array $_data * @return array * * TODO think about moving this to import definition * TODO simplify crm/lead config handling for leadstate/source/type */ protected function _doConversions($_data) { $data = parent::_doConversions($_data); if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) { Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__ . ' ' . print_r($data, true)); } // adjust lead_name/leadstate/source/types if missing $configSettings = Crm_Controller::getInstance()->getConfigSettings()->toArray(); $requiredFields = array('leadstate_id' => 'leadstates', 'leadtype_id' => 'leadtypes', 'leadsource_id' => 'leadsources'); foreach ($requiredFields as $requiredField => $configKey) { if (!empty($data[$requiredField])) { continue; } switch ($requiredField) { default: // get default leadstate/source/type OR try to find it by name if given if (!isset($configSettings[$configKey])) { continue; } $settingField = preg_replace('/s$/', '', $configKey); if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) { Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__ . ' config settings' . print_r($configSettings[$configKey], true)); } // init with default $data[$requiredField] = isset($configSettings[$configKey][0]['id']) ? $configSettings[$configKey][0]['id'] : 1; foreach ($configSettings[$configKey] as $setting) { if (isset($setting[$settingField]) && isset($_data[$settingField]) && strtolower($setting[$settingField]) === strtolower($_data[$settingField])) { $data[$requiredField] = $setting['id']; } } } } return $data; }
/** * the singleton pattern * * @return Crm_Controller */ public static function getInstance() { if (self::$_instance === NULL) { self::$_instance = new Crm_Controller(); } return self::$_instance; }
/** * get record array * * @param Crm_Model_Lead $_lead lead data * @param Zend_Locale $_locale the locale * @param Zend_Translate $_translate * @return array the record * */ protected function getRecord(Crm_Model_Lead $_lead, Zend_Locale $_locale, Zend_Translate $_translate) { $settings = Crm_Controller::getInstance()->getConfigSettings(); $leadFields = array(array('label' => "", 'type' => 'separator'), array('label' => $_translate->_('Leadstate'), 'value' => array('leadstate_id')), array('label' => $_translate->_('Leadtype'), 'value' => array('leadtype_id')), array('label' => $_translate->_('Leadsource'), 'value' => array('leadsource_id')), array('label' => $_translate->_('Turnover'), 'value' => array('turnover')), array('label' => $_translate->_('Probability'), 'value' => array('probability')), array('label' => $_translate->_('Start'), 'value' => array('start')), array('label' => $_translate->_('End'), 'value' => array('end')), array('label' => $_translate->_('End Scheduled'), 'value' => array('end_scheduled'))); // add data to array $record = array(); foreach ($leadFields as $fieldArray) { if (!isset($fieldArray['type']) || $fieldArray['type'] !== 'separator') { $values = array(); foreach ($fieldArray['value'] as $valueFields) { $content = array(); if (is_array($valueFields)) { $keys = $valueFields; } else { $keys = array($valueFields); } foreach ($keys as $key) { if ($_lead->{$key} instanceof DateTime) { $content[] = Tinebase_Translation::dateToStringInTzAndLocaleFormat($_lead->{$key}, NULL, NULL, 'date'); } elseif (!empty($_lead->{$key})) { if ($key === 'turnover') { try { $content[] = Zend_Locale_Format::toNumber($_lead->{$key}, array('locale' => $_locale)) . " €"; } catch (Zend_Locale_Exception $zle) { Tinebase_Core::getLogger()->warn(__METHOD__ . '::' . __LINE__ . ' Could not convert turnover: ' . $zle->getMessage()); $content[] = 'NaN'; } } elseif ($key === 'probability') { $content[] = $_lead->{$key} . " %"; } elseif ($key === 'leadstate_id') { $state = $settings->getOptionById($_lead->leadstate_id, 'leadstates'); $content[] = $state['leadstate']; } elseif ($key === 'leadtype_id') { $type = $settings->getOptionById($_lead->leadtype_id, 'leadtypes'); $content[] = $type['leadtype']; } elseif ($key === 'leadsource_id') { $source = $settings->getOptionById($_lead->leadsource_id, 'leadsources'); $content[] = $source['leadsource']; } else { $content[] = $_lead->{$key}; } } } if (!empty($content)) { $glue = isset($fieldArray['glue']) ? $fieldArray['glue'] : " "; $values[] = implode($glue, $content); } } if (!empty($values)) { $record[] = array('label' => $fieldArray['label'], 'type' => isset($fieldArray['type']) ? $fieldArray['type'] : 'singleRow', 'value' => sizeof($values) === 1 ? $values[0] : $values); } } elseif (isset($fieldArray['type']) && $fieldArray['type'] === 'separator') { $record[] = $fieldArray; } } $record = $this->_addActivities($record, $_lead->notes); return $record; }
/** * add status to default persistent filter */ public function update_2() { $pfe = Tinebase_PersistentFilter::getInstance(); $defaultFavorite = $pfe->getByProperty(Crm_Preference::DEFAULTPERSISTENTFILTER_NAME, 'name'); $defaultFavorite->bypassFilters = TRUE; $closedStatus = Crm_Controller::getInstance()->getConfigSettings()->getEndedLeadstates(TRUE); $defaultFavorite->filters = array(array('field' => 'leadstate_id', 'operator' => 'notin', 'value' => $closedStatus)); $pfe->update($defaultFavorite); $this->setApplicationVersion('Crm', '3.3'); }
/** * init favorites */ protected function _initializeFavorites() { $pfe = Tinebase_PersistentFilter::getInstance(); $commonValues = array('account_id' => NULL, 'application_id' => Tinebase_Application::getInstance()->getApplicationByName('Crm')->getId(), 'model' => 'Crm_Model_LeadFilter'); $closedStatus = Crm_Controller::getInstance()->getConfigSettings()->getEndedLeadstates(TRUE); $pfe->createDuringSetup(new Tinebase_Model_PersistentFilter(array_merge($commonValues, array('name' => Crm_Preference::DEFAULTPERSISTENTFILTER_NAME, 'description' => "All leads I have read grants for", 'filters' => array(array('field' => 'leadstate_id', 'operator' => 'notin', 'value' => $closedStatus)))))); $pfe->createDuringSetup(new Tinebase_Model_PersistentFilter(array_merge($commonValues, array('name' => "Last modified by me", 'description' => "All leads that I have last modified", 'filters' => array(array('field' => 'last_modified_by', 'operator' => 'equals', 'value' => Tinebase_Model_User::CURRENTACCOUNT)))))); $pfe->createDuringSetup(new Tinebase_Model_PersistentFilter(array_merge($commonValues, array('name' => "My leads", 'description' => "All leads that I am responsible for", 'filters' => array(array('field' => 'contact', 'operator' => 'AND', 'value' => array(array('field' => 'id', 'operator' => 'equals', 'value' => Addressbook_Model_Contact::CURRENTCONTACT)))))))); $pfe->createDuringSetup(new Tinebase_Model_PersistentFilter(array_merge($commonValues, array('name' => "Leads with overdue tasks", 'description' => "Leads with overdue tasks", 'filters' => array(array('field' => 'task', 'operator' => 'AND', 'value' => array(array('field' => 'due', 'operator' => 'before', 'value' => 'dayThis')))))))); }
/** * get special field value * * @param Tinebase_Record_Interface $_record * @param array $_param * @param string $_key * @param string $_cellType * @param array $_resolvedRecords * @return string */ public static function getSpecialFieldValue(Tinebase_Record_Interface $_record, $_param, $_key = NULL, &$_cellType = NULL, $_resolvedRecords = NULL) { if (is_null($_key)) { throw new Tinebase_Exception_InvalidArgument('Missing required parameter $key'); } switch ($_param['type']) { case 'status': $value = $_record->getLeadStatus(); break; case 'source': $settings = Crm_Controller::getInstance()->getConfigSettings(); $source = $settings->getOptionById($_record->leadsource_id, 'leadsources'); if (isset($source['leadsource'])) { $value = $source['leadsource']; } else { Tinebase_Core::getLogger()->notice(__METHOD__ . '::' . __LINE__ . ' Leadsource id not found:' . $_record->leadsource_id); $value = ''; } break; case 'type': $settings = Crm_Controller::getInstance()->getConfigSettings(); $type = $settings->getOptionById($_record->leadtype_id, 'leadtypes'); if (isset($type['leadtype'])) { $value = $type['leadtype']; } else { Tinebase_Core::getLogger()->notice(__METHOD__ . '::' . __LINE__ . ' Leadtype id not found:' . $_record->leadtype_id); $value = ''; } break; case 'open_tasks': $value = 0; foreach ($_record->relations as $relation) { // check if is task and open if ($relation->type == 'TASK') { $idx = $_resolvedRecords['tasksStatus']->getIndexById($relation->related_record->status); if ($idx) { $status = $_resolvedRecords['tasksStatus'][$idx]; //if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' ' . print_r($status->toArray(), TRUE)); if ($status->is_open) { $value++; } } } } break; default: $value = ''; } return $value; }
/** * test get settings/config * * @return void */ public function testSaveSettings() { $oldSettings = $this->_instance->getSettings(); // change some settings $newSettings = $oldSettings; $newSettings['defaults']['leadstate_id'] = 2; $newSettings['leadsources'][] = array('id' => 5, 'leadsource' => 'Another Leadsource'); $anotherResult = $this->_instance->saveSettings($newSettings); $this->assertEquals($newSettings, $anotherResult, 'new settings have not been saved'); // reset original settings $result = $this->_instance->saveSettings($oldSettings); $this->assertEquals($result, $oldSettings, 'old settings have not been reset'); // test Crm_Model_Config::getOptionById $settings = Crm_Controller::getInstance()->getConfigSettings(); $this->assertEquals(array(), $settings->getOptionById(5, 'leadsources'), 'Crm_Model_Config::getOptionById failed'); }
/** * creates notification text and sends out notifications * * @todo: * - add changes to mail body * - find updater in addressbook to notify him * - add products? * - add notification levels * * @param Crm_Model_Lead $_lead * @param Tinebase_Model_FullUser $_updater * @param string $_action {created|changed} * @param Crm_Model_Lead $_oldLead * @return void */ protected function doSendNotifications(Crm_Model_Lead $_lead, Tinebase_Model_FullUser $_updater, $_action, $_oldLead = NULL) { $sendOnOwnActions = Tinebase_Core::getPreference('Crm')->getValue(Crm_Preference::SEND_NOTIFICATION_OF_OWN_ACTIONS); if (!$sendOnOwnActions) { Tinebase_Core::getLogger()->info(__METHOD__ . '::' . __LINE__ . ' Sending of Lead notifications disabled by user.'); return; } $view = new Zend_View(); $view->setScriptPath(dirname(__FILE__) . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'views'); $translate = Tinebase_Translation::getTranslation('Crm'); $view->updater = $_updater; $view->lead = $_lead; $settings = Crm_Controller::getInstance()->getConfigSettings(); $view->leadState = $settings->getOptionById($_lead->leadstate_id, 'leadstates'); $view->leadType = $settings->getOptionById($_lead->leadtype_id, 'leadtypes'); $view->leadSource = $settings->getOptionById($_lead->leadsource_id, 'leadsources'); $view->container = Tinebase_Container::getInstance()->getContainerById($_lead->container_id); if (isset($_lead->relations)) { $customer = $_lead->relations->filter('type', 'CUSTOMER')->getFirstRecord(); if ($customer) { $view->customer = $customer->related_record->n_fn; if (isset($customer->related_record->org_name)) { $view->customer .= ' (' . $customer->related_record->org_name . ')'; } } } if ($_lead->start instanceof DateTime) { $view->start = Tinebase_Translation::dateToStringInTzAndLocaleFormat($_lead->start, NULL, NULL, 'date'); } else { $view->start = '-'; } if ($_lead->end instanceof DateTime) { $view->leadEnd = Tinebase_Translation::dateToStringInTzAndLocaleFormat($_lead->end, NULL, NULL, 'date'); } else { $view->leadEnd = '-'; } if ($_lead->end_scheduled instanceof DateTime) { $view->ScheduledEnd = Tinebase_Translation::dateToStringInTzAndLocaleFormat($_lead->end_scheduled, NULL, NULL, 'date'); } else { $view->ScheduledEnd = '-'; } $view->lang_customer = $translate->_('Customer'); $view->lang_state = $translate->_('State'); $view->lang_type = $translate->_('Role'); $view->lang_source = $translate->_('Source'); $view->lang_start = $translate->_('Start'); $view->lang_scheduledEnd = $translate->_('Scheduled end'); $view->lang_end = $translate->_('End'); $view->lang_turnover = $translate->_('Turnover'); $view->lang_probability = $translate->_('Probability'); $view->lang_folder = $translate->_('Folder'); $view->lang_updatedBy = $translate->_('Updated by'); $view->lang_updatedFields = $translate->_('Updated Fields:'); $view->lang_updatedFieldMsg = $translate->_('%s changed from %s to %s.'); $plain = $view->render('newLeadPlain.php'); $html = $view->render('newLeadHtml.php'); if ($_action == 'changed') { $subject = sprintf($translate->_('Lead %s has been changed'), $_lead->lead_name); } else { if ($_action == 'deleted') { $subject = sprintf($translate->_('Lead %s has been deleted'), $_lead->lead_name); } else { $subject = sprintf($translate->_('Lead %s has been created'), $_lead->lead_name); } } // create pdf try { $pdfGenerator = new Crm_Export_Pdf(); $pdfGenerator->generate($_lead); $attachment = array('rawdata' => $pdfGenerator->render(), 'filename' => $_lead->lead_name . '.pdf'); } catch (Zend_Pdf_Exception $e) { Tinebase_Core::getLogger()->warn(__METHOD__ . '::' . __LINE__ . ' error creating pdf: ' . $e->__toString()); $attachment = NULL; } $recipients = $this->_getNotificationRecipients($_lead); // send notification to updater in any case! if (!in_array($_updater->contact_id, $recipients->getArrayOfIds())) { $updaterContact = Addressbook_Controller_Contact::getInstance()->get($_updater->contact_id); $recipients->addRecord($updaterContact); } if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) { Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . $plain); } try { Tinebase_Notification::getInstance()->send(Tinebase_Core::getUser(), $recipients, $subject, $plain, $html, array($attachment)); } catch (Exception $e) { Tinebase_Core::getLogger()->warn(__CLASS__ . '::' . __METHOD__ . '::' . __LINE__ . ' ' . $e->getMessage()); if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) { Tinebase_Core::getLogger()->trace(__CLASS__ . '::' . __METHOD__ . '::' . __LINE__ . ' ' . $e->getTraceAsString()); } } }
/** * (non-PHPdoc) * @see Tinebase_Backend_Sql_Abstract::_appendForeignSort() * * @todo generalize this: find a place (in model config?) for foreign record sorting information * @todo maybe we can use a temp table with joins here * @todo allow to to use it with keyfields, too (and/or switch those settings to keyfield configs) */ protected function _appendForeignSort(Tinebase_Model_Pagination $pagination, Zend_Db_Select $select) { $virtualSortColumns = array('leadstate_id' => Crm_Model_Config::LEADSTATES, 'leadsource_id' => Crm_Model_Config::LEADSOURCES, 'leadtype_id' => Crm_Model_Config::LEADTYPES); $col = $pagination->sort; if (isset($virtualSortColumns[$col])) { $settings = Crm_Controller::getInstance()->getConfigSettings(); $setting = $settings->{$virtualSortColumns[$col]}; // create cases (when => then) for sql switch (CASE) command $cases = array(); foreach ($setting as $settingRecord) { $cases[$settingRecord['id']] = $settingRecord[str_replace('_id', '', $col)]; } $foreignSortCase = $this->_dbCommand->getSwitch($col, $cases); $select->columns(array('foreignSortCol' => $foreignSortCase)); $pagination->sort = 'foreignSortCol'; } }
/** * creates/updates settings * * @return array created/updated settings */ public function saveSettings($recordData) { $settings = new Crm_Model_Config($recordData); $result = Crm_Controller::getInstance()->saveConfigSettings($settings)->toArray(); return $result; }