function updateOrCreateEmail($part = '', $opts, $cm = false) { // DB_DataObject::debugLevel(1); $template_name = preg_replace('/\\.[a-z]+$/i', '', basename($opts['file'])); if (!file_exists($opts['file'])) { $this->jerr("file does not exist : " . $opts['file']); } if (!empty($opts['master']) && !file_exists($opts['master'])) { $this->jerr("master file does not exist : " . $opts['master']); } if (empty($cm)) { $cm = DB_dataObject::factory('core_email'); $ret = $cm->get('name', $template_name); if ($ret && empty($opts['update'])) { $this->jerr("use --update 1 to update the template.."); } } $mailtext = file_get_contents($opts['file']); if (!empty($opts['master'])) { $body = $mailtext; $mailtext = file_get_contents($opts['master']); $mailtext = str_replace('{outputBody():h}', $body, $mailtext); } require_once 'Mail/mimeDecode.php'; require_once 'Mail/RFC822.php'; $decoder = new Mail_mimeDecode($mailtext); $parts = $decoder->getSendArray(); if (is_a($parts, 'PEAR_Error')) { echo $parts->toString() . "\n"; exit; } $headers = $parts[1]; $from = new Mail_RFC822(); $from_str = $from->parseAddressList($headers['From']); $from_name = trim($from_str[0]->personal, '"'); $from_email = $from_str[0]->mailbox . '@' . $from_str[0]->host; if ($cm->id) { $cc = clone $cm; $cm->setFrom(array('bodytext' => $parts[2], 'updated_dt' => date('Y-m-d H:i:s'))); $cm->update($cc); } else { $cm->setFrom(array('from_name' => $from_name, 'from_email' => $from_email, 'subject' => $headers['Subject'], 'name' => $template_name, 'bodytext' => $parts[2], 'updated_dt' => date('Y-m-d H:i:s'), 'created_dt' => date('Y-m-d H:i:s'))); $cm->insert(); } return $cm; }
/** * Delete linked records * @param $param1,$param2,...,$paramN strings foreign table names */ function deleteLinks() { $args = func_get_args(); foreach ($args as $tbl) { $this->say(__('Deleting foreign records in %s', array($tbl))); $do =& DB_dataObject::factory($tbl); $links = $do->links(); foreach ($links as $field => $link) { if (preg_match('`^' . $this->tableName() . ':.+$`', $link, $match)) { $do->{$field} = $this->pk(); break; } } while ($do->fetch()) { $do->delete(); $this->say(__('Deleting %s %s', array($do->tableName(), $do->id))); } } }