/**
  * generate_new_templates
  * This will handle the messenger, message_type selection when "adding a new custom template" for an event and will automatically create the defaults for the event.  The user would then be redirected to edit the default context for the event.
  *
  * @access protected
  * @param  string $messenger     the messenger we are generating templates for
  * @param array   $message_types array of message types that the templates are generated for.
  * @param int     $GRP_ID        If a non global template is being generated then it is expected we'll have a GRP_ID to use as the base for the new generated template.
  * @param bool    $global        true indicates generating templates on messenger activation. false requires GRP_ID for event specific template generation.
  * @throws \EE_Error
  * @return array  @see EEH_MSG_Template::_create_new_templates for the return value of each element in the array for templates
  *                that are generated.  If this is an empty array then it means no templates were generated which usually
  *                means there was an error.  Anything in the array with an empty value for `MTP_context` means that it
  *                was not a new generated template but just reactivated (which only happens for global templates that
  *                already exist in the database.
  */
 public static function generate_new_templates($messenger, $message_types, $GRP_ID = 0, $global = false)
 {
     //make sure message_type is an array.
     $message_types = (array) $message_types;
     $templates = array();
     if (empty($messenger)) {
         throw new EE_Error(__('We need a messenger to generate templates!', 'event_espresso'));
     }
     //if we STILL have empty $message_types then we need to generate an error message b/c we NEED message types to do the template files.
     if (empty($message_types)) {
         throw new EE_Error(__('We need at least one message type to generate templates!', 'event_espresso'));
     }
     EEH_MSG_Template::_set_autoloader();
     foreach ($message_types as $message_type) {
         //if global then let's attempt to get the GRP_ID for this combo IF GRP_ID is empty.
         if ($global && empty($GRP_ID)) {
             $GRP_ID = EEM_Message_Template_Group::instance()->get_one(array(array('MTP_messenger' => $messenger, 'MTP_message_type' => $message_type, 'MTP_is_global' => true)));
             $GRP_ID = $GRP_ID instanceof EE_Message_Template_Group ? $GRP_ID->ID() : 0;
         }
         // if this is global template generation.
         // First let's determine if we already HAVE global templates for this messenger and message_type combination.
         //  If we do then NO generation!!
         if ($global && EEH_MSG_Template::already_generated($messenger, $message_type, $GRP_ID)) {
             $templates[] = array('GRP_ID' => $GRP_ID, 'MTP_context' => '');
             //we already have generated templates for this so let's go to the next message type.
             continue;
         }
         $new_message_template_group = EEH_MSG_Template::create_new_templates($messenger, $message_type, $GRP_ID, $global);
         if (!$new_message_template_group) {
             continue;
         }
         $templates[] = $new_message_template_group;
     }
     return $templates;
 }
 /**
  * generate_default_message_templates
  *
  * 	@access public
  * 	@static
  * 	@return bool | array
  */
 public static function generate_default_message_templates()
 {
     $success = FALSE;
     $installed_messengers = $default_messengers = array();
     //include our helper
     EE_Registry::instance()->load_helper('MSG_Template');
     //get all installed messenger objects
     $installed = EEH_MSG_Template::get_installed_message_objects();
     //let's setup the $installed messengers in an array AND the messengers that are set to be activated on install.
     foreach ($installed['messengers'] as $msgr) {
         if ($msgr instanceof EE_messenger) {
             $installed_messengers[$msgr->name] = $msgr;
             if ($msgr->activate_on_install) {
                 $default_messengers[] = $msgr->name;
             }
         }
     }
     //let's determine if we've already got an active messengers option
     $active_messengers = EEH_MSG_Template::get_active_messengers_in_db();
     //things that have already been activated before
     $has_activated = get_option('ee_has_activated_messenger');
     //do an initial loop to determine if we need to continue
     $def_ms = array();
     foreach ($default_messengers as $msgr) {
         if (isset($active_messengers[$msgr]) || isset($has_activated[$msgr])) {
             continue;
         }
         $def_ms[] = $msgr;
     }
     //setup the $installed_mts in an array
     foreach ($installed['message_types'] as $imt) {
         if ($imt instanceof EE_message_type) {
             $installed_mts[$imt->name] = $imt;
         }
     }
     //loop through default array for default messengers (if present)
     if (!empty($def_ms)) {
         foreach ($def_ms as $messenger) {
             //all is good so let's setup the default stuff. We need to use the given messenger object (if exists) to get the default message type for the messenger.
             if (!isset($installed_messengers[$messenger])) {
                 continue;
             }
             /** @var EE_messenger[] $installed_messengers  */
             $default_mts = $installed_messengers[$messenger]->get_default_message_types();
             $active_messengers[$messenger]['obj'] = $installed_messengers[$messenger];
             foreach ($default_mts as $index => $mt) {
                 //is there an installed_mt matching the default string?  If not then nothing to do here.
                 if (!isset($installed_mts[$mt])) {
                     unset($default_mts[$index]);
                     continue;
                 }
                 //we need to setup any initial settings for message types
                 /** @var EE_message_type[] $installed_mts */
                 $settings_fields = $installed_mts[$mt]->get_admin_settings_fields();
                 $settings = array();
                 if (is_array($settings_fields)) {
                     foreach ($settings_fields as $field => $values) {
                         if (isset($values['default'])) {
                             $settings[$field] = $values['default'];
                         }
                     }
                 }
                 $active_messengers[$messenger]['settings'][$messenger . '-message_types'][$mt]['settings'] = $settings;
                 $has_activated[$messenger][] = $mt;
             }
             //setup any initial settings for the messenger
             $msgr_settings = $installed_messengers[$messenger]->get_admin_settings_fields();
             if (!empty($msgr_settings)) {
                 foreach ($msgr_settings as $field => $value) {
                     $active_messengers[$messenger]['settings'][$field] = $value;
                 }
             }
             //now let's save the settings for this messenger! Must do now because the validator checks the db for active messengers to validate.
             EEH_MSG_Template::update_active_messengers_in_db($active_messengers);
             //let's generate all the templates but only if the messenger has default_mts (otherwise its just activated).
             if (!empty($default_mts)) {
                 $success = EEH_MSG_Template::generate_new_templates($messenger, $default_mts, '', TRUE);
             }
         }
     }
     //end check for empty( $def_ms )
     //still need to see if there are any message types to activate for active messengers
     foreach ($active_messengers as $messenger => $settings) {
         $msg_obj = $settings['obj'];
         if (!$msg_obj instanceof EE_messenger) {
             continue;
         }
         $all_default_mts = $msg_obj->get_default_message_types();
         $new_default_mts = array();
         //loop through each default mt reported by the messenger and make sure its set in its active db entry.
         foreach ($all_default_mts as $index => $mt) {
             //already active? already has generated templates? || has already been activated before (we dont' want to reactivate things users intentionally deactivated).
             if (isset($has_activated[$messenger]) && in_array($mt, $has_activated[$messenger]) || isset($active_messengers[$messenger]['settings'][$messenger . '-message_types'][$mt]) || EEH_MSG_Template::already_generated($messenger, $mt, 0, FALSE)) {
                 continue;
             }
             //is there an installed_mt matching the default string?  If not then nothing to do here.
             if (!isset($installed_mts[$mt])) {
                 unset($all_default_mts[$mt]);
                 continue;
             }
             $settings_fields = $installed_mts[$mt]->get_admin_settings_fields();
             $settings = array();
             if (is_array($settings_fields)) {
                 foreach ($settings_fields as $field => $values) {
                     if (isset($values['default'])) {
                         $settings[$field] = $values['default'];
                     }
                 }
             }
             $active_messengers[$messenger]['settings'][$messenger . '-message_types'][$mt]['settings'] = $settings;
             $new_default_mts[] = $mt;
             $has_activated[$messenger][] = $mt;
         }
         if (!empty($new_default_mts)) {
             $success = EEH_MSG_Template::generate_new_templates($messenger, $new_default_mts, '', TRUE);
         }
     }
     //now let's save the settings for this messenger!
     EEH_MSG_Template::update_active_messengers_in_db($active_messengers);
     //update $has_activated record
     update_option('ee_has_activated_messenger', $has_activated);
     //that's it!
     return $success;
 }