View.php 6.96 KB
Newer Older
bgm's avatar
bgm committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
<?php
/*
 +--------------------------------------------------------------------+
 | CiviCRM version 4.6                                                |
 +--------------------------------------------------------------------+
 | Copyright CiviCRM LLC (c) 2004-2015                                |
 +--------------------------------------------------------------------+
 | 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        |
 +--------------------------------------------------------------------+
*/

/**
 * @package CiviDiscount
 */

require_once 'CRM/CiviDiscount/DAO/Item.php';
bgm's avatar
bgm committed
33
use CRM_CiviDiscount_ExtensionUtil as E;
bgm's avatar
bgm committed
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73

/**
 * Page for displaying discount code details
 */
class CRM_CiviDiscount_Page_View extends CRM_Core_Page {
  /**
   * The id of the discount code
   *
   * @var int
   */
  protected $_id;

  protected $_multiValued = NULL;

  /**
   * The action links that we need to display for the browse screen
   *
   * @var array
   * @static
   */
  static $_links = NULL;

  /**
   * Get BAO Name
   *
   * @return string Classname of BAO.
   */
  function getBAOName() {
    return 'CRM_CiviDiscount_BAO_Item';
  }

  /**
   * Get action Links
   *
   * @return array (reference) of action links
   */
  function &links() {
    if (!(self::$_links)) {
      self::$_links = array(
        CRM_Core_Action::UPDATE => array(
bgm's avatar
bgm committed
74
          'name' => E::ts('Edit'),
bgm's avatar
bgm committed
75 76
          'url' => 'civicrm/cividiscount/discount/edit',
          'qs' => '&id=%%id%%&reset=1',
bgm's avatar
bgm committed
77
          'title' => E::ts('Edit Discount')
bgm's avatar
bgm committed
78 79
        ),
        CRM_Core_Action::DISABLE => array(
bgm's avatar
bgm committed
80
          'name' => E::ts('Disable'),
bgm's avatar
bgm committed
81
          'class' => 'crm-enable-disable',
bgm's avatar
bgm committed
82
          'title' => E::ts('Disable Discount')
bgm's avatar
bgm committed
83 84
        ),
        CRM_Core_Action::ENABLE => array(
bgm's avatar
bgm committed
85
          'name' => E::ts('Enable'),
bgm's avatar
bgm committed
86
          'class' => 'crm-enable-disable',
bgm's avatar
bgm committed
87
          'title' => E::ts('Enable Discount')
bgm's avatar
bgm committed
88 89
        ),
        CRM_Core_Action::DELETE => array(
bgm's avatar
bgm committed
90
          'name' => E::ts('Delete'),
bgm's avatar
bgm committed
91 92
          'url' => 'civicrm/cividiscount/discount/delete',
          'qs' => '&id=%%id%%&reset=1',
bgm's avatar
bgm committed
93
          'title' => E::ts('Delete Discount')
bgm's avatar
bgm committed
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
        )
      );
    }
    return self::$_links;
  }

  /**
   * Get name of edit form
   *
   * @return string Classname of edit form.
   */
  function editForm() {
    return 'CRM_CiviDiscount_Form_Item';
  }

  /**
   * Get edit form name
   *
   * @return string name of this page.
   */
  function editName() {
    return 'Discount Code';
  }

  /**
   * Get user context.
   *
   * @return string user context.
   */
  function userContext($mode = NULL) {
    return 'civicrm/cividiscount/discount';
  }

  function preProcess() {
    $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this, FALSE);

    require_once 'CRM/Utils/Rule.php';
    if (!CRM_Utils_Rule::positiveInteger($this->_id)) {
      CRM_Core_Error::fatal(ts('We need a valid discount ID for view'));
    }

    $this->assign('id', $this->_id);
    $defaults = array();
    $params = array('id' => $this->_id);

    require_once 'CRM/CiviDiscount/BAO/Item.php';
    CRM_CiviDiscount_BAO_Item::retrieve($params, $defaults);

    $this->assign('code_id', $defaults['id']);
    $this->assign('code', $defaults['code']);
    $this->assign('description', $defaults['description']);
    $this->assign('amount', $defaults['amount']);
    $this->assign('amount_type', $defaults['amount_type']);
    $this->assign('count_use', $defaults['count_use']);
    $this->assign('count_max', $defaults['count_max']);
    $this->assign('is_active', $defaults['is_active']);

    if (array_key_exists('expire_on', $defaults)) {
      $this->assign('expire_on', $defaults['expire_on']);
    }

    if (array_key_exists('active_on', $defaults)) {
      $this->assign('active_on', $defaults['active_on']);
    }

    if (array_key_exists('organization_id', $defaults)) {
      $this->assign('organization_id', $defaults['organization_id']);
      require_once 'CRM/Contact/BAO/Contact.php';
      $orgname = CRM_Contact_BAO_Contact::displayName($defaults['organization_id']);
      $this->assign('organization', $orgname);
    }

    $this->_multiValued = array(
      'autodiscount' => NULL,
      'memberships' => NULL,
      'events' => NULL,
      'pricesets' => NULL
    );

    foreach ($this->_multiValued as $mv => $info) {
      if (!empty($defaults[$mv])) {
        $v = substr($defaults[$mv], 1, -1);
        $values = explode(CRM_Core_DAO::VALUE_SEPARATOR, $v);

        $defaults[$mv] = array();
        if (!empty($values)) {
          foreach ($values as $val) {
            $defaults[$mv][] = $val;
          }
        }
      }
    }

    require_once 'CRM/CiviDiscount/Utils.php';
    require_once 'CRM/Member/BAO/MembershipType.php';

    if (array_key_exists('events', $defaults)) {
      $events = CRM_CiviDiscount_Utils::getEvents();
      $defaults['events'] = CRM_CiviDiscount_Utils::getIdsTitles($defaults['events'], $events);
      $this->assign('events', $defaults['events']);
    }

    $membershipTypes = CRM_Member_BAO_MembershipType::getMembershipTypes(FALSE);
    if (array_key_exists('memberships', $defaults)) {
      $defaults['memberships'] = CRM_CiviDiscount_Utils::getIdsTitles($defaults['memberships'], $membershipTypes);
      $this->assign('memberships', $defaults['memberships']);
    }

    if (array_key_exists('autodiscount', $defaults)) {
      $defaults['autodiscount'] = CRM_CiviDiscount_Utils::getIdsTitles($defaults['autodiscount'], $membershipTypes);
      $this->assign('autodiscount', $defaults['autodiscount']);
    }

    if (array_key_exists('pricesets', $defaults)) {
      $priceSets = CRM_CiviDiscount_Utils::getPriceSets();
      $defaults['pricesets'] = CRM_CiviDiscount_Utils::getIdsTitles($defaults['pricesets'], $priceSets);
      $this->assign('pricesets', $defaults['pricesets']);
    }

    CRM_Utils_System::setTitle($defaults['code']);
  }

  function run() {
    $this->preProcess();
    return parent::run();
  }
}