/**
  * 	add_promotion_objects_for_global_promotions
  *
  * 	if a promotion is global but doesn't already have a corresponding EE_Promotion_Object record,
  *	then this method will create one and add it to the supplied list of $promotion_objects
  *
  * @since   1.0.4
  *
  * @param \EE_Promotion_Object[] $promotion_objects
  * @param \EE_Promotion $promotion
  * @param \EE_Base_Class[] $objects
  * @return \EE_Promotion_Object[]
  */
 public function add_promotion_objects_for_global_promotions($promotion_objects, EE_Promotion $promotion, $objects = array())
 {
     $objects = is_array($objects) ? $objects : array($objects);
     if (!empty($objects)) {
         foreach ($objects as $object) {
             if ($object instanceof EE_Base_Class && $promotion instanceof EE_Promotion && $promotion->is_global()) {
                 if (!empty($promotion_objects)) {
                     foreach ($promotion_objects as $promotion_object) {
                         if ($promotion_object instanceof EE_Promotion_Object && $promotion_object->type() == $this->slug && $promotion_object->OBJ_ID() == $object->ID()) {
                             return $promotion_objects;
                         }
                     }
                 }
                 $promotion_obj = EE_Promotion_Object::new_instance(array('PRO_ID' => $promotion->ID(), 'OBJ_ID' => $object->ID(), 'POB_type' => $this->slug, 'POB_used' => 0));
                 if ($promotion_obj->save()) {
                     $promotion_objects[$promotion_obj->ID()] = $promotion_obj;
                 }
             }
         }
     }
     return $promotion_objects;
 }
 /**
  * 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);
 }
 /**
  * Gets the number of times this promotion has been used in its particular scope.
  *
  * @since 1.0.0
  *
  * @param \EE_Promotion_Object $promotion_object
  * @return int
  */
 public function uses_left_for_scope_object(EE_Promotion_Object $promotion_object)
 {
     return $this->uses() === EE_INF_IN_DB ? INF : $this->uses() - $promotion_object->used();
 }