/**
  * 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_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'));
 }
 /**
  * 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;
 }