Task.php 4.64 KB
Newer Older
pratik.joshi's avatar
pratik.joshi committed
1 2 3
<?php
/*
 +--------------------------------------------------------------------+
totten's avatar
totten committed
4
 | CiviCRM version 5                                                  |
pratik.joshi's avatar
pratik.joshi committed
5
 +--------------------------------------------------------------------+
yashodha's avatar
yashodha committed
6
 | Copyright CiviCRM LLC (c) 2004-2018                                |
pratik.joshi's avatar
pratik.joshi committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 +--------------------------------------------------------------------+
 | This file is a part of CiviCRM.                                    |
 |                                                                    |
 | CiviCRM is free software; you can copy, modify, and distribute it  |
 | under the terms of the GNU Affero General Public License           |
 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception.   |
 |                                                                    |
 | CiviCRM is distributed in the hope that it will be useful, but     |
 | WITHOUT ANY WARRANTY; without even the implied warranty of         |
 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.               |
 | See the GNU Affero General Public License for more details.        |
 |                                                                    |
 | You should have received a copy of the GNU Affero General Public   |
 | License and the CiviCRM Licensing Exception along                  |
 | with this program; if not, contact CiviCRM LLC                     |
 | at info[AT]civicrm[DOT]org. If you have questions about the        |
 | GNU Affero General Public License or the licensing of CiviCRM,     |
 | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
 +--------------------------------------------------------------------+
26
 */
pratik.joshi's avatar
pratik.joshi committed
27 28 29 30

/**
 *
 * @package CRM
yashodha's avatar
yashodha committed
31
 * @copyright CiviCRM LLC (c) 2004-2018
pratik.joshi's avatar
pratik.joshi committed
32 33 34
 */

/**
35 36
 * Class for mailing form task actions.
 * FIXME: This needs refactoring to properly inherit from CRM_Core_Form_Task and share more functions.
pratik.joshi's avatar
pratik.joshi committed
37
 */
38
class CRM_Mailing_Form_Task extends CRM_Core_Form_Task {
pratik.joshi's avatar
pratik.joshi committed
39 40

  /**
41
   * Build all the data structures needed to build the form.
pratik.joshi's avatar
pratik.joshi committed
42
   */
43
  public function preProcess() {
pratik.joshi's avatar
pratik.joshi committed
44 45 46
    self::preProcessCommon($this);
  }

47
  /**
48
   * @param CRM_Core_Form $form
49 50
   * @param bool $useTable
   */
51
  public static function preProcessCommon(&$form, $useTable = FALSE) {
pratik.joshi's avatar
pratik.joshi committed
52 53 54 55 56 57
    $values = $form->controller->exportValues($form->get('searchFormName'));

    $form->_task = CRM_Utils_Array::value('task', $values);
    $mailingTasks = CRM_Mailing_Task::tasks();
    $form->assign('taskName', CRM_Utils_Array::value('task', $values));

58
    // ids are mailing event queue ids
pratik.joshi's avatar
pratik.joshi committed
59 60 61 62 63 64 65 66 67 68
    $ids = array();
    if ($values['radio_ts'] == 'ts_sel') {
      foreach ($values as $name => $value) {
        if (substr($name, 0, CRM_Core_Form::CB_PREFIX_LEN) == CRM_Core_Form::CB_PREFIX) {
          $ids[] = substr($name, CRM_Core_Form::CB_PREFIX_LEN);
        }
      }
    }
    else {
      $queryParams = $form->get('queryParams');
totten's avatar
totten committed
69
      $sortOrder = NULL;
totten's avatar
totten committed
70
      if ($form->get(CRM_Utils_Sort::SORT_ORDER)) {
71
        $sortOrder = $form->get(CRM_Utils_Sort::SORT_ORDER);
pratik.joshi's avatar
pratik.joshi committed
72 73 74 75 76 77 78 79
      }

      $query = new CRM_Contact_BAO_Query($queryParams, NULL, NULL, FALSE, FALSE,
        CRM_Contact_BAO_Query::MODE_MAILING
      );

      $result = $query->searchQuery(0, 0, $sortOrder);
      while ($result->fetch()) {
80
        $ids[] = $result->mailing_recipients_id;
pratik.joshi's avatar
pratik.joshi committed
81 82 83 84 85
      }
      $form->assign('totalSelectedMailingRecipients', $form->get('rowCount'));
    }

    if (!empty($ids)) {
totten's avatar
totten committed
86
      $form->_componentClause = ' civicrm_mailing_recipients.id IN ( ' . implode(',', $ids) . ' ) ';
pratik.joshi's avatar
pratik.joshi committed
87 88
    }

89
    // set the context for redirection for any task actions
pratik.joshi's avatar
pratik.joshi committed
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
    $session = CRM_Core_Session::singleton();

    $fragment = 'search';
    if ($form->_action == CRM_Core_Action::ADVANCED) {
      $fragment .= '/advanced';
    }

    $qfKey = CRM_Utils_Request::retrieve('qfKey', 'String', $form);
    $urlParams = 'force=1';
    if (CRM_Utils_Rule::qfKey($qfKey)) {
      $urlParams .= "&qfKey=$qfKey";
    }

    $url = CRM_Utils_System::url('civicrm/contact/' . $fragment, $urlParams);
    $session = CRM_Core_Session::singleton();
    $session->replaceUserContext($url);
  }

  /**
colemanw's avatar
colemanw committed
109
   * Simple shell that derived classes can call to add buttons to
pratik.joshi's avatar
pratik.joshi committed
110 111
   * the form with a customized title for the main Submit
   *
112 113
   * @param string $title
   *   Title of the main button.
Eileen McNaughton's avatar
Eileen McNaughton committed
114 115 116
   * @param string $nextType
   * @param string $backType
   * @param bool $submitOnce
pratik.joshi's avatar
pratik.joshi committed
117
   */
118
  public function addDefaultButtons($title, $nextType = 'next', $backType = 'back', $submitOnce = FALSE) {
pratik.joshi's avatar
pratik.joshi committed
119 120 121 122 123 124 125 126 127 128 129 130 131
    $this->addButtons(array(
        array(
          'type' => $nextType,
          'name' => $title,
          'isDefault' => TRUE,
        ),
        array(
          'type' => $backType,
          'name' => ts('Cancel'),
        ),
      )
    );
  }
132

Eileen McNaughton's avatar
Eileen McNaughton committed
133
}