PageRenderTime 42ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 1ms

/admin444/tabs/AdminModulesPositions.php

http://marocmall.googlecode.com/
PHP | 315 lines | 268 code | 23 blank | 24 comment | 32 complexity | 7698fd44017b788bfb1c4c3f5277b51d MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /**
  3. * Modules positions tab for admin panel, AdminModulesPositions.php
  4. * @category admin
  5. *
  6. * @author PrestaShop <support@prestashop.com>
  7. * @copyright PrestaShop
  8. * @license http://www.opensource.org/licenses/osl-3.0.php Open-source licence 3.0
  9. * @version 1.3
  10. *
  11. */
  12. include_once(PS_ADMIN_DIR.'/../classes/AdminTab.php');
  13. class AdminModulesPositions extends AdminTab
  14. {
  15. private $displayKey = 0;
  16. public function postProcess()
  17. {
  18. global $currentIndex;
  19. // Getting key value for display
  20. if (Tools::getValue('show_modules') AND strval(Tools::getValue('show_modules')) != 'all')
  21. $this->displayKey = intval(Tools::getValue('show_modules'));
  22. // Change position in hook
  23. if (array_key_exists('changePosition', $_GET))
  24. {
  25. if ($this->tabAccess['edit'] === '1')
  26. {
  27. $id_module = intval(Tools::getValue('id_module'));
  28. $id_hook = intval(Tools::getValue('id_hook'));
  29. $module = Module::getInstanceById($id_module);
  30. if (Validate::isLoadedObject($module))
  31. {
  32. $module->updatePosition($id_hook, intval(Tools::getValue('direction')));
  33. Tools::redirectAdmin($currentIndex.($this->displayKey ? '&show_modules='.$this->displayKey : '').'&token='.$this->token);
  34. }
  35. else
  36. $this->_errors[] = Tools::displayError('module cannot be loaded');
  37. }
  38. else
  39. $this->_errors[] = Tools::displayError('You do not have permission to edit anything here.');
  40. }
  41. // Add new module in hook
  42. elseif (Tools::isSubmit('submitAddToHook'))
  43. {
  44. if ($this->tabAccess['add'] === '1')
  45. {
  46. // Getting vars...
  47. $id_module = intval(Tools::getValue('id_module'));
  48. $module = Module::getInstanceById($id_module);
  49. $id_hook = intval(Tools::getValue('id_hook'));
  50. $hook = new Hook($id_hook);
  51. $excepts = explode(',', str_replace(' ', '', Tools::getValue('exceptions')));
  52. // Checking vars...
  53. foreach ($excepts AS $except)
  54. if (!Validate::isFileName($except))
  55. $this->_errors[] = Tools::displayError('no valid value for field exceptions');
  56. if (!$id_module OR !Validate::isLoadedObject($module))
  57. $this->_errors[] = Tools::displayError('module cannot be loaded');
  58. elseif (!$id_hook OR !Validate::isLoadedObject($hook))
  59. $this->_errors[] = Tools::displayError('hook cannot be loaded');
  60. elseif (Hook::getModuleFromHook($id_hook, $id_module))
  61. $this->_errors[] = Tools::displayError('this module is already transplanted to this hook');
  62. // Adding vars...
  63. elseif (!$module->registerHook($hook->name))
  64. $this->_errors[] = Tools::displayError('an error occurred while transplanting module to hook');
  65. elseif (!$module->registerExceptions($id_hook, $excepts))
  66. $this->_errors[] = Tools::displayError('an error occurred while transplanting module to hook');
  67. else
  68. Tools::redirectAdmin($currentIndex.'&conf=16'.($this->displayKey ? '&show_modules='.$this->displayKey : '').'&token='.$this->token);
  69. }
  70. else
  71. $this->_errors[] = Tools::displayError('You do not have permission to add anything here.');
  72. }
  73. // Edit module from hook
  74. elseif (Tools::isSubmit('submitEditGraft'))
  75. {
  76. if ($this->tabAccess['add'] === '1')
  77. {
  78. // Getting vars...
  79. $id_module = intval(Tools::getValue('id_module'));
  80. $module = Module::getInstanceById($id_module);
  81. $id_hook = intval(Tools::getValue('id_hook'));
  82. $hook = new Hook($id_hook);
  83. $excepts = explode(',', str_replace(' ', '', Tools::getValue('exceptions')));
  84. // Checking vars...
  85. foreach ($excepts AS $except)
  86. if (!Validate::isFileName($except))
  87. $this->_errors[] = Tools::displayError('no valid value for field exceptions');
  88. if (!$id_module OR !Validate::isLoadedObject($module))
  89. $this->_errors[] = Tools::displayError('module cannot be loaded');
  90. elseif (!$id_hook OR !Validate::isLoadedObject($hook))
  91. $this->_errors[] = Tools::displayError('hook cannot be loaded');
  92. // Adding vars...
  93. if (!$module->editExceptions($id_hook, $excepts))
  94. $this->_errors[] = Tools::displayError('an error occurred while transplanting module to hook');
  95. else
  96. Tools::redirectAdmin($currentIndex.'&conf=16'.($this->displayKey ? '&show_modules='.$this->displayKey : '').'&token='.$this->token);
  97. }
  98. else
  99. $this->_errors[] = Tools::displayError('You do not have permission to add anything here.');
  100. }
  101. // Delete module from hook
  102. elseif (array_key_exists('deleteGraft', $_GET))
  103. {
  104. if ($this->tabAccess['delete'] === '1')
  105. {
  106. $id_module = intval(Tools::getValue('id_module'));
  107. $module = Module::getInstanceById($id_module);
  108. $id_hook = intval(Tools::getValue('id_hook'));
  109. $hook = new Hook($id_hook);
  110. if (!Validate::isLoadedObject($module))
  111. $this->_errors[] = Tools::displayError('module cannot be loaded');
  112. elseif (!$id_hook OR !Validate::isLoadedObject($hook))
  113. $this->_errors[] = Tools::displayError('hook cannot be loaded');
  114. else
  115. {
  116. if (!$module->unregisterHook($id_hook) OR !$module->unregisterExceptions($id_hook))
  117. $this->_errors[] = Tools::displayError('an error occurred while deleting module from hook');
  118. else
  119. Tools::redirectAdmin($currentIndex.'&conf=17'.($this->displayKey ? '&show_modules='.$this->displayKey : '').'&token='.$this->token);
  120. }
  121. }
  122. else
  123. $this->_errors[] = Tools::displayError('You do not have permission to delete here.');
  124. }
  125. }
  126. public function display()
  127. {
  128. if (array_key_exists('addToHook', $_GET) OR array_key_exists('editGraft', $_GET) OR (Tools::isSubmit('submitAddToHook') AND $this->_errors))
  129. $this->displayForm();
  130. else
  131. $this->displayList();
  132. }
  133. public function displayList()
  134. {
  135. global $currentIndex;
  136. echo '
  137. <script type="text/javascript" src="../js/jquery/jquery.tablednd_0_5.js"></script>
  138. <script type="text/javascript">
  139. var token = \''.$this->token.'\';
  140. var come_from = \'AdminModulesPositions\';
  141. </script>
  142. <script type="text/javascript" src="../js/admin-dnd.js"></script>
  143. ';
  144. echo '<a href="'.$currentIndex.'&addToHook'.($this->displayKey ? '&show_modules='.$this->displayKey : '').'&token='.$this->token.'"><img src="../img/admin/add.gif" border="0" /> <b>'.$this->l('Transplant a module').'</b></a><br /><br />';
  145. // Print select list
  146. echo '
  147. <form>
  148. '.$this->l('Show').' :
  149. <select id="show_modules" onChange="autoUrl(\'show_modules\', \''.$currentIndex.'&token='.$this->token.'&show_modules=\')">
  150. <option value="all">'.$this->l('All modules').'&nbsp;</option>
  151. <option>---------------</option>';
  152. $modules = Module::getModulesInstalled();
  153. foreach ($modules AS $module)
  154. if ($tmpInstance = Module::getInstanceById(intval($module['id_module'])))
  155. $cm[$tmpInstance->displayName] = $tmpInstance;
  156. ksort($cm);
  157. foreach ($cm AS $module)
  158. echo '
  159. <option value="'.intval($module->id).'" '.($this->displayKey == $module->id ? 'selected="selected" ' : '').'>'.$module->displayName.'</option>';
  160. echo '
  161. </select><br /><br />
  162. <input type="checkbox" id="hook_position" onclick="autoUrlNoList(\'hook_position\', \''.$currentIndex.'&token='.$this->token.'&show_modules='.intval(Tools::getValue('show_modules')).'&hook_position=\')" '.(Tools::getValue('hook_position') ? 'checked="checked" ' : '').' />&nbsp;<label class="t" for="hook_position">'.$this->l('Display non-positionnable hook').'</label>
  163. </form>';
  164. // Print hook list
  165. $irow = 0;
  166. $hooks = Hook::getHooks(!intval(Tools::getValue('hook_position')));
  167. foreach ($hooks AS $hook)
  168. {
  169. $modules = array();
  170. if (!$this->displayKey)
  171. $modules = Hook::getModulesFromHook($hook['id_hook']);
  172. elseif ($res = Hook::getModuleFromHook($hook['id_hook'], $this->displayKey))
  173. $modules[0] = $res;
  174. $nbModules = sizeof($modules);
  175. echo '
  176. <a name="'.$hook['name'].'"/>
  177. <table cellpadding="0" cellspacing="0" class="table width3 space'.($nbModules >= 2? ' tableDnD' : '' ).'" id="'.$hook['id_hook'].'">
  178. <tr class="nodrag nodrop"><th colspan="4">'.$hook['title'].' - <span style="color: red">'.$nbModules.'</span> '.(($nbModules > 1) ? $this->l('modules') : $this->l('module'));
  179. if (!empty($hook['description']))
  180. echo '&nbsp;<span style="font-size:0.8em; font-weight: normal">['.$hook['description'].']</span>';
  181. echo ' <sub style="color:grey;"><i>('.$this->l('Technical name: ').$hook['name'].')</i></sub></th></tr>';
  182. // Print modules list
  183. if ($nbModules)
  184. {
  185. $instances = array();
  186. foreach ($modules AS $module)
  187. if ($tmpInstance = Module::getInstanceById(intval($module['id_module'])))
  188. $instances[$tmpInstance->getPosition($hook['id_hook'])] = $tmpInstance;
  189. ksort($instances);
  190. foreach ($instances AS $position => $instance)
  191. {
  192. echo '
  193. <tr id="'.$hook['id_hook'].'_'.$instance->id.'"'.($irow++ % 2 ? ' class="alt_row"' : '').' style="height: 42px;">';
  194. if (!$this->displayKey)
  195. {
  196. echo '
  197. <td class="positions" width="40">'.intval($position).'</td>
  198. <td'.($nbModules >= 2? ' class="dragHandle"' : '').' id="td_'.$hook['id_hook'].'_'.$instance->id.'" width="40">
  199. <a'.($position == 1 ? ' style="display: none;"' : '' ).' href="'.$currentIndex.'&id_module='.$instance->id.'&id_hook='.$hook['id_hook'].'&direction=0&token='.$this->token.'&changePosition='.rand().'#'.$hook['name'].'"><img src="../img/admin/up.gif" alt="'.$this->l('Up').'" title="'.$this->l('Up').'" /></a><br />
  200. <a '.($position == sizeof($instances) ? ' style="display: none;"' : '').'href="'.$currentIndex.'&id_module='.$instance->id.'&id_hook='.$hook['id_hook'].'&direction=1&token='.$this->token.'&changePosition='.rand().'#'.$hook['name'].'"><img src="../img/admin/down.gif" alt="'.$this->l('Down').'" title="'.$this->l('Down').'" /></a>
  201. </td>
  202. <td style="padding-left: 10px;">
  203. ';
  204. }
  205. else
  206. echo '<td style="padding-left: 10px;" colspan="3">';
  207. echo '
  208. <img src="../modules/'.$instance->name.'/logo.gif" alt="'.stripslashes($instance->name).'" /> <strong>'.stripslashes($instance->displayName).'</strong>
  209. '.($instance->version ? ' v'.(intval($instance->version) == $instance->version? sprintf('%.1f', $instance->version) : floatval($instance->version)) : '').'<br />'.$instance->description.'
  210. </td>
  211. <td width="40">
  212. <a href="'.$currentIndex.'&id_module='.$instance->id.'&id_hook='.$hook['id_hook'].'&editGraft'.($this->displayKey ? '&show_modules='.$this->displayKey : '').'&token='.$this->token.'"><img src="../img/admin/edit.gif" border="0" alt="'.$this->l('Edit').'" title="'.$this->l('Edit').'" /></a>
  213. <a href="'.$currentIndex.'&id_module='.$instance->id.'&id_hook='.$hook['id_hook'].'&deleteGraft'.($this->displayKey ? '&show_modules='.$this->displayKey : '').'&token='.$this->token.'"><img src="../img/admin/delete.gif" border="0" alt="'.$this->l('Delete').'" title="'.$this->l('Delete').'" /></a>
  214. </td>
  215. </tr>';
  216. }
  217. } else
  218. echo '<tr><td colspan="4">'.$this->l('No module for this hook').'</td></tr>';
  219. echo '</table>';
  220. }
  221. }
  222. public function displayForm($isMainTab = true)
  223. {
  224. global $currentIndex;
  225. parent::displayForm();
  226. $id_module = intval(Tools::getValue('id_module'));
  227. $id_hook = intval(Tools::getValue('id_hook'));
  228. if ($id_module AND $id_hook AND Tools::isSubmit('editGraft'))
  229. {
  230. $slModule = Module::getInstanceById($id_module);
  231. $exceptsList = $slModule->getExceptions($id_hook);
  232. $excepts = '';
  233. foreach ($exceptsList as $key => $except)
  234. $excepts .= ($key ? ',' : '').$except['file_name'];
  235. }
  236. $excepts = strval(Tools::getValue('exceptions', ((isset($slModule) AND Validate::isLoadedObject($slModule)) ? $excepts : '')));
  237. $modules = Module::getModulesInstalled(0);
  238. $instances = array();
  239. foreach ($modules AS $module)
  240. if ($tmpInstance = Module::getInstanceById($module['id_module']))
  241. $instances[$tmpInstance->displayName] = $tmpInstance;
  242. ksort($instances);
  243. $modules = $instances;
  244. $hooks = Hook::getHooks(0);
  245. echo '
  246. <form action="'.$currentIndex.'&token='.$this->token.'" method="post">';
  247. if ($this->displayKey)
  248. echo '<input type="hidden" name="show_modules" value="'.$this->displayKey.'" />';
  249. echo '<fieldset class="width3" style="width:700px;"><legend><img src="../img/t/AdminModulesPositions.gif" />'.$this->l('Transplant a module').'</legend>
  250. <label>'.$this->l('Module').' :</label>
  251. <div class="margin-form">
  252. <select name="id_module"'.(Tools::isSubmit('editGraft') ? ' disabled="disabled"' : '').'>';
  253. foreach ($modules AS $module)
  254. echo '
  255. <option value="'.$module->id.'" '.($id_module == $module->id ? 'selected="selected" ' : '').'>'.stripslashes($module->displayName).'</option>';
  256. echo '
  257. </select><sup> *</sup>
  258. </div>
  259. <label>'.$this->l('Hook into').' :</label>
  260. <div class="margin-form">
  261. <select name="id_hook"'.(Tools::isSubmit('editGraft') ? ' disabled="disabled"' : '').'>';
  262. foreach ($hooks AS $hook)
  263. echo '
  264. <option value="'.$hook['id_hook'].'" '.($id_hook == $hook['id_hook'] ? 'selected="selected" ' : '').'>'.$hook['title'].'</option>';
  265. echo '
  266. </select><sup> *</sup>
  267. </div>
  268. <label>'.$this->l('Exceptions').' :</label>
  269. <div class="margin-form">
  270. <input type="text" name="exceptions" size="40" '.(!empty($excepts) ? 'value="'.$excepts.'"' : '').'><br />Ex: identity.php, history.php, order.php, product.php<br /><br />
  271. '.$this->l('Please specify those files in which you do not want the module to be displayed').'.<br />
  272. '.$this->l('These files are located in your base directory').', '.$this->l('e.g., ').' <b>identity.php</b>.<br />
  273. '.$this->l('Please type each filename separated by a comma').'.
  274. <br /><br />
  275. </div>
  276. <div class="margin-form">
  277. ';
  278. if (Tools::isSubmit('editGraft'))
  279. {
  280. echo '
  281. <input type="hidden" name="id_module" value="'.$id_module.'" />
  282. <input type="hidden" name="id_hook" value="'.$id_hook.'" />';
  283. }
  284. echo '
  285. <input type="submit" value="'.$this->l('Save').'" name="'.(Tools::isSubmit('editGraft') ? 'submitEditGraft' : 'submitAddToHook').'" class="button" />
  286. </div>
  287. <div class="small"><sup>*</sup> '.$this->l('Required field').'</div>
  288. </fieldset>
  289. </form>';
  290. }
  291. }
  292. ?>