Пример #1
0
/**
 * Constructor function.
 *
 * @param null $class
 *
 * @return \Microweber\Application Microweber Application object
 */
function mw($class = null)
{
    return app($class);
    $app = \Microweber\Application::getInstance();
    $class = str_replace('/', '\\', $class);
    if ($class == null or $class == false or strtolower($class) == 'application') {
        return $app;
    } else {
        //return $app->make($class);
        return $app->make($class);
    }
}
Пример #2
0
 public function delete_order($data)
 {
     // this function also handles ajax requests from admin
     $adm = $this->app->user_manager->is_admin();
     if (defined('MW_API_CALL') and $adm == false) {
         return $this->app->error('Not logged in as admin.' . __FILE__ . __LINE__);
     }
     $table = $table = $this->table;
     if (!is_array($data)) {
         $data = array('id' => intval($data));
     }
     if (isset($data['is_cart']) and trim($data['is_cart']) != 'false' and isset($data['id'])) {
         $this->app->cart_manager->delete_cart('session_id=' . $data['id']);
         return $data['id'];
     } elseif (isset($data['id'])) {
         $c_id = intval($data['id']);
         $this->app->database_manager->delete_by_id($table, $c_id);
         $this->app->event_manager->trigger('mw.cart.delete_order', $c_id);
         $this->app->cart_manager->delete_cart('order_id=' . $data['id']);
         return $c_id;
     }
 }
Пример #3
0
 public function apijs()
 {
     if (!defined('MW_NO_SESSION')) {
         define('MW_NO_SESSION', 1);
     }
     $ref_page = false;
     if (isset($_REQUEST['id'])) {
         $ref_page = $this->app->content_manager->get_by_id($_REQUEST['id']);
     } elseif (isset($_SERVER['HTTP_REFERER'])) {
         $ref_page = $_SERVER['HTTP_REFERER'];
         if ($ref_page != '') {
             $ref_page = $this->app->content_manager->get_by_url($ref_page);
             $page_id = $ref_page['id'];
         }
     }
     if (isset($_SERVER['HTTP_REFERER'])) {
         $cat_url = mw()->category_manager->get_category_id_from_url($_SERVER['HTTP_REFERER']);
         if ($cat_url != false) {
             if (!defined('CATEGORY_ID')) {
                 define('CATEGORY_ID', intval($cat_url));
             }
         }
     }
     $file = mw_includes_path() . 'api' . DS . 'api.js';
     $last_modified_time = $lastModified = filemtime($file);
     $ifModifiedSince = isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ? $_SERVER['HTTP_IF_MODIFIED_SINCE'] : false;
     $etagHeader = isset($_SERVER['HTTP_IF_NONE_MATCH']) ? trim($_SERVER['HTTP_IF_NONE_MATCH']) : false;
     if (defined('MW_VERSION')) {
         $etag = md5(filemtime($file) . MW_VERSION);
     } else {
         $etag = filemtime($file);
     }
     $this->app->content_manager->define_constants($ref_page);
     $l = new \Microweber\View($file);
     $l = $l->__toString();
     $l = str_replace('{SITE_URL}', $this->app->url_manager->site(), $l);
     $l = str_replace('{MW_SITE_URL}', $this->app->url_manager->site(), $l);
     $l = str_replace('%7BSITE_URL%7D', $this->app->url_manager->site(), $l);
     $response = \Response::make($l);
     $response->header('Content-Type', 'application/javascript');
     $compile_assets = \Config::get('microweber.compile_assets');
     if ($compile_assets and defined('MW_VERSION')) {
         $userfiles_dir = userfiles_path();
         $hash = md5(site_url());
         $userfiles_cache_dir = normalize_path($userfiles_dir . 'cache' . DS . 'apijs');
         $userfiles_cache_filename = $userfiles_cache_dir . 'api.' . $hash . '.' . MW_VERSION . '.js';
         if (!is_file($userfiles_cache_filename)) {
             if (!is_dir($userfiles_cache_dir)) {
                 mkdir_recursive($userfiles_cache_dir);
             }
             if (is_dir($userfiles_cache_dir)) {
                 @file_put_contents($userfiles_cache_filename, $l);
             }
         } else {
             $fmd5 = md5_file($userfiles_cache_filename);
             $fmd = md5($l);
             if ($fmd5 != $fmd) {
                 @file_put_contents($userfiles_cache_filename, $l);
             }
         }
     }
     if (!$this->app->make('config')->get('app.debug')) {
         // enable caching if in not in debug mode
         $response->header('Etag', $etag);
         $response->header('Last-Modified', gmdate('D, d M Y H:i:s', $last_modified_time) . ' GMT');
         $response->setTtl(30);
     }
     return $response;
 }
