/**
  * Retrieve addons filtered by package, employee, event month, event type or category.
  *
  * @since	1.4
  * @return	void
  */
 public function list_addons()
 {
     $response = array();
     $package_addons = array();
     $addons = array();
     do_action('mdjm_before_api_list_addons', $this);
     if (isset($this->request['package'])) {
         if (!is_numeric($this->request['package'])) {
             // Using name or slug
             $package = mdjm_get_package_by('name', $this->request['package']);
             if ($package) {
                 $package_id = $package->ID;
             }
         } else {
             $package_id = $this->request['package'];
         }
         if (!empty($package_id)) {
             $package_addons = mdjm_get_package_addons($package_id);
             if ($package_addons) {
                 foreach ($package_addons as $package_addon) {
                     $all_addons[] = mdjm_get_addon($package_addon);
                 }
             }
         }
     } else {
         $all_addons = mdjm_get_addons(array('suppress_filters' => false));
     }
     if (!empty($all_addons)) {
         foreach ($all_addons as $addon) {
             if (isset($this->request['employee_id']) && !mdjm_employee_has_addon($addon->ID, $this->request['employee_id'])) {
                 continue;
             }
             if (isset($this->request['event_month']) && !mdjm_addon_is_available_for_event_date($addon->ID, $this->request['event_month'])) {
                 continue;
             }
             if (isset($this->request['event_type']) && !mdjm_addon_is_available_for_event_type($addon->ID, $this->request['event_type'])) {
                 continue;
             }
             $addons[] = $addon->ID;
         }
     }
     if (empty($addons)) {
         $error = array();
         $error['error'] = __('No addons found.', 'mobile-dj-manager');
         $this->data = $error;
         $this->output();
     }
     $response['addons'] = array();
     $i = 0;
     foreach ($addons as $addon) {
         $response['addons'][$addon] = mdjm_get_addon_data($addon);
         $i++;
     }
     $response['count'] = $i;
     $this->data = array_merge($this->data, $response);
     do_action('mdjm_after_api_list_addons', $this);
     $this->output();
 }
 /**
  * Renders a dropdown list of equipment add-ons.
  *
  * @since	1.3.7
  * @param	arr		$args	@see $default
  * @return	str
  */
 public function addons_dropdown($args = array())
 {
     $defaults = array('name' => 'event_addons', 'id' => '', 'class' => '', 'selected' => '', 'show_option_none' => __('No Addons', 'mobile-dj-manager'), 'show_option_all' => false, 'chosen' => false, 'employee' => false, 'event_type' => false, 'event_date' => false, 'placeholder' => null, 'multiple' => true, 'package' => '', 'cost' => true, 'desc' => false, 'titles' => false, 'options_only' => false, 'blank_first' => false, 'data' => array());
     $args = wp_parse_args($args, $defaults);
     $options = array();
     $titles = array();
     $addons = mdjm_get_addons();
     if ($addons) {
         foreach ($addons as $addon) {
             if (!empty($args['package'])) {
                 if (is_numeric($args['package'])) {
                     $package = mdjm_get_package($args['package']);
                 } else {
                     $package = mdjm_get_package_by('slug', $args['package']);
                 }
                 if ($package) {
                     $package_items = mdjm_get_package_addons($package->ID);
                 }
                 if (!empty($package_items) && in_array($addon->ID, $package_items)) {
                     continue;
                 }
             }
             if (!empty($args['employee'])) {
                 if (!mdjm_employee_has_addon($addon->ID, $args['employee'])) {
                     continue;
                 }
             }
             if ($args['event_type']) {
                 if (!mdjm_addon_is_available_for_event_type($addon->ID, $args['event_type'])) {
                     continue;
                 }
             }
             if ($args['event_date']) {
                 if (!mdjm_addon_is_available_for_event_date($addon->ID, $args['event_date'])) {
                     continue;
                 }
             } else {
                 $args['event_date'] = NULL;
             }
             $price = '';
             if ($args['cost'] == true) {
                 $price .= ' - ' . mdjm_currency_filter(mdjm_format_amount(mdjm_get_addon_price($addon->ID, $args['event_date'])));
             }
             $desc = '';
             if ($args['desc']) {
                 $desc .= ' - ' . mdjm_get_addon_excerpt($addon->ID, $args['desc']);
             }
             $term = '';
             $terms = get_the_terms($addon->ID, 'addon-category');
             if (!empty($terms)) {
                 $term = esc_html($terms[0]->name);
             }
             $args['options']['groups'][$term][] = array($addon->ID => $addon->post_title . $price . $desc);
             if ($args['titles']) {
                 $titles[$addon->ID] = mdjm_get_addon_excerpt($addon->ID);
             }
         }
     }
     if (!empty($args['options']['groups'])) {
         ksort($args['options']['groups']);
     }
     if (!empty($titles)) {
         $args['titles'] = $titles;
     }
     $output = $this->select($args);
     return $output;
 }
