/** * Generates emails from the templates & datas. * - For each emails and for each active locale : * - Check if the emails is empty or does not exists anymore. If so, do nothing. * - Combines templates and datas to produce the s_title and s_text fields. * - Saves those settings for next run. * @return void. */ public function update() { // Decode and transform into a PHP array (true as a second parameter). $data = json_decode(Params::getParam("email_datas", false, false), true); if (!is_array($data)) { mdh_handle_error(sprintf(__("Given JSON datas are not correct (Error #%d)", mdh_current_plugin_name()), json_last_error()), mdh_emailmagick_url()); } // Get the HTML template. $template = Params::getParam("email_template", false, false); // This is current emails datas (stored in the database). $currentDatas = json_decode(osc_get_preference("email_datas", mdh_current_preferences_section()), true); // Create a new array, merge of the old and the new one. $newDatas = array("emails" => array(), "footer" => array("locales" => Madhouse_EmailMagick_Utils::mergeLocales($currentDatas["footer"]["locales"], $data["footer"]["locales"]))); // Iterate through new submitted emails data to update. $i = 0; foreach ($data["emails"] as $e) { // Get the old email datas, as they are right now. $oe = Madhouse_Utils_Collections::findByField($currentDatas["emails"], "s_internal_name", $e["s_internal_name"]); if (is_null($oe)) { // Not found in old emails. => new email has been installed. $locales = $e["locales"]; } else { // Found in old emails. Merge with new locales. $locales = Madhouse_EmailMagick_Utils::mergeLocales($oe["locales"], $e["locales"]); } // Email exists, create in order to be filled and pushed to the new datas (at the end). $nEmail = array("s_internal_name" => $e["s_internal_name"], "locales" => $locales); // Get the email from the database. $email = Page::newInstance()->findByInternalName($e["s_internal_name"]); if ($email !== false && count($email) > 0) { // Update the email. $updated = Madhouse_EmailMagick_Models_Emails::newInstance()->update($template, $email["pk_i_id"], $newDatas, $nEmail); $i += $updated; } array_push($newDatas["emails"], $nEmail); } // Saves the settings. Madhouse_Utils_Controllers::doSettingsPost(array("email_template", "email_datas"), array("email_template" => $template, "email_datas" => json_encode($newDatas)), mdh_emailmagick_url(), null, sprintf(__("Sucessfully updated %d emails (out of %d)!", mdh_current_plugin_name()), $i, count($data["emails"]) * count(osc_get_locales()))); }
/** * Returns a new empty email. * @param String $name internal name for this new email. * @return Array a new empty email, as it is in the model. */ public static function newEmptyEmail($name) { return array("s_internal_name" => $name, "locales" => array_map(function ($v) { return Madhouse_EmailMagick_Utils::newEmptyLocale($v["pk_c_code"]); }, osc_get_locales())); }
/** * Generate and updates mails. * @param String $template HTML template to serve as a base. * @param Array $data New datas for emails and footer to use * to generate and update emails in oc_t_pages. * @return int Number of locales updated. */ public function update($template, $id, $newDatas, $nEmail) { $m = new Mustache_Engine(); $activeLocales = Madhouse_Utils_Collections::getFieldsFromList(osc_get_locales(), "pk_c_code"); if ($nEmail["s_internal_name"] == "exemple_page") { mdh_error_log(array($nEmail)); } $i = 0; foreach ($nEmail["locales"] as $l) { // Find the correct footer content. $footer = Madhouse_Utils_Collections::findByField($newDatas["footer"]["locales"], "fk_c_locale_code", $l["fk_c_locale_code"]); if (in_array($l["fk_c_locale_code"], $activeLocales) && !Madhouse_EmailMagick_Utils::isLocaleEmpty($l)) { // Updates the description using DAO method. Page::newInstance()->updateDescription($id, $l["fk_c_locale_code"], $l["s_title"], $m->render($template, array("CONTENT" => $l["s_text"], "TITLE" => $l["s_title"], "EXCERPT" => $l["s_excerpt"], "FOOTER" => $footer["s_text"]))); $i++; } } return $i; }