/** * Sets up the JavsScript cart * * Searches all $themesPages elements for the first occurrence of the * "shopJsCart" template block. * Generates the structure of the Javascript cart, puts it in the template, * and registers all required JS code. * Note that this is only ever called when the JS cart is enabled in the * extended settings! * @access public * @global array $_ARRAYLANG Language array * @global array $themesPages Theme template array * @return void * @static */ static function setJsCart() { global $_ARRAYLANG, $themesPages; if (!\Cx\Core\Setting\Controller\Setting::getValue('use_js_cart', 'Shop')) { return; } $objTemplate = new \Cx\Core\Html\Sigma('.'); $objTemplate->setErrorHandling(PEAR_ERROR_DIE); $match = null; $div_cart = $div_product = ''; foreach ($themesPages as $index => $content) { //\DBG::log("Shop::setJsCart(): Section $index"); $objTemplate->setTemplate($content, false, false); if (!$objTemplate->blockExists('shopJsCart')) { continue; } //\DBG::log("Shop::setJsCart(): In themespage $index: {$themesPages[$index]}"); $objTemplate->setCurrentBlock('shopJsCart'); // Set all language entries and replace formats $objTemplate->setGlobalVariable($_ARRAYLANG); if ($objTemplate->blockExists('shopJsCartProducts')) { $objTemplate->parse('shopJsCartProducts'); $div_product = $objTemplate->get('shopJsCartProducts'); //\DBG::log("Shop::setJsCart(): Got Product: $div_product"); $objTemplate->replaceBlock('shopJsCartProducts', '[[SHOP_JS_CART_PRODUCTS]]'); } $objTemplate->touchBlock('shopJsCart'); $objTemplate->parse('shopJsCart'); $div_cart = $objTemplate->get('shopJsCart'); //\DBG::log("Shop::setJsCart(): Got Cart: $div_cart"); if (preg_match('#^([\\n\\r]?[^<]*<.*id=["\']shopJsCart["\'][^>]*>)(([\\n\\r].*)*)(</[^>]*>[^<]*[\\n\\r]?)$#', $div_cart, $match)) { //\DBG::log("Shop::setJsCart(): Matched DIV {$match[1]}, content: {$match[2]}"); $themesPages[$index] = preg_replace('@(<!--\\s*BEGIN\\s+(shopJsCart)\\s*-->.*?<!--\\s*END\\s+\\2\\s*-->)@s', $match[1] . $_ARRAYLANG['TXT_SHOP_CART_IS_LOADING'] . $match[4], $content); /* // Template use won't work, because it kills the remaining <!-- blocks -->! $objTemplate->setTemplate($content, false, false); $objTemplate->replaceBlock('shopJsCart', $match[1]. $_ARRAYLANG['TXT_SHOP_CART_IS_LOADING']. $match[4]); $themesPages[$index] = $objTemplate->get(); */ //\DBG::log("Shop::setJsCart(): Out themespage $index: {$themesPages[$index]}"); } // One instance only (mind that there's a unique id attribute) self::$use_js_cart = true; break; } if (!self::$use_js_cart) { return; } self::registerJavascriptCode(); \ContrexxJavascript::getInstance()->setVariable('TXT_SHOP_CART_IS_LOADING', $_ARRAYLANG['TXT_SHOP_CART_IS_LOADING'], 'shop/cart'); \ContrexxJavascript::getInstance()->setVariable('TXT_SHOP_COULD_NOT_LOAD_CART', $_ARRAYLANG['TXT_SHOP_COULD_NOT_LOAD_CART'], 'shop/cart'); \ContrexxJavascript::getInstance()->setVariable('TXT_EMPTY_SHOPPING_CART', $_ARRAYLANG['TXT_EMPTY_SHOPPING_CART'], 'shop/cart'); \ContrexxJavascript::getInstance()->setVariable("url", (string) \Cx\Core\Routing\URL::fromModuleAndCMd('Shop' . MODULE_INDEX, 'cart', FRONTEND_LANG_ID, array('remoteJs' => 'addProduct')), 'shop/cart'); \JS::registerJS(substr(\Cx\Core\Core\Controller\Cx::instanciate()->getModuleFolderName() . '/Shop/View/Script/cart.js', 1)); \JS::registerCode("cartTpl = '" . preg_replace(array('/\'/', '/[\\n\\r]/', '/\\//'), array('\\\'', '\\n', '\\/'), $div_cart) . "';\n" . "cartProductsTpl = '" . preg_replace(array('/\'/', '/[\\n\\r]/', '/\\//'), array('\\\'', '\\n', '\\/'), $div_product) . "';\n"); }
/** * Sets up the Order statistics * @param \Cx\Core\Html\Sigma $objTemplate The optional Template, * by reference * @global ADONewConnection $objDatabase * @global array $_ARRAYLANG * @todo Rewrite the statistics in a seperate class, extending Order * @static */ static function view_statistics(&$objTemplate = null) { global $objDatabase, $_ARRAYLANG; if (!$objTemplate || !$objTemplate->blockExists('no_order')) { $objTemplate = new \Cx\Core\Html\Sigma(\Cx\Core\Core\Controller\Cx::instanciate()->getCodeBaseModulePath() . '/Shop/View/Template/Backend'); $objTemplate->loadTemplateFile('module_shop_statistic.html'); } $objTemplate->setGlobalVariable($_ARRAYLANG); // Get the first order date; if its empty, no order has been placed yet $time_first_order = Order::getFirstOrderTime(); if (!$time_first_order) { $objTemplate->touchBlock('no_order'); return $objTemplate; } $year_first_order = date('Y', $time_first_order); $month_first_order = date('m', $time_first_order); $start_month = $end_month = $start_year = $end_year = NULL; if (isset($_REQUEST['submitdate'])) { // A range is requested $start_month = intval($_REQUEST['startmonth']); $end_month = intval($_REQUEST['stopmonth']); $start_year = intval($_REQUEST['startyear']); $end_year = intval($_REQUEST['stopyear']); } else { // Default range to one year, or back to the first order if less $start_month = $month_first_order; $end_month = Date('m'); $start_year = $end_year = Date('Y'); if ($year_first_order < $start_year) { $start_year -= 1; if ($year_first_order < $start_year || $month_first_order < $start_month) { $start_month = $end_month; } } } $objTemplate->setVariable(array('SHOP_START_MONTH' => Shopmanager::getMonthDropdownMenu($start_month), 'SHOP_END_MONTH' => Shopmanager::getMonthDropdownMenu($end_month), 'SHOP_START_YEAR' => Shopmanager::getYearDropdownMenu($start_year, $year_first_order), 'SHOP_END_YEAR' => Shopmanager::getYearDropdownMenu($end_year, $year_first_order))); $start_date = date(ASCMS_DATE_FORMAT_INTERNATIONAL_DATETIME, mktime(0, 0, 0, $start_month, 1, $start_year)); // mktime() will fix the month from 13 to 01, see example 2 // on http://php.net/manual/de/function.mktime.php. // Mind that this is exclusive and only used in the queries below // so that Order date < $end_date! $end_date = date(ASCMS_DATE_FORMAT_INTERNATIONAL_DATETIME, mktime(0, 0, 0, $end_month + 1, 1, $end_year)); $selectedStat = isset($_REQUEST['selectstats']) ? intval($_REQUEST['selectstats']) : 0; if ($selectedStat == 2) { // Product statistic $objTemplate->setVariable(array('TXT_COLUMN_1_DESC' => $_ARRAYLANG['TXT_PRODUCT_NAME'], 'TXT_COLUMN_2_DESC' => $_ARRAYLANG['TXT_COUNT_ARTICLES'], 'TXT_COLUMN_3_DESC' => $_ARRAYLANG['TXT_STOCK'], 'SHOP_ORDERS_SELECTED' => '', 'SHOP_ARTICLES_SELECTED' => \Html::ATTRIBUTE_SELECTED, 'SHOP_CUSTOMERS_SELECTED' => '')); $arrSql = \Text::getSqlSnippets('`B`.`id`', FRONTEND_LANG_ID, 'Shop', array('title' => Product::TEXT_NAME)); $query = "\n SELECT A.product_id AS id,\n A.quantity AS shopColumn2,\n A.price AS sum,\n B.stock AS shopColumn3,\n C.currency_id, " . $arrSql['field'] . "\n FROM " . DBPREFIX . "module_shop" . MODULE_INDEX . "_order_items AS A\n JOIN " . DBPREFIX . "module_shop" . MODULE_INDEX . "_orders AS C\n ON A.order_id=C.id\n JOIN " . DBPREFIX . "module_shop" . MODULE_INDEX . "_products AS B\n ON A.product_id=B.id" . $arrSql['join'] . "\n WHERE C.date_time>='{$start_date}'\n AND C.date_time<'{$end_date}'\n AND ( C.status=" . Order::STATUS_CONFIRMED . "\n OR C.status=" . Order::STATUS_COMPLETED . ")\n ORDER BY shopColumn2 DESC"; } elseif ($selectedStat == 3) { // Customer statistic $objTemplate->setVariable(array('TXT_COLUMN_1_DESC' => $_ARRAYLANG['TXT_NAME'], 'TXT_COLUMN_2_DESC' => $_ARRAYLANG['TXT_COMPANY'], 'TXT_COLUMN_3_DESC' => $_ARRAYLANG['TXT_COUNT_ARTICLES'], 'SHOP_ORDERS_SELECTED' => '', 'SHOP_ARTICLES_SELECTED' => '', 'SHOP_CUSTOMERS_SELECTED' => \Html::ATTRIBUTE_SELECTED)); $query = "\n SELECT A.sum AS sum,\n A.currency_id AS currency_id,\n sum(B.quantity) AS shopColumn3,\n A.customer_id AS id\n FROM " . DBPREFIX . "module_shop" . MODULE_INDEX . "_orders AS A\n JOIN " . DBPREFIX . "module_shop" . MODULE_INDEX . "_order_items AS B\n ON A.id=B.order_id\n WHERE A.date_time>='{$start_date}'\n AND A.date_time<'{$end_date}'\n AND ( A.status=" . Order::STATUS_CONFIRMED . "\n OR A.status=" . Order::STATUS_COMPLETED . ")\n GROUP BY B.order_id\n ORDER BY sum DESC"; } else { // Order statistic (default); sales per month $objTemplate->setVariable(array('TXT_COLUMN_1_DESC' => $_ARRAYLANG['TXT_DATE'], 'TXT_COLUMN_2_DESC' => $_ARRAYLANG['TXT_COUNT_ORDERS'], 'TXT_COLUMN_3_DESC' => $_ARRAYLANG['TXT_COUNT_ARTICLES'], 'SHOP_ORDERS_SELECTED' => \Html::ATTRIBUTE_SELECTED, 'SHOP_ARTICLES_SELECTED' => '', 'SHOP_CUSTOMERS_SELECTED' => '')); $query = "\n SELECT SUM(A.quantity) AS shopColumn3,\n COUNT(A.order_id) AS shopColumn2,\n B.currency_id,\n B.sum AS sum,\n DATE_FORMAT(B.date_time, '%m') AS month,\n DATE_FORMAT(B.date_time, '%Y') AS year\n FROM " . DBPREFIX . "module_shop" . MODULE_INDEX . "_order_items AS A,\n " . DBPREFIX . "module_shop" . MODULE_INDEX . "_orders AS B\n WHERE A.order_id=B.id\n AND B.date_time>='{$start_date}'\n AND B.date_time<'{$end_date}'\n AND ( B.status=" . Order::STATUS_CONFIRMED . "\n OR B.status=" . Order::STATUS_COMPLETED . ")\n GROUP BY B.id\n ORDER BY year DESC, month DESC"; } $arrayResults = array(); $objResult = $objDatabase->Execute($query); if (!$objResult) { return Order::errorHandler(); } $sumColumn3 = $sumColumn4 = 0; $sumColumn2 = ''; if ($selectedStat == 2) { // Product statistc while (!$objResult->EOF) { // set currency id Currency::setActiveCurrencyId($objResult->fields['currency_id']); $key = $objResult->fields['id']; if (!isset($arrayResults[$key])) { $arrayResults[$key] = array('column1' => '<a href="index.php?cmd=Shop' . MODULE_INDEX . '&act=products&tpl=manage&id=' . $objResult->fields['id'] . '" title="' . $objResult->fields['title'] . '">' . $objResult->fields['title'] . '</a>', 'column2' => 0, 'column3' => $objResult->fields['shopColumn3'], 'column4' => 0); } $arrayResults[$key]['column2'] += +$objResult->fields['shopColumn2']; $arrayResults[$key]['column4'] += +$objResult->fields['shopColumn2'] * Currency::getDefaultCurrencyPrice($objResult->fields['sum']); $objResult->MoveNext(); } if (is_array($arrayResults)) { foreach ($arrayResults as $entry) { $sumColumn2 = $sumColumn2 + $entry['column2']; $sumColumn3 = $sumColumn3 + $entry['column3']; $sumColumn4 = $sumColumn4 + $entry['column4']; } rsort($arrayResults); } } elseif ($selectedStat == 3) { // Customer statistic while (!$objResult->EOF) { Currency::setActiveCurrencyId($objResult->fields['currency_id']); $key = $objResult->fields['id']; if (!isset($arrayResults[$key])) { $objUser = \FWUser::getFWUserObject()->objUser; $objUser = $objUser->getUser($key); $company = ''; $name = $_ARRAYLANG['TXT_SHOP_CUSTOMER_NOT_FOUND']; if ($objUser) { $company = $objUser->getProfileAttribute('company'); $name = $objUser->getProfileAttribute('firstname') . ' ' . $objUser->getProfileAttribute('lastname'); } $arrayResults[$key] = array('column1' => '<a href="index.php?cmd=Shop' . MODULE_INDEX . '&act=customerdetails&customer_id=' . $objResult->fields['id'] . '">' . $name . '</a>', 'column2' => $company, 'column3' => 0, 'column4' => 0); } $arrayResults[$key]['column3'] += $objResult->fields['shopColumn3']; $arrayResults[$key]['column4'] += Currency::getDefaultCurrencyPrice($objResult->fields['sum']); $sumColumn3 += $objResult->fields['shopColumn3']; $sumColumn4 += Currency::getDefaultCurrencyPrice($objResult->fields['sum']); $objResult->MoveNext(); } } else { // Order statistic (default) $arrayMonths = explode(',', $_ARRAYLANG['TXT_MONTH_ARRAY']); while (!$objResult->EOF) { $key = $objResult->fields['year'] . '.' . $objResult->fields['month']; if (!isset($arrayResults[$key])) { $arrayResults[$key] = array('column1' => '', 'column2' => 0, 'column3' => 0, 'column4' => 0); } $arrayResults[$key]['column1'] = $arrayMonths[intval($objResult->fields['month']) - 1] . ' ' . $objResult->fields['year']; $arrayResults[$key]['column2'] = $arrayResults[$key]['column2'] + 1; $arrayResults[$key]['column3'] = $arrayResults[$key]['column3'] + $objResult->fields['shopColumn3']; $arrayResults[$key]['column4'] = $arrayResults[$key]['column4'] + Currency::getDefaultCurrencyPrice($objResult->fields['sum']); $sumColumn2 = $sumColumn2 + 1; $sumColumn3 = $sumColumn3 + $objResult->fields['shopColumn3']; $sumColumn4 = $sumColumn4 + Currency::getDefaultCurrencyPrice($objResult->fields['sum']); $objResult->MoveNext(); } krsort($arrayResults, SORT_NUMERIC); } $objTemplate->setCurrentBlock('statisticRow'); $i = 0; if (is_array($arrayResults)) { foreach ($arrayResults as $entry) { $objTemplate->setVariable(array('SHOP_ROWCLASS' => 'row' . (++$i % 2 + 1), 'SHOP_COLUMN_1' => $entry['column1'], 'SHOP_COLUMN_2' => $entry['column2'], 'SHOP_COLUMN_3' => $entry['column3'], 'SHOP_COLUMN_4' => Currency::formatPrice($entry['column4']) . ' ' . Currency::getDefaultCurrencySymbol())); $objTemplate->parse('statisticRow'); } } $query_currency = "\n SELECT currency_id, sum,\n DATE_FORMAT(date_time, '%m') AS month,\n DATE_FORMAT(date_time, '%Y') AS year\n FROM " . DBPREFIX . "module_shop" . MODULE_INDEX . "_orders\n WHERE status=" . Order::STATUS_CONFIRMED . "\n OR status=" . Order::STATUS_COMPLETED . "\n ORDER BY date_time DESC"; $objResult = $objDatabase->Execute($query_currency); if (!$objResult) { return Order::errorHandler(); } $totalSoldProducts = 0; $query_totalproducts = "\n SELECT sum(A.quantity) AS shopTotalSoldProducts\n FROM " . DBPREFIX . "module_shop" . MODULE_INDEX . "_order_items AS A,\n " . DBPREFIX . "module_shop" . MODULE_INDEX . "_orders AS B\n WHERE A.order_id=B.id\n AND ( B.status=" . Order::STATUS_CONFIRMED . "\n OR B.status=" . Order::STATUS_COMPLETED . ")"; $objResult = $objDatabase->SelectLimit($query_totalproducts, 1); if ($objResult) { if (!$objResult->EOF) { $totalSoldProducts = $objResult->fields['shopTotalSoldProducts']; $objResult->MoveNext(); } } $totalOrderSum = 0; $totalOrders = 0; $bestMonthSum = 0; $bestMonthDate = ''; $arrShopMonthSum = array(); $objResult = $objDatabase->Execute($query); while (!$objResult->EOF) { $orderSum = Currency::getDefaultCurrencyPrice($objResult->fields['sum']); if (!isset($arrShopMonthSum[$objResult->fields['year']][$objResult->fields['month']])) { $arrShopMonthSum[$objResult->fields['year']][$objResult->fields['month']] = 0; } $arrShopMonthSum[$objResult->fields['year']][$objResult->fields['month']] += $orderSum; $totalOrderSum += $orderSum; $totalOrders++; $objResult->MoveNext(); } $months = explode(',', $_ARRAYLANG['TXT_MONTH_ARRAY']); foreach ($arrShopMonthSum as $year => $arrMonth) { foreach ($arrMonth as $month => $sum) { if ($bestMonthSum < $sum) { $bestMonthSum = $sum; $bestMonthDate = $months[$month - 1] . ' ' . $year; } } } $objTemplate->setVariable(array('SHOP_ROWCLASS' => 'row' . (++$i % 2 + 1), 'SHOP_TOTAL_SUM' => Currency::formatPrice($totalOrderSum) . ' ' . Currency::getDefaultCurrencySymbol(), 'SHOP_MONTH' => $bestMonthDate, 'SHOP_MONTH_SUM' => Currency::formatPrice($bestMonthSum) . ' ' . Currency::getDefaultCurrencySymbol(), 'SHOP_TOTAL_ORDERS' => $totalOrders, 'SHOP_SOLD_ARTICLES' => $totalSoldProducts, 'SHOP_SUM_COLUMN_2' => $sumColumn2, 'SHOP_SUM_COLUMN_3' => $sumColumn3, 'SHOP_SUM_COLUMN_4' => Currency::formatPrice($sumColumn4) . ' ' . Currency::getDefaultCurrencySymbol())); return true; }