PageRenderTime 49ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/controllers/admin/AdminSearchController.php

https://github.com/netplayer/PrestaShop
PHP | 421 lines | 325 code | 40 blank | 56 comment | 94 complexity | a2f687a8f273fefe78ff74d7b5707f3f MD5 | raw file
Possible License(s): CC-BY-SA-3.0, LGPL-2.1, LGPL-3.0
  1. <?php
  2. /*
  3. * 2007-2014 PrestaShop
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@prestashop.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
  18. * versions in the future. If you wish to customize PrestaShop for your
  19. * needs please refer to http://www.prestashop.com for more information.
  20. *
  21. * @author PrestaShop SA <contact@prestashop.com>
  22. * @copyright 2007-2014 PrestaShop SA
  23. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  24. * International Registered Trademark & Property of PrestaShop SA
  25. */
  26. class AdminSearchControllerCore extends AdminController
  27. {
  28. public function __construct()
  29. {
  30. $this->bootstrap = true;
  31. parent::__construct();
  32. }
  33. public function postProcess()
  34. {
  35. $this->context = Context::getContext();
  36. $this->query = trim(Tools::getValue('bo_query'));
  37. $searchType = (int)Tools::getValue('bo_search_type');
  38. /* Handle empty search field */
  39. if (!empty($this->query))
  40. {
  41. if (!$searchType && strlen($this->query) > 1)
  42. $this->searchFeatures();
  43. /* Product research */
  44. if (!$searchType || $searchType == 1)
  45. {
  46. /* Handle product ID */
  47. if ($searchType == 1 && (int)$this->query && Validate::isUnsignedInt((int)$this->query))
  48. if (($product = new Product($this->query)) && Validate::isLoadedObject($product))
  49. Tools::redirectAdmin('index.php?tab=AdminProducts&id_product='.(int)($product->id).'&addproduct'.'&token='.Tools::getAdminTokenLite('AdminProducts'));
  50. /* Normal catalog search */
  51. $this->searchCatalog();
  52. }
  53. /* Customer */
  54. if (!$searchType || $searchType == 2 || $searchType == 6)
  55. {
  56. if (!$searchType || $searchType == 2)
  57. {
  58. /* Handle customer ID */
  59. if ($searchType && (int)$this->query && Validate::isUnsignedInt((int)$this->query))
  60. if (($customer = new Customer($this->query)) && Validate::isLoadedObject($customer))
  61. Tools::redirectAdmin('index.php?tab=AdminCustomers&id_customer='.(int)$customer->id.'&viewcustomer'.'&token='.Tools::getAdminToken('AdminCustomers'.(int)Tab::getIdFromClassName('AdminCustomers').(int)$this->context->employee->id));
  62. /* Normal customer search */
  63. $this->searchCustomer();
  64. }
  65. if ($searchType == 6)
  66. $this->searchIP();
  67. }
  68. /* Order */
  69. if (!$searchType || $searchType == 3)
  70. {
  71. if (Validate::isUnsignedInt(trim($this->query)) && (int)$this->query && ($order = new Order((int)$this->query)) && Validate::isLoadedObject($order))
  72. {
  73. if ($searchType == 3)
  74. Tools::redirectAdmin('index.php?tab=AdminOrders&id_order='.(int)$order->id.'&vieworder'.'&token='.Tools::getAdminTokenLite('AdminOrders'));
  75. else
  76. {
  77. $row = get_object_vars($order);
  78. $row['id_order'] = $row['id'];
  79. $customer = $order->getCustomer();
  80. $row['customer'] = $customer->firstname.' '.$customer->lastname;
  81. $order_state = $order->getCurrentOrderState();
  82. $row['osname'] = $order_state->name[$this->context->language->id];
  83. $this->_list['orders'] = array($row);
  84. }
  85. }
  86. else
  87. {
  88. $orders = Order::getByReference($this->query);
  89. $nb_orders = count($orders);
  90. if ($nb_orders == 1 && $searchType == 3)
  91. Tools::redirectAdmin('index.php?tab=AdminOrders&id_order='.(int)$orders[0]->id.'&vieworder'.'&token='.Tools::getAdminTokenLite('AdminOrders'));
  92. elseif ($nb_orders)
  93. {
  94. $this->_list['orders'] = array();
  95. foreach ($orders as $order)
  96. {
  97. $row = get_object_vars($order);
  98. $row['id_order'] = $row['id'];
  99. $customer = $order->getCustomer();
  100. $row['customer'] = $customer->firstname.' '.$customer->lastname;
  101. $order_state = $order->getCurrentOrderState();
  102. $row['osname'] = $order_state->name[$this->context->language->id];
  103. $this->_list['orders'][] = $row;
  104. }
  105. }
  106. elseif ($searchType == 3)
  107. $this->errors[] = Tools::displayError('No order was found with this ID:').' '.Tools::htmlentitiesUTF8($this->query);
  108. }
  109. }
  110. /* Invoices */
  111. if ($searchType == 4)
  112. {
  113. if (Validate::isOrderInvoiceNumber($this->query) && ($invoice = OrderInvoice::getInvoiceByNumber($this->query)))
  114. Tools::redirectAdmin($this->context->link->getAdminLink('AdminPdf').'&submitAction=generateInvoicePDF&id_order='.(int)($invoice->id_order));
  115. $this->errors[] = Tools::displayError('No invoice was found with this ID:').' '.Tools::htmlentitiesUTF8($this->query);
  116. }
  117. /* Cart */
  118. if ($searchType == 5)
  119. {
  120. if ((int)$this->query && Validate::isUnsignedInt((int)$this->query) && ($cart = new Cart($this->query)) && Validate::isLoadedObject($cart))
  121. Tools::redirectAdmin('index.php?tab=AdminCarts&id_cart='.(int)($cart->id).'&viewcart'.'&token='.Tools::getAdminToken('AdminCarts'.(int)(Tab::getIdFromClassName('AdminCarts')).(int)$this->context->employee->id));
  122. $this->errors[] = Tools::displayError('No cart was found with this ID:').' '.Tools::htmlentitiesUTF8($this->query);
  123. }
  124. /* IP */
  125. // 6 - but it is included in the customer block
  126. /* Module search */
  127. if (!$searchType || $searchType == 7)
  128. {
  129. /* Handle module name */
  130. if ($searchType == 7 && Validate::isModuleName($this->query) AND ($module = Module::getInstanceByName($this->query)) && Validate::isLoadedObject($module))
  131. Tools::redirectAdmin('index.php?tab=AdminModules&tab_module='.$module->tab.'&module_name='.$module->name.'&anchor='.ucfirst($module->name).'&token='.Tools::getAdminTokenLite('AdminModules'));
  132. /* Normal catalog search */
  133. $this->searchModule();
  134. }
  135. }
  136. $this->display = 'view';
  137. }
  138. public function searchIP()
  139. {
  140. if (!ip2long(trim($this->query)))
  141. {
  142. $this->errors[] = Tools::displayError('This is not a valid IP address:').' '.Tools::htmlentitiesUTF8($this->query);
  143. return;
  144. }
  145. $this->_list['customers'] = Customer::searchByIp($this->query);
  146. }
  147. /**
  148. * Search a specific string in the products and categories
  149. *
  150. * @params string $query String to find in the catalog
  151. */
  152. public function searchCatalog()
  153. {
  154. $this->context = Context::getContext();
  155. $this->_list['products'] = Product::searchByName($this->context->language->id, $this->query);
  156. $this->_list['categories'] = Category::searchByName($this->context->language->id, $this->query);
  157. }
  158. /**
  159. * Search a specific name in the customers
  160. *
  161. * @params string $query String to find in the catalog
  162. */
  163. public function searchCustomer()
  164. {
  165. $this->_list['customers'] = Customer::searchByName($this->query);
  166. }
  167. public function searchModule()
  168. {
  169. $this->_list['modules'] = array();
  170. $all_modules = Module::getModulesOnDisk(true, true, Context::getContext()->employee->id);
  171. foreach ($all_modules as $module)
  172. if (stripos($module->name, $this->query) !== false || stripos($module->displayName, $this->query) !== false || stripos($module->description, $this->query) !== false)
  173. {
  174. $module->linkto = 'index.php?tab=AdminModules&tab_module='.$module->tab.'&module_name='.$module->name.'&anchor='.ucfirst($module->name).'&token='.Tools::getAdminTokenLite('AdminModules');
  175. $this->_list['modules'][] = $module;
  176. }
  177. if (!is_numeric(trim($this->query)) && !Validate::isEmail($this->query))
  178. {
  179. $iso_lang = Tools::strtolower(Context::getContext()->language->iso_code);
  180. $iso_country = Tools::strtolower(Country::getIsoById(Configuration::get('PS_COUNTRY_DEFAULT')));
  181. if (($json_content = Tools::file_get_contents('https://api.addons.prestashop.com/'._PS_VERSION_.'/search/'.urlencode($this->query).'/'.$iso_country.'/'.$iso_lang.'/')) != false)
  182. $this->_list['addons'] = Tools::jsonDecode($json_content, true);
  183. }
  184. }
  185. /**
  186. * Search a feature in all store
  187. *
  188. * @params string $query String to find in the catalog
  189. */
  190. public function searchFeatures()
  191. {
  192. $this->_list['features'] = array();
  193. global $_LANGADM;
  194. if ($_LANGADM === null)
  195. return;
  196. $tabs = array();
  197. $key_match = array();
  198. $result = Db::getInstance()->executeS('
  199. SELECT class_name, name
  200. FROM '._DB_PREFIX_.'tab t
  201. INNER JOIN '._DB_PREFIX_.'tab_lang tl ON (t.id_tab = tl.id_tab AND tl.id_lang = '.(int)$this->context->employee->id_lang.')
  202. LEFT JOIN '._DB_PREFIX_.'access a ON (a.id_tab = t.id_tab AND a.id_profile = '.(int)$this->context->employee->id_profile.')
  203. WHERE active = 1
  204. '.($this->context->employee->id_profile != 1 ? 'AND view = 1' : '').
  205. (defined('_PS_HOST_MODE_') ? ' AND t.`hide_host_mode` = 0' : '')
  206. );
  207. foreach ($result as $row)
  208. {
  209. $tabs[strtolower($row['class_name'])] = $row['name'];
  210. $key_match[strtolower($row['class_name'])] = $row['class_name'];
  211. }
  212. foreach (AdminTab::$tabParenting as $key => $value)
  213. {
  214. $value = stripslashes($value);
  215. if (!isset($tabs[strtolower($key)]) || !isset($tabs[strtolower($value)]))
  216. continue;
  217. $tabs[strtolower($key)] = $tabs[strtolower($value)];
  218. $key_match[strtolower($key)] = $key;
  219. }
  220. $this->_list['features'] = array();
  221. foreach ($_LANGADM as $key => $value)
  222. {
  223. if (stripos($value, $this->query) !== false)
  224. {
  225. $value = stripslashes($value);
  226. $key = strtolower(substr($key, 0, -32));
  227. if (in_array($key, array('AdminTab', 'index')))
  228. continue;
  229. // if class name doesn't exists, just ignore it
  230. if (!isset($tabs[$key]))
  231. continue;
  232. if (!isset($this->_list['features'][$tabs[$key]]))
  233. $this->_list['features'][$tabs[$key]] = array();
  234. $this->_list['features'][$tabs[$key]][] = array('link' => Context::getContext()->link->getAdminLink($key_match[$key]), 'value' => Tools::safeOutput($value));
  235. }
  236. }
  237. }
  238. protected function initOrderList()
  239. {
  240. $this->fields_list['orders'] = array(
  241. 'reference' => array('title' => $this->l('Reference'), 'align' => 'center', 'width' => 65),
  242. 'id_order' => array('title' => $this->l('ID'), 'align' => 'center', 'width' => 25),
  243. 'customer' => array('title' => $this->l('Customer')),
  244. 'total_paid_tax_incl' => array('title' => $this->l('Total'), 'width' => 70, 'align' => 'right', 'type' => 'price', 'currency' => true),
  245. 'payment' => array( 'title' => $this->l('Payment'), 'width' => 100),
  246. 'osname' => array('title' => $this->l('Status'), 'width' => 280),
  247. 'date_add' => array('title' => $this->l('Date'), 'width' => 130, 'align' => 'right', 'type' => 'datetime'),
  248. );
  249. }
  250. protected function initCustomerList()
  251. {
  252. $genders_icon = array('default' => 'unknown.gif');
  253. $genders = array(0 => $this->l('?'));
  254. foreach (Gender::getGenders() as $gender)
  255. {
  256. $genders_icon[$gender->id] = '../genders/'.(int)$gender->id.'.jpg';
  257. $genders[$gender->id] = $gender->name;
  258. }
  259. $this->fields_list['customers'] = (array(
  260. 'id_customer' => array('title' => $this->l('ID'), 'align' => 'center', 'width' => 25),
  261. 'id_gender' => array('title' => $this->l('Social title'), 'align' => 'center', 'icon' => $genders_icon, 'list' => $genders, 'width' => 25),
  262. 'firstname' => array('title' => $this->l('First Name'), 'align' => 'left', 'width' => 150),
  263. 'lastname' => array('title' => $this->l('Name'), 'align' => 'left', 'width' => 'auto'),
  264. 'email' => array('title' => $this->l('Email address'), 'align' => 'left', 'width' => 250),
  265. 'birthday' => array('title' => $this->l('Birth date'), 'align' => 'center', 'type' => 'date', 'width' => 75),
  266. 'date_add' => array('title' => $this->l('Registration date'), 'align' => 'center', 'type' => 'date', 'width' => 75),
  267. 'orders' => array('title' => $this->l('Orders'), 'align' => 'center', 'width' => 50),
  268. 'active' => array('title' => $this->l('Enabled'),'align' => 'center','active' => 'status','type' => 'bool', 'width' => 25),
  269. ));
  270. }
  271. protected function initProductList()
  272. {
  273. $this->show_toolbar = false;
  274. $this->fields_list['products'] = array(
  275. 'id_product' => array('title' => $this->l('ID'), 'width' => 25),
  276. 'manufacturer_name' => array('title' => $this->l('Manufacturer'), 'align' => 'center', 'width' => 200),
  277. 'reference' => array('title' => $this->l('Reference'), 'align' => 'center', 'width' => 150),
  278. 'name' => array('title' => $this->l('Name'), 'width' => 'auto'),
  279. 'price_tax_excl' => array('title' => $this->l('Price (tax excl.)'), 'align' => 'right', 'type' => 'price', 'width' => 60),
  280. 'price_tax_incl' => array('title' => $this->l('Price (tax incl.)'), 'align' => 'right', 'type' => 'price', 'width' => 60),
  281. 'active' => array('title' => $this->l('Active'), 'width' => 70, 'active' => 'status', 'align' => 'center', 'type' => 'bool')
  282. );
  283. }
  284. public function setMedia()
  285. {
  286. parent::setMedia();
  287. $this->addJqueryPlugin('highlight');
  288. }
  289. /* Override because we don't want any buttons */
  290. public function initToolbar()
  291. {
  292. }
  293. public function initToolbarTitle()
  294. {
  295. $this->toolbar_title = $this->l('Search results', null, null, false);
  296. }
  297. public function renderView()
  298. {
  299. $this->tpl_view_vars['query'] = Tools::safeOutput($this->query);
  300. $this->tpl_view_vars['show_toolbar'] = true;
  301. if (count($this->errors))
  302. return parent::renderView();
  303. else
  304. {
  305. $nb_results = 0;
  306. foreach ($this->_list as $list)
  307. if ($list != false)
  308. $nb_results += count($list);
  309. $this->tpl_view_vars['nb_results'] = $nb_results;
  310. if (isset($this->_list['features']) && count($this->_list['features']))
  311. $this->tpl_view_vars['features'] = $this->_list['features'];
  312. if (isset($this->_list['categories']) && count($this->_list['categories']))
  313. {
  314. $categories = array();
  315. foreach ($this->_list['categories'] as $category)
  316. $categories[] = getPath($this->context->link->getAdminLink('AdminCategories', false), $category['id_category']);
  317. $this->tpl_view_vars['categories'] = $categories;
  318. }
  319. if (isset($this->_list['products']) && count($this->_list['products']))
  320. {
  321. $view = '';
  322. $this->initProductList();
  323. $helper = new HelperList();
  324. $helper->shopLinkType = '';
  325. $helper->simple_header = true;
  326. $helper->identifier = 'id_product';
  327. $helper->actions = array('edit');
  328. $helper->show_toolbar = false;
  329. $helper->table = 'product';
  330. $helper->currentIndex = $this->context->link->getAdminLink('AdminProducts', false);
  331. $helper->token = Tools::getAdminTokenLite('AdminProducts');
  332. if ($this->_list['products'])
  333. $view = $helper->generateList($this->_list['products'], $this->fields_list['products']);
  334. $this->tpl_view_vars['products'] = $view;
  335. }
  336. if (isset($this->_list['customers']) && count($this->_list['customers']))
  337. {
  338. $view = '';
  339. $this->initCustomerList();
  340. $helper = new HelperList();
  341. $helper->shopLinkType = '';
  342. $helper->simple_header = true;
  343. $helper->identifier = 'id_customer';
  344. $helper->actions = array('edit', 'view');
  345. $helper->show_toolbar = false;
  346. $helper->table = 'customer';
  347. $helper->currentIndex = $this->context->link->getAdminLink('AdminCustomers', false);
  348. $helper->token = Tools::getAdminTokenLite('AdminCustomers');
  349. if ($this->_list['customers'])
  350. {
  351. foreach ($this->_list['customers'] as $key => $val)
  352. $this->_list['customers'][$key]['orders'] = Order::getCustomerNbOrders((int)$val['id_customer']);
  353. $view = $helper->generateList($this->_list['customers'], $this->fields_list['customers']);
  354. }
  355. $this->tpl_view_vars['customers'] = $view;
  356. }
  357. if (isset($this->_list['orders']) && count($this->_list['orders']))
  358. {
  359. $view = '';
  360. $this->initOrderList();
  361. $helper = new HelperList();
  362. $helper->shopLinkType = '';
  363. $helper->simple_header = true;
  364. $helper->identifier = 'id_order';
  365. $helper->actions = array('view');
  366. $helper->show_toolbar = false;
  367. $helper->table = 'order';
  368. $helper->currentIndex = $this->context->link->getAdminLink('AdminOrders', false);
  369. $helper->token = Tools::getAdminTokenLite('AdminOrders');
  370. if ($this->_list['orders'])
  371. $view = $helper->generateList($this->_list['orders'], $this->fields_list['orders']);
  372. $this->tpl_view_vars['orders'] = $view;
  373. }
  374. if (isset($this->_list['modules']) && count($this->_list['modules']))
  375. $this->tpl_view_vars['modules'] = $this->_list['modules'];
  376. if (isset($this->_list['addons']) && count($this->_list['addons']))
  377. $this->tpl_view_vars['addons'] = $this->_list['addons'];
  378. return parent::renderView();
  379. }
  380. }
  381. }