/**
  * save event relation for the applies to promotion
  *
  * @since   1.0.0
  *
  * @param EE_Promotion $promotion
  * @param array  	    $data the incoming form data
  * @return void
  */
 public function handle_promotion_update(EE_Promotion $promotion, $data)
 {
     //first do we have any selected items?
     $selected_items = !empty($data['ee_promotions_applied_selected_items_Event']) ? explode(',', $data['ee_promotions_applied_selected_items_Event']) : array();
     $evt_ids = array();
     //existing pro_objects
     $pro_objects = $promotion->promotion_objects();
     //loop through existing and remove any that aren't present in the selected_items.
     foreach ($pro_objects as $pro_obj) {
         if (!in_array($pro_obj->OBJ_ID(), $selected_items)) {
             $promotion->delete_related('Promotion_Object', array(array('POB_ID' => $pro_obj->ID())));
         }
         $evt_ids[] = $pro_obj->OBJ_ID();
     }
     //k now let's make sure any that should be added are added.
     foreach ($selected_items as $EVT_ID) {
         if (in_array($EVT_ID, $evt_ids)) {
             continue;
         }
         $promotion_obj = EE_Promotion_Object::new_instance(array('PRO_ID' => $promotion->ID(), 'OBJ_ID' => $EVT_ID, 'POB_type' => $this->slug, 'POB_used' => 0));
         $promotion_obj->save();
     }
     //any filters to save?
     $set_filters = array('EVT_CAT_ID' => !empty($data['EVT_CAT_ID']) ? $data['EVT_CAT_ID'] : null, 'EVT_start_date_filter' => !empty($data['EVT_start_date_filter']) ? $data['EVT_start_date_filter'] : null, 'EVT_end_date_filter' => !empty($data['EVT_end_date_filter']) ? $data['EVT_end_date_filter'] : null, 'EVT_title_filter' => !empty($data['EVT_title_filter']) ? $data['EVT_title_filter'] : null, 'include_expired_events_filter' => !empty($data['include_expired_events_filter']) ? $data['include_expired_events_filter'] : null);
     $promotion->update_extra_meta('promo_saved_filters', $set_filters);
 }
 /**
  * get_existing_promotion_line_item
  * searches the cart for existing line items for the specified promotion
  *
  * @since   1.0.0
  *
  * @param EE_Line_Item $parent_line_item
  * @param EE_Promotion $promotion
  * @return EE_Line_Item
  */
 public function get_existing_promotion_line_item(EE_Line_Item $parent_line_item, EE_Promotion $promotion)
 {
     return $this->get_one(array(array('TXN_ID' => $parent_line_item->TXN_ID(), 'LIN_parent' => $parent_line_item->ID(), 'OBJ_type' => 'Promotion', 'OBJ_ID' => $promotion->ID())));
 }
 public function column_actions(EE_Promotion $item)
 {
     $action_links = array();
     EE_Registry::instance()->load_helper('URL');
     if ($this->_view != 'trash') {
         $edit_query_args = array('action' => 'edit', 'PRO_ID' => $item->ID());
         $dupe_query_args = array('action' => 'duplicate', 'PRO_ID' => $item->ID());
         $edit_link = EEH_URL::add_query_args_and_nonce($edit_query_args, EE_PROMOTIONS_ADMIN_URL);
         $dupe_link = EEH_URL::add_query_args_and_nonce($dupe_query_args, EE_PROMOTIONS_ADMIN_URL);
         if (EE_Registry::instance()->CAP->current_user_can('ee_edit_promotion', 'espresso_promotions_edit_promotion', $item->ID())) {
             $action_links[] = '<a href="' . $edit_link . '" title="' . __('Edit Promotion', 'event_espresso') . '"><div class="dashicons dashicons-edit clickable ee-icon-size-20"></div></a>';
         }
         if (EE_Registry::instance()->CAP->current_user_can('ee_edit_promotion', 'espresso_promotions_edit_promotion', $item->ID())) {
             $action_links[] = '<a href="' . $dupe_link . '" title="' . __('Duplicate Promotion', 'event_espresso') . '"><div class="ee-icon ee-icon-clone clickable ee-icon-size-16"></div></a>';
         }
     } else {
         $restore_query_args = array('action' => 'restore_promotion', 'PRO_ID' => $item->ID());
         $restore_link = EEH_URL::add_query_args_and_nonce($restore_query_args, EE_PROMOTIONS_ADMIN_URL);
         if (EE_Registry::instance()->CAP->current_user_can('ee_delete_promotion', 'espresso_promotions_delete_promotion', $item->ID())) {
             $action_links[] = '<a href="' . $restore_link . '" title="' . __('Restore Promotion', 'event_espresso') . '"><div class="dashicons dashicons-backup ee-icon-size-18"></div></a>';
         }
     }
     $trash_query_args = array('action' => $this->_view == 'trash' && $item->redeemed() ? 'delete_promotion' : 'trash_promotion', 'PRO_ID' => $item->ID());
     $trash_link = EEH_URL::add_query_args_and_nonce($trash_query_args, EE_PROMOTIONS_ADMIN_URL);
     $trash_text = $this->_view == 'trash' ? __('Delete Promotion permanently', 'event_espresso') : __('Trash Promotion', 'event_espresso');
     $trash_class = $this->_view == 'trash' ? ' red-icon' : '';
     if (EE_Registry::instance()->CAP->current_user_can('ee_delete_promotion', 'espresso_promotions_delete_promotion', $item->ID())) {
         $action_links[] = $this->_view == 'trash' && $item->redeemed() > 0 ? '' : '<a href="' . $trash_link . '" title="' . $trash_text . '"><div class="dashicons dashicons-trash clickable ee-icon-size-18' . $trash_class . '"></div></a>';
     }
     $content = '<div style="width:100%;">' . "\n\t";
     $content .= implode("\n\t", $action_links);
     $content .= "\n" . '</div>' . "\n";
     echo $content;
 }
 /**
  * get_redeemable_scope_promos
  *
  * @param EE_Promotion $promotion
  * @param int          $OBJ_ID
  * @throws \EE_Error
  * @return bool
  */
 public function increment_promotion_scope_uses(EE_Promotion $promotion, $OBJ_ID = 0)
 {
     $EEM_Promotion_Object = EE_Registry::instance()->load_model('Promotion_Object');
     // retrieve promotion object having the given ID and type scope
     $promotion_object = $EEM_Promotion_Object->get_one(array(array('PRO_ID' => $promotion->ID(), 'OBJ_ID' => $OBJ_ID)));
     if ($promotion_object instanceof EE_Promotion_Object) {
         $promotion_object->increment_used();
         $promotion_object->save();
         return TRUE;
     }
     throw new EE_Error(__('A valid EE_Promotion_Object object could not be found.', 'event_espresso'));
 }
 /**
  * verify_no_exclusive_promotions_combined
  * verifies that no exclusive promotions are being combined together
  *
  * @since   1.0.0
  *
  * @param EE_Line_Item $parent_line_item
  * @param EE_Promotion $promotion
  * @return EE_Line_Item
  */
 public function verify_no_exclusive_promotions_combined(EE_Line_Item $parent_line_item, EE_Promotion $promotion)
 {
     /** @type EEM_Line_Item $EEM_Line_Item */
     $EEM_Line_Item = EE_Registry::instance()->load_model('Line_Item');
     // get all existing promotions that have already been added to the cart
     $existing_promotion_line_items = $EEM_Line_Item->get_all_promotion_line_items($parent_line_item);
     if (!empty($existing_promotion_line_items)) {
         // can't apply this new promotion if it is exclusive
         if ($promotion->is_exclusive()) {
             EE_Error::add_attention(sprintf(apply_filters('FHEE__EED_Promotions__verify_no_exclusive_promotions_combined__new_promotion_is_exclusive_notice', __('We\'re sorry, but %3$s have already been added to the cart and the "%1$s%2$s" promotion can not be combined with others.', 'event_espresso')), $promotion->code() ? $promotion->code() . ' : ' : '', $promotion->name(), strtolower($this->_config->label->plural)), __FILE__, __FUNCTION__, __LINE__);
             return false;
         }
         // new promotion is not exclusive...
         // so now determine if any existing ones are
         foreach ($existing_promotion_line_items as $existing_promotion_line_item) {
             if ($existing_promotion_line_item instanceof EE_Line_Item) {
                 $existing_promotion = $this->get_promotion_from_line_item($existing_promotion_line_item);
                 if ($existing_promotion instanceof EE_Promotion && $existing_promotion->is_exclusive()) {
                     EE_Error::add_attention(sprintf(apply_filters('FHEE__EED_Promotions__verify_no_exclusive_promotions_combined__existing_promotion_is_exclusive_notice', __('We\'re sorry, but the "%1$s%2$s" %3$s has already been added to the cart and can not be combined with others.', 'event_espresso')), $existing_promotion->code() ? $existing_promotion->code() . ' : ' : '', $existing_promotion->name(), strtolower($this->_config->label->singular)), __FILE__, __FUNCTION__, __LINE__);
                     return false;
                 }
             }
         }
     }
     return true;
 }