Development

/branches/1.0/lib/view/sfMailView.class.php

You must first sign up to be able to contribute.

root/branches/1.0/lib/view/sfMailView.class.php

Revision 8288, 4.0 kB (checked in by noel, 7 years ago)

added ignore_version_control() for every call to a finder (related to #2795)

  • Property svn:mime-type set to text/x-php
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Rev Date
Line 
1 <?php
2
3 /*
4  * This file is part of the symfony package.
5  * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
6  *
7  * For the full copyright and license information, please view the LICENSE
8  * file that was distributed with this source code.
9  */
10
11 /**
12  *
13  * @package    symfony
14  * @subpackage view
15  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
16  * @version    SVN: $Id$
17  */
18 class sfMailView extends sfPHPView
19 {
20   /**
21    * Retrieves the template engine associated with this view.
22    *
23    * @return string sfMail
24    */
25   public function getEngine()
26   {
27     return 'sfMail';
28   }
29
30   /**
31    * Configures template for this view.
32    *
33    * @throws <b>sfActionException</b> If the configure fails
34    */
35   public function configure()
36   {
37     // view.yml configure
38     parent::configure();
39
40     // require our configuration
41     $moduleName = $this->moduleName;
42     require(sfConfigCache::getInstance()->checkConfig(sfConfig::get('sf_app_module_dir_name').'/'.$this->moduleName.'/'.sfConfig::get('sf_app_module_config_dir_name').'/mailer.yml'));
43   }
44
45   /**
46    * Renders the presentation and send the email to the client.
47    *
48    * @return mixed Raw data of the mail
49    */
50   public function render($templateVars = null)
51   {
52     $template         = $this->getDirectory().'/'.$this->getTemplate();
53     $actionStackEntry = $this->getContext()->getActionStack()->getLastEntry();
54     $actionInstance   = $actionStackEntry->getActionInstance();
55
56     $retval = null;
57
58     // execute pre-render check
59     $this->preRenderCheck();
60
61     if (sfConfig::get('sf_logging_enabled'))
62     {
63       $this->getContext()->getLogger()->info('{sfView} render "'.$template.'"');
64     }
65
66     // get sfMail object from action
67     $mail = $actionInstance->getVarHolder()->get('mail');
68     if (!$mail)
69     {
70       $error = 'You must define a sfMail object named $mail ($this->mail) in your action to be able to use a sfMailView.';
71       throw new sfActionException($error);
72     }
73
74     // assigns some variables to the template
75     $this->attributeHolder->add($this->getGlobalVars());
76     $this->attributeHolder->add($actionInstance->getVarHolder()->getAll());
77
78     // render main template
79     $retval = $this->renderFile($template);
80
81     // render main and alternate templates
82     $all_template_dir  = dirname($template);
83     $all_template_regex = preg_replace('/\\.php$/', '\..+\.php', basename($template));
84     $all_templates = sfFinder::type('file')->ignore_version_control()->name('/^'.$all_template_regex.'$/')->in($all_template_dir);
85     $all_retvals = array();
86     foreach ($all_templates as $templateFile)
87     {
88       if (preg_match('/\.([^.]+?)\.php$/', $templateFile, $matches))
89       {
90         $all_retvals[$matches[1]] = $this->renderFile($templateFile);
91       }
92     }
93
94     // send email
95     if (sfConfig::get('sf_logging_enabled'))
96     {
97       $this->getContext()->getLogger()->info('{sfView} send email to client');
98     }
99
100     // configuration prefix
101     $config_prefix = 'sf_mailer_'.strtolower($this->moduleName).'_';
102
103     $vars = array(
104       'mailer',
105       'priority', 'content_type', 'charset', 'encoding', 'wordwrap',
106       'hostname', 'port', 'domain', 'username', 'password'
107     );
108     $defaultMail = new sfMail();
109
110     foreach ($vars as $var)
111     {
112       $setter = 'set'.sfInflector::camelize($var);
113       $getter = 'get'.sfInflector::camelize($var);
114       $value  = $mail->$getter() !== $defaultMail->$getter() ? $mail->$getter() : sfConfig::get($config_prefix.strtolower($var));
115       $mail->$setter($value);
116     }
117
118     $mail->setBody($retval);
119
120     // alternate bodies
121     $i = 0;
122     foreach ($all_retvals as $type => $retval)
123     {
124       if ($type == 'altbody' && !$mail->getAltBody())
125       {
126         $mail->setAltBody($retval);
127       }
128       else
129       {
130         ++$i;
131         $mail->addStringAttachment($retval, 'file'.$i, 'base64', 'text/'.$type);
132       }
133     }
134
135     // preparing email to be sent
136     $mail->prepare();
137
138     // send e-mail
139     if (sfConfig::get($config_prefix.'deliver'))
140     {
141       $mail->send();
142     }
143
144     return $mail->getRawHeader().$mail->getRawBody();
145   }
146 }
147
Note: See TracBrowser for help on using the browser.