From: <gem...@li...> - 2011-09-16 15:15:03
|
Revision: 32 http://gemstracker.svn.sourceforge.net/gemstracker/?rev=32&view=rev Author: matijsdejong Date: 2011-09-16 15:14:56 +0000 (Fri, 16 Sep 2011) Log Message: ----------- - sorted function names TemplateMailer.php - extended existing logging with template id & from address Modified Paths: -------------- trunk/library/classes/Gems/Email/EmailFormAbstract.php trunk/library/classes/Gems/Email/TemplateMailer.php trunk/library/classes/GemsEscort.php trunk/library/configs/db/patches.sql trunk/library/configs/db/tables/gems__respondent_communications.60.sql Modified: trunk/library/classes/Gems/Email/EmailFormAbstract.php =================================================================== --- trunk/library/classes/Gems/Email/EmailFormAbstract.php 2011-09-16 08:28:47 UTC (rev 31) +++ trunk/library/classes/Gems/Email/EmailFormAbstract.php 2011-09-16 15:14:56 UTC (rev 32) @@ -486,6 +486,7 @@ $this->mailer->setMethod($this->getValue('multi_method')); $this->mailer->setSubject($this->getValue('gmt_subject')); $this->mailer->setBody($this->getValue('gmt_body')); + $this->mailer->setTemplateId($this->getValue('select_subject')); $result = $this->mailer->process($this->getTokensData()); Modified: trunk/library/classes/Gems/Email/TemplateMailer.php =================================================================== --- trunk/library/classes/Gems/Email/TemplateMailer.php 2011-09-16 08:28:47 UTC (rev 31) +++ trunk/library/classes/Gems/Email/TemplateMailer.php 2011-09-16 15:14:56 UTC (rev 32) @@ -66,6 +66,7 @@ private $_subject = null; private $_body = null; private $_method = 'M'; + private $_templateId = null; // Not used for lookup private $_tokenData = array(); private $_verbose = false; @@ -86,11 +87,45 @@ return $this; } + /** + * Replaces fields with their values + * @param string $value + * @return string + */ + public function applyFields($value) + { + if (! $this->_mailFields) { + $this->getTokenMailFields(); + } + if (! $this->_mailKeys) { + $this->_mailKeys = array_keys($this->_mailFields); + } + + return str_replace($this->_mailKeys, $this->_mailFields, $value); + } + + /** + * Returns true if the "email.bounce" setting exists in the project + * configuration and is true + * @return boolean + */ + public function bounceCheck() + { + return isset($this->escort->project->email['bounce']) && $this->escort->project->email['bounce']; + } + + /** + * Returns Zend_Mail_Transport_Abstract when something else than the default mail protocol should be used. + * + * @staticvar array $mailServers + * @param email address $from + * @return Zend_Mail_Transport_Abstract or null + */ public function checkTransport($from) { static $mailServers = array(); - if (!array_key_exists($from, $mailServers)) { + if (! array_key_exists($from, $mailServers)) { $sql = 'SELECT * FROM gems__mail_servers WHERE ? LIKE gms_from ORDER BY LENGTH(gms_from) DESC LIMIT 1'; // Always set cache, se we know when not to check for this row. @@ -138,31 +173,13 @@ return $this->messages; } - /** - * Returns true if the "email.bounce" setting exists in the project - * configuration and is true - * @return boolean - */ - public function bounceCheck() + public function getTokenMailFields() { - return isset($this->escort->project->email['bounce']) && $this->escort->project->email['bounce']; - } - - /** - * Replaces fields with their values - * @param string $value - * @return string - */ - public function applyFields($value) - { if (! $this->_mailFields) { - $this->getTokenMailFields(); + $this->_mailFields = $this->escort->tokenMailFields($this->_tokenData); } - if (! $this->_mailKeys) { - $this->_mailKeys = array_keys($this->_mailFields); - } - return str_replace($this->_mailKeys, $this->_mailFields, $value); + return $this->_mailFields; } /** @@ -184,68 +201,6 @@ } /** - * Sets verbose (noisy) operation - * @param boolean $verbose - */ - public function setVerbose($verbose) - { - $this->_verbose = $verbose; - } - - /** - * Sets sender (regular e-mail address) or one of: - * 'O' - Uses the contact information of the selected organization - * 'S' - Uses the site-wide contact information - * 'U' - Uses the contact information of the currently logged in user - * - * @param string $from - */ - public function setFrom($from) - { - $this->_from = $from; - } - - /** - * Sets a list of tokens - * @param string[] $tokens - */ - public function setTokens(array $tokens) - { - $this->_tokens = $tokens; - } - - /** - * Sets the sending method to use - * 'M' - Send multiple mails per respondent, one for each checked token. - * 'O' - Send one mail per respondent, mark all checked tokens as send. - * 'A' - Send one mail per respondent, mark only mailed tokens as send. - * - * @param string $method - */ - public function setMethod($method) - { - $this->_method = $method; - } - - /** - * Sets the subject of the mail - * @param string $subject - */ - public function setSubject($subject) - { - $this->_subject = $subject; - } - - /** - * Sets the body of the mail - * @param string $body - */ - public function setBody($body) - { - $this->_body = $body; - } - - /** * Processes an array of token data and sends e-mails * @param array $tokensData * @return boolean @@ -279,7 +234,10 @@ $ucount = 0; foreach ($tokensData as $tokenData) { + // Should this token be mailed? if (in_array($tokenData['gto_id_token'], $this->_tokens)) { + + // Should all tokens be mailed or is this the first? if ($mailAll || (! isset($send[$tokenData['grs_email']]))) { if ($message = $this->processMail($tokenData)) { @@ -347,7 +305,7 @@ if ($message = $this->sendMail($to, $to_name, $from, $from_name, $tokenData)) { return $message; } else { - $this->updateToken($tokenData); + $this->updateToken($tokenData, $to, $from); return false; } } @@ -395,59 +353,67 @@ try { $mail->send($this->checkTransport($from)); - return false; + $result = false; } catch (Exception $e) { - return $e->getMessage(); + $result = $e->getMessage(); + + // Log to error file + $this->escort->logger->logError($e, $this->escort->request); } + + return $result; } /** - * Updates a token - * @param array $tokenData - * @param string $subject + * Sets the body of the mail + * @param string $body */ - protected function updateToken(array $tokenData, $subject = null) + public function setBody($body) { - if (null === $subject) { - $subject = $this->_mailSubject; - } else { - $this->_mailSubject = $subject; - } + $this->_body = $body; + } - if (null === $this->_changeDate) { - $this->_changeDate = new Zend_Db_Expr('CURRENT_TIMESTAMP'); - } + /** + * Sets sender (regular e-mail address) or one of: + * 'O' - Uses the contact information of the selected organization + * 'S' - Uses the site-wide contact information + * 'U' - Uses the contact information of the currently logged in user + * + * @param string $from + */ + public function setFrom($from) + { + $this->_from = $from; + } - $db = $this->escort->db; - $uid = $this->escort->session->user_id; + /** + * Sets the sending method to use + * 'M' - Send multiple mails per respondent, one for each checked token. + * 'O' - Send one mail per respondent, mark all checked tokens as send. + * 'A' - Send one mail per respondent, mark only mailed tokens as send. + * + * @param string $method + */ + public function setMethod($method) + { + $this->_method = $method; + } - $tdata['gto_mail_sent_date'] = $this->_mailDate; - - $db->update('gems__tokens', $tdata, $db->quoteInto('gto_id_token = ?', $tokenData['gto_id_token'])); - - $cdata['grco_id_to'] = $tokenData['grs_id_user']; - $cdata['grco_id_by'] = $uid; - $cdata['grco_organization'] = $tokenData['gor_id_organization']; - $cdata['grco_id_token'] = $tokenData['gto_id_token']; - $cdata['grco_method'] = 'email'; - $cdata['grco_topic'] = substr($subject, 0, 120); - $cdata['grco_address'] = substr($tokenData['grs_email'], 0, 120); - $cdata['grco_changed'] = $this->_changeDate; - $cdata['grco_changed_by'] = $uid; - $cdata['grco_created'] = $this->_changeDate; - $cdata['grco_created_by'] = $uid; - - $db->insert('gems__respondent_communications', $cdata); + /** + * Sets the subject of the mail + * @param string $subject + */ + public function setSubject($subject) + { + $this->_subject = $subject; } - public function getTokenMailFields() + public function setTemplateId($templatedId) { - if (! $this->_mailFields) { - $this->_mailFields = $this->escort->tokenMailFields($this->_tokenData); - } + $this->_templateId = $templatedId; - return $this->_mailFields; + return $this; } public function setTokenData(array $tokenData) @@ -470,4 +436,65 @@ return $this->_mailFields; } + + /** + * Sets a list of tokens + * @param string[] $tokens + */ + public function setTokens(array $tokens) + { + $this->_tokens = $tokens; + } + + /** + * Sets verbose (noisy) operation + * + * @param boolean $verbose + */ + public function setVerbose($verbose) + { + $this->_verbose = $verbose; + } + + /** + * Updates a token and log's the communication + * + * @param array $tokenData + * @param string $to Optional, if available the communication is logged. + * @param string $from Optional + */ + protected function updateToken(array $tokenData, $to = null, $from = null) + { + if (null === $this->_changeDate) { + $this->_changeDate = new Zend_Db_Expr('CURRENT_TIMESTAMP'); + } + + $db = $this->escort->db; + $uid = $this->escort->getCurrentUserId(); + + $tdata['gto_mail_sent_date'] = $this->_mailDate; + + $db->update('gems__tokens', $tdata, $db->quoteInto('gto_id_token = ?', $tokenData['gto_id_token'])); + + if ($to) { + $cdata['grco_id_to'] = $tokenData['grs_id_user']; + $cdata['grco_id_by'] = $uid; + $cdata['grco_organization'] = $tokenData['gor_id_organization']; + $cdata['grco_id_token'] = $tokenData['gto_id_token']; + + $cdata['grco_method'] = 'email'; + $cdata['grco_topic'] = substr($this->_mailSubject, 0, 120); + $cdata['grco_address'] = substr($to, 0, 120); + $cdata['grco_sender'] = substr($from, 0, 120); + + $cdata['grco_id_message'] = $this->_templateId; + + $cdata['grco_changed'] = $this->_changeDate; + $cdata['grco_changed_by'] = $uid; + $cdata['grco_created'] = $this->_changeDate; + $cdata['grco_created_by'] = $uid; + + $db->insert('gems__respondent_communications', $cdata); + } + } } Modified: trunk/library/classes/GemsEscort.php =================================================================== --- trunk/library/classes/GemsEscort.php 2011-09-16 08:28:47 UTC (rev 31) +++ trunk/library/classes/GemsEscort.php 2011-09-16 15:14:56 UTC (rev 32) @@ -1090,6 +1090,10 @@ return $allowedOrganizations; } + /** + * + * @return int The current active organization id or 0 when not known + */ public function getCurrentOrganization() { if ($this instanceof Gems_Project_Organization_MultiOrganizationInterface) { @@ -1100,9 +1104,26 @@ return $this->getRespondentOrganization(); } - return $this->session->user_organization_id; + if (isset($this->session->user_organization_id)) { + return $this->session->user_organization_id; + } else { + return 0; + } } + /** + * + * @return int The current user id or 0 when not known. + */ + public function getCurrentUserId() + { + if (isset($this->session->user_id)) { + return $this->session->user_id; + } else { + return 0; + } + } + public function getDatabasePaths() { $path = APPLICATION_PATH . '/configs/db'; Modified: trunk/library/configs/db/patches.sql =================================================================== --- trunk/library/configs/db/patches.sql 2011-09-16 08:28:47 UTC (rev 31) +++ trunk/library/configs/db/patches.sql 2011-09-16 15:14:56 UTC (rev 32) @@ -190,3 +190,6 @@ -- PATCH: Organization codes ALTER TABLE `gems__organizations` ADD gor_code varchar(20) CHARACTER SET 'utf8' COLLATE 'utf8_general_ci' AFTER gor_name; +-- PATCH: Extra log +ALTER TABLE gems__respondent_communications ADD grco_sender varchar(120) CHARACTER SET 'utf8' COLLATE 'utf8_general_ci' null AFTER grco_address; +ALTER TABLE gems__respondent_communications ADD grco_id_message bigint unsigned null references gems__mail_templates (gmt_id_message) AFTER grco_comments; Modified: trunk/library/configs/db/tables/gems__respondent_communications.60.sql =================================================================== --- trunk/library/configs/db/tables/gems__respondent_communications.60.sql 2011-09-16 08:28:47 UTC (rev 31) +++ trunk/library/configs/db/tables/gems__respondent_communications.60.sql 2011-09-16 15:14:56 UTC (rev 32) @@ -11,8 +11,11 @@ grco_method varchar(12) CHARACTER SET 'utf8' COLLATE 'utf8_general_ci' not null, grco_topic varchar(120) CHARACTER SET 'utf8' COLLATE 'utf8_general_ci' not null, grco_address varchar(120) CHARACTER SET 'utf8' COLLATE 'utf8_general_ci' null, + grco_sender varchar(120) CHARACTER SET 'utf8' COLLATE 'utf8_general_ci' null, grco_comments varchar(120) CHARACTER SET 'utf8' COLLATE 'utf8_general_ci' null, + grco_id_message bigint unsigned null references gems__mail_templates (gmt_id_message), + grco_changed timestamp not null default current_timestamp, grco_changed_by bigint unsigned not null, grco_created timestamp not null, This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |