/** * add auto tasks if config option is set and lead has responsible person * * @param Crm_Model_Lead $lead */ protected function _addLeadAutoTaskForResponsibles(Crm_Model_Lead $lead) { $responsibles = $lead->getResponsibles(); if (count($responsibles) === 0) { if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) { Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' No responsibles found'); } return; } $translate = Tinebase_Translation::getTranslation('Crm'); // create task (if current user has edit grant for other users default tasks container) $autoTask = new Tasks_Model_Task(array('summary' => $translate->_('Edit new lead'), 'due' => Tinebase_DateTime::now()->addHour(2), 'status' => 'IN-PROCESS', 'relations' => array(array('own_model' => 'Tasks_Model_Task', 'own_backend' => 'Sql', 'own_id' => 0, 'related_degree' => Tinebase_Model_Relation::DEGREE_SIBLING, 'type' => 'TASK', 'related_record' => $lead, 'related_id' => $lead->getId(), 'related_model' => 'Crm_Model_Lead', 'related_backend' => 'Sql')))); foreach ($responsibles as $responsible) { if ($responsible->type !== Addressbook_Model_Contact::CONTACTTYPE_USER) { if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) { Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Responsible is no user'); } continue; } try { $user = Tinebase_User::getInstance()->getUserByPropertyFromSqlBackend('accountId', $responsible->account_id); } catch (Tinebase_Exception_NotFound $tenf) { if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) { Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Could not find user'); } continue; } $autoTaskForResponsible = clone $autoTask; $responsibleContainer = Tinebase_Container::getInstance()->getDefaultContainer('Tasks_Model_Task', $user->getId(), 'defaultTaskList'); if (!$responsibleContainer) { if (Tinebase_Core::isLogLevel(Zend_Log::NOTICE)) { Tinebase_Core::getLogger()->notice(__METHOD__ . '::' . __LINE__ . ' Could not find default container of user with ADD grant'); } continue; } $autoTaskForResponsible->container_id = $responsibleContainer->getId(); $autoTaskForResponsible->organizer = $responsible->account_id; Tasks_Controller_Task::getInstance()->create($autoTaskForResponsible); if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) { Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Created auto task for user ' . $user->getId() . ' in container ' . $responsibleContainer->name); } } }
/** * returns recipients for a lead notification * * @param Crm_Model_Lead $_lead * @return Tinebase_Record_RecordSet of Addressbook_Model_Contact */ protected function _getNotificationRecipients(Crm_Model_Lead $_lead) { if (!$_lead->relations instanceof Tinebase_Record_RecordSet) { $_lead->relations = Tinebase_Relations::getInstance()->getRelations('Crm_Model_Lead', 'Sql', $_lead->getId(), true); } $recipients = $_lead->getResponsibles(); // if no responsibles are defined, send message to all readers of container if (count($recipients) === 0) { if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) { Tinebase_Core::getLogger()->debug(__CLASS__ . '::' . __METHOD__ . '::' . __LINE__ . ' no responsibles found for lead: ' . $_lead->getId() . ' sending notification to all people having read access to container ' . $_lead->container_id); } $containerGrants = Tinebase_Container::getInstance()->getGrantsOfContainer($_lead->container_id, TRUE); // NOTE: we just send notifications to users, not to groups or anyones! foreach ($containerGrants as $grant) { if ($grant['account_type'] == Tinebase_Acl_Rights::ACCOUNT_TYPE_USER && $grant[Tinebase_Model_Grants::GRANT_READ] == 1) { try { $contact = Addressbook_Controller_Contact::getInstance()->getContactByUserId($grant['account_id'], TRUE); $recipients->addRecord($contact); } catch (Addressbook_Exception_NotFound $aenf) { if (Tinebase_Core::isLogLevel(Zend_Log::NOTICE)) { Tinebase_Core::getLogger()->notice(__CLASS__ . '::' . __METHOD__ . '::' . __LINE__ . ' Do not send notification to non-existant user: ' . $aenf->getMessage()); } } } } } return $recipients; }