/**
 * Renders the HTML code for an Addons checkbox list
 *
 * @since	1.0
 * @param	arr		$args	See @defaultsSettings for the dropdown
 * @return	HTML output for checkboxes
 */
function mdjm_addons_checkboxes($args = array())
{
    global $current_user;
    $defaults = array('name' => 'event_addons', 'id' => '', 'class' => '', 'current' => array(), 'employee' => is_user_logged_in() && !current_user_can('client') ? $current_user->ID : '', 'package' => '', 'event_type' => false, 'event_date' => false, 'title' => true, 'cost' => false);
    $args = wp_parse_args($args, $defaults);
    $output = '';
    $options = array();
    $addons = mdjm_get_addons();
    if (empty($args['id'])) {
        $args['id'] = $args['name'];
    }
    // For backwards compatibility
    if (isset($args['dj'])) {
        $args['employee'] = $args['dj'];
    }
    if ($addons) {
        foreach ($addons as $addon) {
            if (!empty($args['package'])) {
                if (is_numeric($args['package'])) {
                    $package = mdjm_get_package($args['package']);
                } else {
                    $package = mdjm_get_package_by('slug', $args['package']);
                }
                if ($package) {
                    $package_items = mdjm_get_package_addons($package->ID);
                }
                if (!empty($package_items) && in_array($addon->ID, $package_items)) {
                    continue;
                }
            }
            if (!empty($args['employee'])) {
                if (!mdjm_employee_has_addon($addon->ID, $args['employee'])) {
                    continue;
                }
            }
            if ($args['event_type']) {
                if (!mdjm_addon_is_available_for_event_type($addon->ID, $args['event_type'])) {
                    continue;
                }
            }
            if ($args['event_date']) {
                if (!mdjm_addon_is_available_for_event_date($addon->ID, $args['event_date'])) {
                    continue;
                }
            } else {
                $args['event_date'] = NULL;
            }
            $price = '';
            if ($args['cost'] == true) {
                $price .= ' - ' . mdjm_currency_filter(mdjm_format_amount(mdjm_get_addon_price($addon->ID, $args['event_date'])));
            }
            $term = '';
            $terms = get_the_terms($addon->ID, 'addon-category');
            if (!empty($terms)) {
                $term = esc_html($terms[0]->name);
            }
            $options[$term][] = array($addon->ID => $addon->post_title . $price);
        }
    }
    if (!empty($options)) {
        ksort($options);
        $i = 0;
        foreach ($options as $term => $addons) {
            if ($i == 0) {
                $output .= '<strong>' . $term . '</strong><br />' . "\n";
            }
            foreach ($addons as $items) {
                foreach ($items as $item_id => $item) {
                    $output .= sprintf('<input type="checkbox" name="%1$s[]" id="%1$s-%2$d" class="%3$s" value="%2$d" %4$s />', esc_attr($args['name']), $item_id, sanitize_html_class($args['class']), checked(in_array($item_id, $args['current']), true, false));
                    $output .= '<label for="' . esc_attr($args['name']) . '-' . $item_id . '" title="' . mdjm_get_addon_excerpt($item_id) . '">';
                    $output .= esc_html($item);
                    $output .= '</label>';
                    $output .= '<br />';
                    $i++;
                    if ($i >= count($items)) {
                        $i = 0;
                    }
                }
            }
        }
    } else {
        $output .= __('No add-ons are available', 'mobile-dj-manager');
    }
    return $output;
}