<commit_before><?php

/**
 * An improved and more pleasant base Email class to use on your project
 *
 * - URL safe rewriting
 * - Base template
 * - Send email according to member locale
 * - Auto set template based on ClassName for subclasses
 * - Basic theme options
 * - Check for subject
 * - Send to member
 *
 * @author lekoala
 */
class MandrillEmail extends Email
{

    /**
     * @var ViewableData
     */
    protected $template_data;
    protected $ss_template = "email/BasicEmail"; // This will be overriden by default_template config if set
    protected $original_body;
    protected $locale;
    protected $callout;
    protected $sidebar;
    protected $image;
    protected $userDefinedSubject = false;

    /**
     *
     * @var Member
     */
    protected $to_member;

    /**
     *
     * @var Member
     */
    protected $from_member;
    protected $parse_body = false;
    protected $required_objects = array();
    protected $theme = null;
    protected $header_color;
    protected $header_font_color;
    protected $footer_color;
    protected $footer_font_color;
    protected $panel_color;
    protected $panel_border_color;
    protected $panel_font_color;
    protected $btn_color;
    protected $btn_border_color;
    protected $btn_font_color;

    public function __construct($from = null, $to = null, $subject = null, $body = null, $bounceHandlerURL = null, $cc = null, $bcc = null)
    {
        parent::__construct($from, $to, $subject, $body, $bounceHandlerURL, $cc, $bcc);

        // Use config template
        if ($defaultTemplate = self::config()->default_template) {
            $this->setTemplate($defaultTemplate);
        }

        // Allow subclass template
        $class = get_called_class();
        if ($class != 'MandrillEmail') {
            $this->ss_template = array('email/' . $class, $this->ss_template);
        }

        // Allow user configurable theming
        $config = SiteConfig::current_site_config();
        if ($config->hasMethod('ConfigurableEmailTheme')) {
            $this->theme = 'configurable_theme';
            $this->setThemeOptions($config->ConfigurableEmailTheme());
        } elseif ($config->EmailTheme) {
            $this->setTheme($config->EmailTheme);
        } elseif ($theme = self::config()->default_theme) {
            $this->setTheme($theme);
        }
    }

    /**
     * Determine if email is using configurable theme
     * 
     * @return bool
     */
    public function hasConfigurableTheme()
    {
        return $this->theme == 'configurable_theme';
    }

