Exemplo n.º 1
0
 /**
  * try to create a pdf with a linked task
  *
  */
 public function testLeadPdfLinkedTask()
 {
     // create lead + task + link
     $task = Tasks_Controller_Task::getInstance()->create($this->objects['linkedTask']);
     $lead = Crm_Controller_Lead::getInstance()->get($this->objects['leadWithLink']->getId());
     $lead->relations = array(array('own_model' => 'Crm_Model_Lead', 'own_backend' => 'Sql', 'own_id' => $lead->getId(), 'own_degree' => Tinebase_Model_Relation::DEGREE_SIBLING, 'related_model' => 'Tasks_Model_Task', 'related_backend' => Tasks_Backend_Factory::SQL, 'related_id' => $task->getId(), 'type' => 'TASK'));
     $lead = Crm_Controller_Lead::getInstance()->update($lead);
     $pdf = new Crm_Export_Pdf();
     $pdf->generate($lead);
     $pdfOutput = $pdf->render();
     //$pdf->save("test.pdf");
     $this->assertEquals(1, preg_match("/^%PDF-1.4/", $pdfOutput), "no pdf generated");
     $this->assertEquals(1, preg_match("/" . $task->summary . "/", $pdfOutput), "no summary found");
     // remove
     Tasks_Controller_Task::getInstance()->delete($task->getId());
     // purge all relations
     $backend = new Tinebase_Relation_Backend_Sql();
     $backend->purgeAllRelations('Crm_Model_Lead', 'Sql', $this->objects['leadWithLink']->getId());
 }
 /**
  * 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());
         }
     }
 }