function _get_field_names()
 {
     $fields = $this->call_parent('_get_field_names');
     // Add an option to enable e-commerce only if there are pricelists created
     if (C_Pricelist_Mapper::get_instance()->count() > 0) {
         if (is_array($fields)) {
             $fields[] = 'is_ecommerce_enabled';
         }
     }
     return $fields;
 }
 function render()
 {
     $retval = '';
     $context = $this->view->get_context('object');
     // For Galleria & slideshow displays: show the gallery trigger if a single
     // image is available for sale
     if ($context && get_class($context) == 'C_MVC_View' && !empty($context->_params['images'])) {
         $mapper = C_Pricelist_Mapper::get_instance();
         foreach ($context->_params['images'] as $image) {
             if ($mapper->find_for_image($image)) {
                 $retval = parent::render();
                 break;
             }
         }
     } else {
         // Display the trigger if the image is for sale
         $mapper = C_Pricelist_Mapper::get_instance();
         if ($mapper->find_for_image($context)) {
             $retval = parent::render();
         }
     }
     return $retval;
 }
 function get_image_items_action()
 {
     $retval = array();
     if ($image_id = $this->param('image_id')) {
         $cart = $this->param('cart');
         if ($pricelist = C_Pricelist_Mapper::get_instance()->find_for_image($image_id, TRUE)) {
             $retval = $pricelist->get_items($image_id);
             // Determine if the item is in the cart. If so, set the item's quantity
             if (isset($cart['images'][$image_id])) {
                 foreach ($retval as &$item) {
                     foreach ($cart['images'][$image_id]['items'] as $item_id => $item_props) {
                         if ($item->{$item->id_field} == $item_id) {
                             $item->quantity = $item_props['quantity'];
                             break;
                         }
                     }
                 }
             }
         }
     }
     return $retval;
 }
 function save_action()
 {
     $retval = FALSE;
     // Do I need to check security token?
     $pricelist = $this->get_model();
     // disable caching or the changes we're about to save() won't be displayed
     $mapper = C_Pricelist_Mapper::get_instance();
     $mapper->_use_cache = FALSE;
     if ($pricelist->save($_REQUEST['pricelist'])) {
         // Reset the pricelist object
         $this->pricelist = $pricelist;
         // Create price list items
         $item_mapper = C_Pricelist_Item_Mapper::get_instance();
         foreach ($_POST['pricelist_item'] as $id => $updates) {
             // Set the pricelist associated to each item
             $updates['pricelist_id'] = $pricelist->id();
             if (strpos($id, 'new-') !== FALSE) {
                 $item = $item_mapper->create($updates);
                 $item->save();
             } else {
                 $item = $item_mapper->find($id, TRUE);
                 $item->save($updates);
             }
         }
         if (!isset($_REQUEST['id'])) {
             wp_redirect(admin_url("edit.php?post_type=ngg_pricelist&id=" . $pricelist->id() . '&message=saved'));
         }
     }
     if (isset($_REQUEST['deleted_items'])) {
         $pricelist->destroy_items($_REQUEST['deleted_items']);
     }
     return $retval;
 }
 function get_shipping($use_home_country = TRUE)
 {
     $retval = 0;
     $sources = C_Pricelist_Source_Manager::get_instance();
     $mapper = C_Pricelist_Mapper::get_instance();
     $settings = C_NextGen_Settings::get_instance();
     $currency = C_NextGen_Pro_Currencies::$currencies[$settings->ecommerce_currency];
     // Consolidate items via pricelist
     $consolidated = array();
     foreach ($this->_state as $image_id => $image) {
         foreach ($image->items as $source => $pricelists) {
             foreach ($pricelists as $pricelist_id => $items) {
                 if (!isset($consolidated[$pricelist_id])) {
                     $consolidated[$pricelist_id] = array();
                 }
                 if (!isset($consolidated[$pricelist_id][$source])) {
                     $consolidated[$pricelist_id][$source] = array();
                 }
                 foreach ($items as $item) {
                     $consolidated[$pricelist_id][$source][] = $item;
                 }
             }
         }
     }
     // Foreach pricelist, calculate the items shipping
     foreach ($consolidated as $pricelist_id => $source_array) {
         foreach ($source_array as $source => $items) {
             // Normally, we would instantiate the class responsible for calculating
             // the shipping method. But for now, for simplicity sake, we'll just assume that there is
             // only one calculation method
             if ($shipping_klass = $sources->get($source, 'shipping_method')) {
                 $pricelist = $mapper->find($pricelist_id);
                 $settings = $pricelist->manual_settings;
                 // Calculate the item subtotal
                 $subtotal = 0;
                 foreach ($items as $item_id => $item) {
                     $subtotal = bcadd($subtotal, bcmul($item->price, $item->quantity, $currency['exponent']), $currency['exponent']);
                 }
                 // Calculate the shipping cost for local orders
                 $local_rate = 0;
                 if ($settings['domestic_shipping_method'] == 'flat') {
                     $local_rate = bcadd($local_rate, $settings['domestic_shipping_rate'], $currency['exponent']);
                 } else {
                     $local_rate = bcadd($local_rate, bcmul($settings['domestic_shipping_rate'], $subtotal / 100, $currency['exponent']), $currency['exponent']);
                 }
                 // Calculate the shipping cost for international orders
                 $global_rate = 0;
                 if ($settings['global_shipping_method'] == 'flat') {
                     $global_rate = bcadd($global_rate, $settings['global_shipping_rate'], $currency['exponent']);
                 } else {
                     $global_rate = bcadd($global_rate, bcmul($settings['global_shipping_rate'], $subtotal / 100, $currency['exponent']), $currency['exponent']);
                 }
                 // Determine what rate to use. The local rate is used as a minimum rate as well
                 if ($use_home_country) {
                     $retval = bcadd($retval, $local_rate, $currency['exponent']);
                 } else {
                     if ($local_rate > $global_rate) {
                         $retval = bcadd($retval, $local_rate, $currency['exponent']);
                     } else {
                         $retval = bcadd($retval, $global_rate, $currency['exponent']);
                     }
                 }
             }
         }
     }
     return $retval;
 }
 /**
  * Renders the gallery pricelist field
  */
 function render_gallery_pricelist_field($gallery)
 {
     $mapper = C_Pricelist_Mapper::get_instance();
     $selected_pricelist_id = 0;
     if ($selected_pricelist = $mapper->find_for_gallery($gallery)) {
         $selected_pricelist_id = $selected_pricelist->{$selected_pricelist->id_field};
     }
     echo "<select name='pricelist_id' id='gallery_pricelist'>";
     $selected = selected($selected_pricelist_id, 0, FALSE);
     echo "<option value='0' {$selected}>" . __('None', 'nextgen-gallery-pro') . "</option>";
     foreach ($mapper->find_all() as $pricelist) {
         $pricelist_id = $pricelist->{$pricelist->id_field};
         $pricelist_title = esc_html($pricelist->title);
         $selected = selected($selected_pricelist_id, $pricelist_id, FALSE);
         echo "<option {$selected} value='{$pricelist_id}'>{$pricelist_title}</option>";
     }
     echo "</select>";
 }