field = $config['field']; } if(array_key_exists('emailable', $config)) { $this->emailable = $config['emailable']; } if(array_key_exists('email_fields', $config)) { $this->email_fields = $config['email_fields']; if(is_array($this->email_fields)) { // Emails fields was an array meaning more than one, I am not set up to handle more than one field at the moment trigger_error("I am not set up to handle multiple email fields. Choose one and pass it as a string"); exit; } } } // Get the current debug setting $this->debug = Configure::read(); } /** * Before we save, we want to set the emailable field * We also want to switch recursive to 0 * Please look over this method's comments */ function beforeSave(&$model) { if($model->hasField($this->field)) { // Here, we check if the field was included in the form, if not, we assign it the default value. // If it was set, we do nothing and just let it save the value from the form if(!isset($model->data[$model->name][$this->field]) && empty($model->data[$model->name][$this->field])) { $model->data[$model->name][$this->field] = $this->emailable; } } $model->recursive = $this->__old_recursive; } /** * After saving, set the recursive back to it's old value */ function afterSave(&$model) { $model->recursive = $this->__old_recursive; } /** * Before we find something, we want to store the old recursive and set a * new one of 0 */ function beforeFind(&$model) { $this->__old_recursive = $model->recursive; $model->recursive = 0; } /** * After we finished our searches, set recursive back to it's old value */ function afterFind(&$model) { $model->recursive = $this->__old_recursive; } /** * This method finds all entries that are emailable * * @return array An array of all emailable objects */ function find_all_emailable(&$model, $extract = true, $fields_only = true) { $bool = 1; if($extract) { if($fields_only) { return Set::extract($model->findAll("$this->field = $bool", $this->email_fields), "{n}.$model->name.$this->email_fields"); } else { return Set::extract($model->findAll("$this->field = $bool"), "{n}.$model->name"); } } else { if($fields_only) { return $model->findAll("$this->field = $bool", $this->email_fields); } else { return $model->findAll("$this->field = $bool"); } } } /** * This method finds all entries that are not emailable * * @return array An array of all non-emailable objects */ function find_all_non_emailable(&$model, $extract = true, $fields_only = true) { $bool = 0; // Check if we are going to do a Set:;extract() on the array if($extract) { // We are going to extract, do they want the fields only or the whole array? if($fields_only) { // Return the extracted array with only the fields they want return Set::extract($model->findAll("$this->field = $bool", $this->email_fields), "{n}.$model->name.$this->email_fields"); } else { // Returns the extracted array with every field included return Set::extract($model->findAll("$this->field = $bool"), "{n}.$model->name"); } } else { // We are not going to extract but, do they still want the fields only? if($fields_only) { // Return the array with only the fields they wanted return $model->findAll("$this->field = $bool", $this->email_fields); } else { // Return the whole array return $model->findAll("$this->field = $bool"); } } } /** * Set the emailable field to 0 (false) * * @param integer $id The id of the record to set to 0 * @return boolean */ function remove_emailable(&$model, $id) { // Get the object by it's id $object = $model->findById($id); // Set the emailable field to 0 (false) $object[$model->name][$this->field] = 0; // If it saves, return true else false return ($model->save($object)) ? true : false; } /** * Set the emailable field to 1 (true) * * @param integer $id The id of the record to set to 1 * @return boolean */ function add_emailable(&$model, $id) { // Get the object by it's id $object = $model->findById($id); // Set the emailable field to 1 (true) $object[$model->name][$this->field] = 1; // If it saves, return true else false return ($model->save($object)) ? true : false; } /** * Send an email message to everyone regardless of emailable * * @param string $template The message to send * @param object $email The email object pre-configured by the user */ function send_campaign_to_all(&$model, $template, $email_options) { // Make a new email component object $this->__get_email_object(); // Checks the message $this->__check_message($template); // Checks the options $this->__check_email_options($email_options); // Get an array of all objects $list = $model->findAll(); // Loop through the object array, sending an email to each foreach($list as $object) { // Send the email if(!$this->__send_mail($model->name, $template, $object, $email_options)) { // Could not send the email, trigger_error and exit trigger_error("Cannot send mail!"); exit; } else { // The email went through, reset the email component for the next pass $this->__email->reset(); } } // All email went through, return true return true; } function send_campaign_to_emailable(&$model, $template, $email_options) { // Make a new email component object $this->__get_email_object(); // Checks the message $this->__check_message($template); // Checks the options $this->__check_email_options($email_options); // Get an array of all non emailable objects $list = $this->find_all_emailable($model, false, false); // Loop through the object array, sending an email to each foreach($list as $object) { // Send the email if(!$this->__send_mail($model->name, $template, $object, $email_options)) { // Could not send the email, trigger_error and exit trigger_error("Cannot send mail!"); exit; } else { // The email went through, reset the email component for the next pass $this->__email->reset(); } } // All email went through, return true return true; } /** * Send an email to all objects marked as non-emailable * * @param string $template The message to send * @param array $email_options The options to be applied the the Email * Component * @return true or trigger_error */ function send_campaign_to_non_emailable(&$model, $template, $email_options = array()) { // Make a new email component object $this->__get_email_object(); // Checks the message $this->__check_message($template); // Checks the options $this->__check_email_options($email_options); // Get an array of all non emailable objects $list = $this->find_all_non_emailable($model, false, false); // Loop through the object array, sending an email to each foreach($list as $object) { // Send the email if(!$this->__send_mail($model->name, $template, $object, $email_options)) { // Could not send the email, trigger_error and exit trigger_error("Cannot send mail!"); exit; } else { // The email went through, reset the email component for the next pass $this->__email->reset(); } } // All email went through, return true return true; } /** * Makes a new Email Component * * @param mixed $email */ function __get_email_object() { App::import('Component', 'EmailComponent'); $this->__email = new EmailComponent(); $this->__email->reset(); } /** * Checks to make sure the message is a string and not empty * * @param mixed $template */ function __check_message($template) { if(!is_string($template) || empty($template)) { trigger_error("The message to be emailed must not be empty and be a string!"); exit; } } /** * Makes sure that $email_options is set and has at least a "from" and * "subject" * * @param array $email_options The options to be applied to the email * component * @return boolean */ function __check_email_options($email_options) { if(!empty($email_options)) { return (array_key_exists('subject', $email_options) && array_key_exists('from', $email_options)) ? true : false; } else { return false; } } /** * Send the email * * @param string $model_name The name of the model that is using this * behavior * @param string $template The message to send * @param array $object The object array to send to * @param array $email_options The options for the email component * @return boolean */ function __send_mail($model_name, $template, $object, $email_options) { // Extract the keys for the specific model extract($object["$model_name"]); // Start the output buffer ob_start(); // Include the template. This will substitute and passed variables in the string with the extracted keys include($template); // Clean up the buffer and store the new message in a variable $new_message = ob_get_clean(); // Set who this email is going to $this->__email->to = ${$this->email_fields}; // Set any other email options $this->__set_email_options($email_options); // Send the email and return the results return ($this->__email->send($new_message)) ? true : false; } /** * This method just sets the options passed for the email component. At the * very least, we need a "from" and a "subject" * * @param array $email_options An array of email options */ function __set_email_options($email_options) { foreach($email_options as $eKey => $eValue) { $this->__email->$eKey = $eValue; } } } ?>