/** * List all email datas stored in the database. * @return Array the complete list of datas for emails and footer. Ready to use. */ public function listAll() { // Retrieve email datas from database. $datas = json_decode(osc_get_preference("email_datas", mdh_current_preferences_section()), true); if (!is_array($datas)) { // TODO. Handle error. } // Retrieve all emails from database; $emails = Page::newInstance()->listAll(true); $activeLocales = Madhouse_Utils_Collections::getFieldsFromList(osc_get_locales(), "pk_c_code"); // Complete the list of emails with new mails (not already in the JSON). foreach ($emails as $e) { if (!in_array($e["s_internal_name"], Madhouse_Utils_Collections::getFieldsFromList($datas["emails"], "s_internal_name"))) { array_push($datas["emails"], Madhouse_EmailMagick_Utils::newEmptyEmail($e["s_internal_name"])); } } // Complete, filter and sort emails. $datas["emails"] = Madhouse_Utils_Collections::sortListByField(array_map(function ($e) use($emails, $activeLocales) { $row = Madhouse_Utils_Collections::findByField($emails, "s_internal_name", $e["s_internal_name"]); $e["pk_i_id"] = $row["pk_i_id"]; // Compute the filling rate. $e["i_filled"] = Madhouse_EmailMagick_Utils::computeFillingRate($e); // Is the email still exists ? (in oc_t_pages) if (!in_array($e["s_internal_name"], Madhouse_Utils_Collections::getFieldsFromList($emails, "s_internal_name"))) { $e["b_deleted"] = true; } // Add a locale for locales that have not been provided. foreach ($activeLocales as $al) { $el = Madhouse_Utils_Collections::findByField($e["locales"], "fk_c_locale_code", $al); if (is_null($el)) { array_push($e["locales"], Madhouse_EmailMagick_Utils::newEmptyLocale($al)); } } $e["locales"] = array_values(array_filter($e["locales"], function ($v) use($activeLocales) { return in_array($v["fk_c_locale_code"], $activeLocales); })); return $e; }, $datas["emails"]), "s_internal_name"); // Filter the footer datas (only active locales are left). $datas["footer"]["locales"] = array_values(array_filter($datas["footer"]["locales"], function ($v) use($activeLocales) { return in_array($v["fk_c_locale_code"], $activeLocales); })); // Add a locale for locales that have not been provided. foreach ($activeLocales as $al) { $fl = Madhouse_Utils_Collections::findByField($datas["footer"]["locales"], "fk_c_locale_code", $al); if (is_null($fl)) { array_push($datas["footer"]["locales"], Madhouse_EmailMagick_Utils::newEmptyLocale($al)); } } return $datas; }
/** * 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()))); }
/** * Upgrades the model & preferences to 1.01. * - Fix for 1.00 where version does not exist. * @return void * @since 1.01 */ public static function upgrade101() { if (mdh_get_preference("version") === "") { osc_set_preference("version", "1.01", mdh_current_preferences_section(), "STRING"); } }
/** * (hook: uninstall) Make un-installation operations * It destroys the database schema and unsets some preferences. * @returns void. */ function mdh_emailmagick_uninstall() { mdh_delete_preferences(mdh_current_preferences_section()); }