    /**
     * Send an email with HTML content.
     *
     * @see sendPlain() for sending plaintext emails only.
     * @uses Mailer->sendHTML()
     *
     * @param string $messageID Optional message ID so the message can be identified in bounces etc.
     * @return bool Success of the sending operation from an MTA perspective.
     * Doesn't actually give any indication if the mail has been delivered to the recipient properly)
     */
    public function send($messageID = null)
    {
        // Check required objects
        if ($this->required_objects) {
            foreach ($this->required_objects as $reqName => $reqClass) {
                if ($reqName == 'Member' && !$this->templateData()->$reqName) {
                    $this->templateData()->$reqName = Member::currentUser();
                }
                if ($reqName == 'SiteConfig' && !$this->templateData()->$reqName) {
                    $this->templateData()->$reqName = SiteConfig::current_site_config();
                }
                if (!$this->templateData()->$reqName) {
                    throw new Exception('Required object ' . $reqName . ' of class ' . $reqClass . ' is not defined in template data');
                }
            }
        }

        // Check for Subject
        if (!$this->subject) {
            throw new Exception('You must set a subject');
        }

        $this->from = MandrillMailer::resolveDefaultFromEmail($this->from);
        if (!$this->from) {
            throw new Exception('You must set a sender');
        }
        if ($this->to_member && !$this->to) {
            // Include name in to as standard rfc<commit_msg>make throwing exceptions configurable<commit_after><?php

/**
 * An improved and more pleasant base Email class to use on your project
 *
 * - URL safe rewriting
 * - Base template
 * - Send email according to member locale
 * - Auto set template based on ClassName for subclasses
 * - Basic theme options
 * - Check for subject
 * - Send to member
 *
 * @author lekoala
 */
class MandrillEmail extends Email
{

    private static $required_objects_throw_exceptions = false;

    /**
     * @var ViewableData
     */
    protected $template_data;
    protected $ss_template = "email/BasicEmail"; // This will be overriden by default_template config if set
    protected $original_body;
    protected $locale;
    protected $callout;
    protected $sidebar;
    protected $image;
    protected $userDefinedSubject = false;

    /**
     *
     * @var Member
     */
    protected $to_member;

    /**
     *
     * @var Member
     */
    protected $from_member;
    protected $parse_body = false;
    protected $required_objects = array();
    protected $theme = null;
    protected $header_color;
    protected $header_font_color;
    protected $footer_color;
    protected $footer_font_color;
    protected $panel_color;
    protected $panel_border_color;
    protected $panel_font_color;
    protected $btn_color;
    protected $btn_border_color;
    protected $btn_font_color;

    public function __construct($from = null, $to = null, $subject = null, $body = null, $bounceHandlerURL = null, $cc = null, $bcc = null)
    {
        parent::__construct($from, $to, $subject, $body, $bounceHandlerURL, $cc, $bcc);

        // Use config template
        if ($defaultTemplate = self::config()->default_template) {
            $this->setTemplate($defaultTemplate);
        }

        // Allow subclass template
        $class = get_called_class();
        if ($class != 'MandrillEmail') {
            $this->ss_template = array('email/' . $class, $this->ss_template);
        }

        // Allow user configurable theming
        $config = SiteConfig::current_site_config();
        if ($config->hasMethod('ConfigurableEmailTheme')) {
            $this->theme = 'configurable_theme';
            $this->setThemeOptions($config->ConfigurableEmailTheme());
        } elseif ($config->EmailTheme) {
            $this->setTheme($config->EmailTheme);
        } elseif ($theme = self::config()->default_theme) {
            $this->setTheme($theme);
        }
    }

    /**
     * Determine if email is using configurable theme
     * 
     * @return bool
     */
    public function hasConfigurableTheme()
    {
        return $this->theme == 'configurable_theme';
    }

    /**
     * Send an email with HTML content.
     *
     * @see sendPlain() for sending plaintext emails only.
     * @uses Mailer->sendHTML()
     *
     * @param string $messageID Optional message ID so the message can be identified in bounces etc.
     * @return bool Success of the sending operation from an MTA perspective.
     * Doesn't actually give any indication if the mail has been delivered to the recipient properly)
     */
    public function send($messageID = null)
    {
        // Check required objects
        if ($this->required_objects) {
            foreach ($this->required_objects as $reqName => $reqClass) {
                if ($reqName == 'Member' && !$this->templateData()->$reqName) {
                    $this->templateData()->$reqName = Member::currentUser();
                }
                if ($reqName == 'SiteConfig' && !$this->templateData()->$reqName) {
                    $this->templateData()->$reqName = SiteConfig::current_site_config();
                }
                if (!$this->templateData()->$reqName && self::$required_objects_throw_exceptions) {
                    throw new Exception('Required object ' . $reqName . ' of class ' . $reqClass . ' is not defined in template data');
                }
            }
        }

        // Check for Subject
        if (!$this->subject) {
            throw new Exception('You must set a subject');
        }

        $this->from = MandrillMailer::resolveDefaultFromEmail($this->from);
        if (!$this->from) {
            throw new Exception('You must set a sender');
        }
        if ($this->to_member && !$this->to) {
            // Include name in to as standard rfc
{'fraction_non_alphanumeric': 0.11889745386591918, 'fraction_numerical': 0.0, 'mean_word_length': 2.321567106283941, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 71, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2720994', 'n_tokens_mistral': 2329, 'n_tokens_neox': 2280, 'n_words': 647}