<?php
/**
  * Ahead WebSoft Technologies Pvt Ltd.
  * @copyright  Copyright 2006-2026 Ahead WebSoft Technologies Pvt Ltd.
  * @license    https://www.socialengine.com/policies
*/

class Core_Form_Admin_Message_Mail extends Engine_Form
{

  public function init()
  {

  	$description = $this->getTranslator()->translate(
          'Using this form, you will be able to send an email out to all of your members. Emails are sent out using a queue system, so they will be sent out over time. An email will be sent to you when all emails have been sent. <br>');

  	$settings = Engine_Api::_()->getApi('settings', 'core');

  	if( $settings->getSetting('user.support.links', 0) == 1 ) {
  	  $moreinfo = $this->getTranslator()->translate(
          'More Info: <a href="%1$s" target="_blank"> KB Article</a>');
  	} else {
  	  $moreinfo = $this->getTranslator()->translate(
          '');
  	}

    $description = vsprintf($description.$moreinfo, array(
      'https://community.socialengine.com/blogs/597/20/email-all-members',
    ));

	  // Decorators
    $this->loadDefaultDecorators();
	  $this->getDecorator('Description')->setOption('escape', false);

    $this
      ->setTitle('Email All Members')
      ->setDescription($description);

    $settings = Engine_Api::_()->getApi('settings', 'core')->core_mail;

    if( !@$settings['queueing'] ) {
      $this->addElement('Radio', 'queueing', array(
        'label' => 'Utilize Mail Queue',
        'description' => 'Mail queueing permits the emails to be sent out over time, preventing your mail server
           from being overloaded by outgoing emails.  It is recommended you utilize mail queueing for large email
           blasts to help prevent negative performance impacts on your site.',
        'multiOptions' => array(
          1 => 'Utilize Mail Queue (recommended)',
          0 => 'Send all emails immediately (only recommended for less than 100 recipients).',
        ),
        'value' => 1,
      ));
    }

    $this->addElement('Text', 'from_address', array(
      'label' => 'From:',
      'value' => (!empty($settings['from']) ? $settings['from'] : 'noreply@' . $_SERVER['HTTP_HOST']),
      'required' => true,
      'allowEmpty' => false,
      'validators' => array(
        'EmailAddress',
      )
    ));
    $this->from_address->getValidator('EmailAddress')->getHostnameValidator()->setValidateTld(false);
    $this->addElement('Text', 'from_name', array(
      'label' => 'From (name):',
      'required' => true,
      'allowEmpty' => false,
      'value' => (!empty($settings['name']) ? $settings['name'] : 'Site Administrator'),
    ));

    $this->addElement('Text', 'subject', array(
      'label' => 'Subject:',
      'required' => true,
      'allowEmpty' => false,
    ));

    $this->addElement('Textarea', 'body', array(
      'label' => 'Body',
      'required' => true,
      'allowEmpty' => false,
      'description' => '(HTML or Plain Text)',
    ));
    $this->body->getDecorator('Description')->setOptions(array('placement' => 'APPEND'));

    $this->addElement('Textarea', 'body_text', array(
      'label' => 'Body (text)',
    ));

    $this->addDisplayGroup(array('body_text'), 'advanced', array(
      'decorators' => array(
        'FormElements',
        array('Fieldset', array('style' => 'display:none;')),
      ),
    ));

    $this->addElement('Select', 'choose_member', array(
      'label' => 'Choose Members',
      'description' => 'Choose Members ( To send emailt o all members select "All Members / Member Levels / Profile Types / Networks" option. New settings will show to choose different member levels, profile types and networks for this option. You can choose all or specific members to send the emails.)',
      'multiOptions' => array(
				'4' => 'All Members / Member Levels / Profile Types / Networks',
        '1' => 'Specific Member',
        '5' => 'External Emails',
      ),
      'onchange' =>  'choosemember(this.value);',
    ));

    $levelOptions = array();
    $levelValues = array();
    foreach (Engine_Api::_()->getDbtable('levels', 'authorization')->fetchAll() as $level) {
			if($level->level_id == 5) continue;
      $levelOptions[$level->level_id] = $level->getTitle();
      $levelValues[] = $level->level_id;
    }
    // Select Member Levels
    $this->addElement('multiselect', 'member_levels', array(
        'label' => 'Member Levels',
        'multiOptions' => $levelOptions,
        'description' => 'Choose the Member Levels to which this mail will be send.',
        'value' => $levelValues,
    ));

    $networkOptions = array();
    $networkValues = array();
    foreach (Engine_Api::_()->getDbtable('networks', 'network')->fetchAll() as $network) {
      $networkOptions[$network->network_id] = $network->getTitle();
      $networkValues[] = $network->network_id;
    }
    if(engine_count($networkOptions) > 0) {
      // Select Networks
      $this->addElement('multiselect', 'networks', array(
          'label' => 'Networks',
          'multiOptions' => $networkOptions,
          'description' => 'Choose the Networks to which this mail will be send.',
          'value' => $networkValues,
      ));
    }

    $topStructure = Engine_Api::_()->fields()->getFieldStructureTop('user');
    if (engine_count($topStructure) == 1 && $topStructure[0]->getChild()->type == 'profile_type') {
      $profileTypeField = $topStructure[0]->getChild();
      $options = $profileTypeField->getOptions();
      if (engine_count($options) > 1) {
        $options = $profileTypeField->getElementParams('user');
        unset($options['options']['order']);
        unset($options['options']['multiOptions']['']);
        $optionValues = array();
        foreach ($options['options']['multiOptions'] as $key => $option) {
          $optionValues[] = $key;
        }

        $this->addElement('multiselect', 'profile_types', array(
            'label' => 'Profile Types',
            'multiOptions' => $options['options']['multiOptions'],
            'description' => 'Choose the Profile Types to which this mail will be send.',
            'value' => $optionValues
        ));
      } else if (engine_count($options) == 1) {
        $this->addElement('Hidden', 'profile_types', array(
            'value' => $options[0]->option_id,
			'order' => 10000,
        ));
      }
    }

    $this->addElement('Text', "member_name", array(
        'label' => 'Member Name',
        'description' => "Enter the name of member on your website who you want to send email in the auto-suggest below.",
        'allowEmpty' => true,
        'required' => false,
    ));
    $this->addElement('Hidden', 'user_id', array('order' => 10100,));

    $this->addElement('Textarea', 'external_emails', array(
      'label' => 'Enter correct email seprated by commas',
      'required' => false,
      'allowEmpty' => true,
    ));

    // init submit
    $this->addElement('Button', 'submit', array(
      'label' => 'Send Emails',
      'type' => 'submit',
      'ignore' => true,
    ));
  }
}