static function getUsage($id = NULL, $cid = NULL, $orgid = NULL)
 {
     require_once 'CRM/CiviDiscount/Utils.php';
     require_once 'CRM/Member/BAO/Membership.php';
     require_once 'CRM/Contact/BAO/Contact.php';
     $where = '';
     $sql = "\nSELECT    t.item_id as item_id,\n      t.contact_id as contact_id,\n      t.used_date as used_date,\n      t.contribution_id as contribution_id,\n      t.entity_table as entity_table,\n      t.entity_id as entity_id,\n      t.description as description ";
     $from = " FROM cividiscount_track AS t ";
     if ($orgid) {
         $sql .= ", i.code ";
         $where = " LEFT JOIN cividiscount_item AS i ON (i.id = t.item_id) ";
         $where .= " WHERE i.organization_id = " . CRM_Utils_Type::escape($orgid, 'Integer');
     } else {
         if ($cid) {
             $where = " WHERE t.contact_id = " . CRM_Utils_Type::escape($cid, 'Integer');
         } else {
             $where = " WHERE t.item_id = " . CRM_Utils_Type::escape($id, 'Integer');
         }
     }
     $orderby = " ORDER BY t.item_id, t.used_date ";
     $sql = $sql . $from . $where . $orderby;
     $dao = new CRM_Core_DAO();
     $dao->query($sql);
     $rows = array();
     while ($dao->fetch()) {
         $row = array();
         $row['contact_id'] = $dao->contact_id;
         $row['display_name'] = CRM_Contact_BAO_Contact::displayName($dao->contact_id);
         $row['used_date'] = $dao->used_date;
         $row['contribution_id'] = $dao->contribution_id;
         $row['entity_table'] = $dao->entity_table;
         $row['entity_id'] = $dao->entity_id;
         $row['description'] = $dao->description;
         if (isset($dao->code)) {
             $row['code'] = $dao->code;
         }
         if ($row['entity_table'] == 'civicrm_participant') {
             $event_id = self::_get_participant_event($dao->entity_id);
             $events = CRM_CiviDiscount_Utils::getEvents();
             if (array_key_exists($event_id, $events)) {
                 $row['event_title'] = $events[$event_id];
             }
         } else {
             if ($row['entity_table'] == 'civicrm_membership') {
                 $result = CRM_Member_BAO_Membership::getStatusANDTypeValues($dao->entity_id);
                 if (array_key_exists($dao->entity_id, $result)) {
                     if (array_key_exists('membership_type', $result[$dao->entity_id])) {
                         $row['membership_title'] = $result[$dao->entity_id]['membership_type'];
                     }
                 }
             }
         }
         $rows[] = $row;
     }
     return $rows;
 }
 /**
  * Function to build the form
  *
  * @return None
  * @access public
  */
 public function buildQuickForm()
 {
     parent::buildQuickForm();
     if ($this->_action & (CRM_Core_Action::DELETE | CRM_Core_Action::COPY)) {
         return;
     }
     $this->applyFilter('__ALL__', 'trim');
     $element = $this->add('text', 'code', ts('Discount Code'), CRM_Core_DAO::getAttribute('CRM_CiviDiscount_DAO_Item', 'code'), TRUE);
     $this->addRule('code', ts('Code already exists in Database.'), 'objectExists', array('CRM_CiviDiscount_DAO_Item', $this->_id, 'code'));
     $this->addRule('code', ts('Code can only consist of alpha-numeric characters'), 'variable');
     if ($this->_action & CRM_Core_Action::UPDATE) {
         $element->freeze();
     }
     $this->add('text', 'description', ts('Description'), CRM_Core_DAO::getAttribute('CRM_CiviDiscount_DAO_Item', 'description'));
     $this->addMoney('amount', ts('Discount Amount'), TRUE, CRM_Core_DAO::getAttribute('CRM_CiviDiscount_DAO_Item', 'amount'), FALSE);
     $this->add('select', 'amount_type', NULL, array(1 => ts('Percent'), 2 => ts('Fixed Amount')), TRUE);
     $this->add('text', 'count_max', ts('Usage Limit'), CRM_Core_DAO::getAttribute('CRM_CiviDiscount_DAO_Item', 'count_max') + array('min' => 1));
     $this->addRule('count_max', ts('Must be an integer'), 'integer');
     $this->addDate('active_on', ts('Activation Date'), FALSE);
     $this->addDate('expire_on', ts('Expiration Date'), FALSE);
     $this->addEntityRef('organization_id', ts('Organization'), array('api' => array('params' => array('contact_type' => 'Organization'))));
     // is this discount active ?
     $this->addElement('checkbox', 'is_active', ts('Is this discount active?'));
     $this->addElement('checkbox', 'discount_msg_enabled', ts('Display a message to users not eligible for this discount?'));
     $this->add('textarea', 'discount_msg', ts('Message to non-eligible users'), array('class' => 'big'));
     // add memberships, events, pricesets
     $membershipTypes = CRM_Member_BAO_MembershipType::getMembershipTypes(FALSE);
     if (!empty($membershipTypes)) {
         $this->add('select', 'memberships', ts('Memberships'), $membershipTypes, FALSE, $this->select2style);
     }
     $this->assignAutoDiscountFields();
     $this->addElement('text', 'advanced_autodiscount_filter_entity', ts('Specify entity for advanced autodiscount'));
     $this->addElement('text', 'advanced_autodiscount_filter_string', ts('Specify api string for advanced filter'), array('class' => 'huge'));
     $events = CRM_CiviDiscount_Utils::getEvents();
     if (!empty($events)) {
         $events = array(ts('--any event--')) + $events;
         $this->_multiValued['events'] = $events;
         $this->add('select', 'events', ts('Events'), $events, FALSE, $this->select2style);
         $eventTypes = $this->getOptions('event', 'event_type_id');
         $this->_multiValued['eventtypes'] = $eventTypes;
         $this->add('select', 'event_type_id', ts('Event Types'), $eventTypes, FALSE, $this->select2style);
     }
     $pricesets = CRM_CiviDiscount_Utils::getNestedPriceSets();
     if (!empty($pricesets)) {
         $this->_multiValued['pricesets'] = $pricesets;
         $this->add('select', 'pricesets', ts('Price Field Options'), $pricesets, FALSE, array('placeholder' => ts('- any -')) + $this->select2style);
     }
 }
 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']);
     $this->assign('discount_term', $defaults['discount_term']);
     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, 'pp_types' => 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('pp_types', $defaults)) {
         $pp_types = CRM_CiviDiscount_Utils::getPaymentProcessorTypes();
         $defaults['pp_types'] = CRM_CiviDiscount_Utils::getIdsTitles($defaults['pp_types'], $pp_types);
         $this->assign('pp_types', $defaults['pp_types']);
     }
     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']);
 }