Пример #4
0
 public function update_cart($data)
 {
     if (isset($data['content_id'])) {
         $data['for'] = 'content';
         $for_id = $data['for_id'] = $data['content_id'];
     }
     $override = $this->app->event_manager->trigger('mw.shop.update_cart', $data);
     if (is_array($override)) {
         foreach ($override as $resp) {
             if (is_array($resp) and !empty($resp)) {
                 $data = array_merge($data, $resp);
             }
         }
     }
     if (!isset($data['for'])) {
         $data['for'] = 'content';
     }
     $update_qty = 0;
     $update_qty_new = 0;
     if (isset($data['qty'])) {
         $update_qty_new = $update_qty = intval($data['qty']);
         unset($data['qty']);
     }
     if (!isset($data['for']) or !isset($data['for_id'])) {
         if (!isset($data['id'])) {
         } else {
             $cart = array();
             $cart['id'] = intval($data['id']);
             $cart['limit'] = 1;
             $data_existing = $this->get($cart);
             if (is_array($data_existing) and is_array($data_existing[0])) {
                 $data = array_merge($data, $data_existing[0]);
             }
         }
     }
     if (!isset($data['for']) and isset($data['rel_type'])) {
         $data['for'] = $data['rel_type'];
     }
     if (!isset($data['for_id']) and isset($data['rel_id'])) {
         $data['for_id'] = $data['rel_id'];
     }
     if (!isset($data['for']) and !isset($data['for_id'])) {
         $this->app->error('Invalid for and for_id params');
     }
     $data['for'] = $this->app->database_manager->assoc_table_name($data['for']);
     $for = $data['for'];
     $for_id = intval($data['for_id']);
     if ($for_id == 0) {
         $this->app->error('Invalid data');
     }
     $cont_data = false;
     if ($update_qty > 0) {
         $data['qty'] = $update_qty;
     }
     if ($data['for'] == 'content') {
         $cont = $this->app->content_manager->get_by_id($for_id);
         $cont_data = $this->app->content_manager->data($for_id);
         if ($cont == false) {
             $this->app->error('Invalid product?');
         } else {
             if (is_array($cont) and isset($cont['title'])) {
                 $data['title'] = $cont['title'];
             }
         }
     }
     if (isset($data['title']) and is_string($data['title'])) {
         $data['title'] = strip_tags($data['title']);
     }
     $found_price = false;
     $add = array();
     if (isset($data['custom_fields_data']) and is_array($data['custom_fields_data'])) {
         $add = $data['custom_fields_data'];
     }
     $prices = array();
     $skip_keys = array();
     $content_custom_fields = array();
     $content_custom_fields = $this->app->fields_manager->get($for, $for_id, 1);
     if ($content_custom_fields == false) {
         $content_custom_fields = $data;
         if (isset($data['price'])) {
             $found_price = $data['price'];
         }
     } elseif (is_array($content_custom_fields)) {
         foreach ($content_custom_fields as $cf) {
             if (isset($cf['type']) and $cf['type'] == 'price') {
                 $prices[$cf['name']] = $cf['value'];
             }
         }
     }
     foreach ($data as $k => $item) {
         if ($k != 'for' and $k != 'for_id' and $k != 'title') {
             $found = false;
             foreach ($content_custom_fields as $cf) {
                 if (isset($cf['type']) and isset($cf['name']) and $cf['type'] != 'price') {
                     $key1 = str_replace('_', ' ', $cf['name']);
                     $key2 = str_replace('_', ' ', $k);
                     if (isset($cf['name']) and ($cf['name'] == $k or $key1 == $key2)) {
                         $k = str_replace('_', ' ', $k);
                         $found = true;
                         if (is_array($cf['values'])) {
                             if (in_array($item, $cf['values'])) {
                                 $found = true;
                             }
                         }
                         if ($found == false and $cf['value'] != $item) {
                             unset($item);
                         }
                     }
                 } elseif (isset($cf['type']) and $cf['type'] == 'price') {
                     if ($cf['value'] != '') {
                         $prices[$cf['name']] = $cf['value'];
                     }
                 } elseif (isset($cf['type']) and $cf['type'] == 'price') {
                     if ($cf['value'] != '') {
                         $prices[$cf['name']] = $cf['value'];
                     }
                 }
             }
             if ($found == false) {
                 $skip_keys[] = $k;
             }
             if (is_array($prices)) {
                 foreach ($prices as $price_key => $price) {
                     if (isset($data['price'])) {
                         if ($price == $data['price']) {
                             $found = true;
                             $found_price = $price;
                         }
                     } elseif ($price == $item) {
                         $found = true;
                         if ($found_price == false) {
                             $found_price = $item;
                         }
                     }
                 }
                 if ($found_price == false) {
                     $found_price = array_pop($prices);
                 } else {
                     if (count($prices) > 1) {
                         foreach ($prices as $pk => $pv) {
                             if ($pv == $found_price) {
                                 $add[$pk] = $this->app->shop_manager->currency_format($pv);
                             }
                         }
                     }
                 }
             }
             if (isset($item)) {
                 if ($found == true) {
                     if ($k != 'price' and !in_array($k, $skip_keys)) {
                         $add[$k] = $this->app->format->clean_html($item);
                     }
                 }
             }
         }
     }
     if ($found_price == false and is_array($prices)) {
         $found_price = array_pop($prices);
     }
     if ($found_price == false) {
         $found_price = 0;
     }
     if (is_array($prices)) {
         ksort($add);
         asort($add);
         $add = mw()->format->clean_xss($add);
         $table = $this->table;
         $cart = array();
         $cart['rel_type'] = $data['for'];
         $cart['rel_id'] = intval($data['for_id']);
         $cart['title'] = mw()->format->clean_html($data['title']);
         $cart['price'] = floatval($found_price);
         $cart_return = $cart;
         $cart_return['custom_fields_data'] = $add;
         $cart['custom_fields_data'] = $this->app->format->array_to_base64($add);
         $cart['custom_fields_json'] = json_encode($add);
         $cart['order_completed'] = 0;
         $cart['allow_html'] = 1;
         $cart['session_id'] = mw()->user_manager->session_id();
         $cart['limit'] = 1;
         $check_cart = $this->get($cart);
         if ($check_cart != false and is_array($check_cart) and isset($check_cart[0])) {
             $cart['id'] = $check_cart[0]['id'];
             if ($update_qty > 0) {
                 $cart['qty'] = $check_cart[0]['qty'] + $update_qty;
             } elseif ($update_qty_new > 0) {
                 $cart['qty'] = $update_qty_new;
             } else {
                 $cart['qty'] = $check_cart[0]['qty'] + 1;
             }
         } else {
             if ($update_qty > 0) {
                 $cart['qty'] = $update_qty;
             } else {
                 $cart['qty'] = 1;
             }
         }
         if (isset($cont_data['qty']) and trim($cont_data['qty']) != 'nolimit') {
             if (intval($cont_data['qty']) < intval($cart['qty'])) {
                 $cart['qty'] = $cont_data['qty'];
             }
         }
         if (isset($data['other_info']) and is_string($data['other_info'])) {
             $cart['other_info'] = strip_tags($data['other_info']);
         }
         if (isset($data['description']) and is_string($data['description'])) {
             $cart_return['description'] = $cart['description'] = $this->app->format->clean_html($data['description']);
         }
         if (isset($data['item_image']) and is_string($data['item_image'])) {
             $cart_return['item_image'] = $cart['item_image'] = $this->app->format->clean_html($data['item_image']);
         }
         if (isset($data['link']) and is_string($data['link'])) {
             $cart_return['link'] = $cart['link'] = $this->app->format->clean_html($data['link']);
         }
         if (isset($data['currency']) and is_string($data['currency'])) {
             $cart_return['currency'] = $cart['currency'] = $this->app->format->clean_html($data['link']);
         }
         $cart_saved_id = $this->app->database_manager->save($table, $cart);
         $this->app->cache_manager->delete('cart');
         $this->app->cache_manager->delete('cart_orders/global');
         if (isset($cart['rel_type']) and isset($cart['rel_id']) and $cart['rel_type'] == 'content') {
             $cart_return['image'] = $this->app->media_manager->get_picture($cart['rel_id']);
             $cart_return['product_link'] = $this->app->content_manager->link($cart['rel_id']);
         }
         $cart_sum = $this->sum();
         $cart_qty = $this->sum();
         return array('success' => 'Item added to cart', 'product' => $cart_return, 'cart_sum' => $cart_sum, 'cart_items' => $cart_qty);
     } else {
         return array('error' => 'Invalid cart items');
     }
 }