/gadgets/Menu/Actions/Admin/Ajax.php

https://github.com/jaws-project/jaws · PHP · 365 lines · 234 code · 33 blank · 98 comment · 26 complexity · 46f2e38b09d23356b2f9419d84724f4b MD5 · raw file

  1. <?php
  2. /**
  3. * Menu AJAX API
  4. *
  5. * @category Ajax
  6. * @package Menu
  7. */
  8. class Menu_Actions_Admin_Ajax extends Jaws_Gadget_Action
  9. {
  10. /**
  11. * Get all menus and groups data
  12. *
  13. * @access public
  14. * @return mixed Data array or False on error
  15. */
  16. function GetMenusTrees()
  17. {
  18. $gadget = $this->gadget->action->loadAdmin('Menu');
  19. $data = $gadget->GetMenusTrees();
  20. unset($gadget);
  21. if (Jaws_Error::IsError($data)) {
  22. return false;
  23. }
  24. return $data;
  25. }
  26. /**
  27. * Returns the group form
  28. *
  29. * @access public
  30. * @return string XHTML template of groupForm
  31. */
  32. function GetGroupUI()
  33. {
  34. $gadget = $this->gadget->action->loadAdmin('Menu');
  35. return $gadget->GetGroupUI();
  36. }
  37. /**
  38. * Returns the menu form
  39. *
  40. * @access public
  41. * @return string XHTML template of groupForm
  42. */
  43. function GetMenuUI()
  44. {
  45. $gadget = $this->gadget->action->loadAdmin('Menu');
  46. return $gadget->GetMenuUI();
  47. }
  48. /**
  49. * Get information of a group
  50. *
  51. * @access public
  52. * @return mixed Group information array or False on error
  53. */
  54. function GetGroups()
  55. {
  56. @list($gid) = $this->gadget->request->fetchAll('post');
  57. $model = $this->gadget->model->load('Group');
  58. $groupInfo = $model->GetGroups($gid);
  59. if (Jaws_Error::IsError($groupInfo)) {
  60. return false; //we need to handle errors on ajax
  61. }
  62. return $groupInfo;
  63. }
  64. /**
  65. * Get menu data
  66. *
  67. * @access public
  68. * @return mixed Menu data array or False on error
  69. */
  70. function GetMenu()
  71. {
  72. @list($mid) = $this->gadget->request->fetchAll('post');
  73. $model = $this->gadget->model->load('Menu');
  74. $menu = $model->GetMenu($mid);
  75. if (Jaws_Error::IsError($menu)) {
  76. return false; //we need to handle errors on ajax
  77. }
  78. if (false === @unserialize($menu['url'])) {
  79. $menu['url'] = Jaws_XSS::defilterURL($menu['url']);
  80. }
  81. // menu options
  82. $menu['options'] = @unserialize($menu['options']);
  83. if (!empty($menu['options'])) {
  84. $menu['options'] = http_build_query($menu['options']);
  85. } else {
  86. $menu['options'] = '';
  87. }
  88. return $menu;
  89. }
  90. /**
  91. * Insert group
  92. *
  93. * @access public
  94. * @return array Response array (notice or error)
  95. */
  96. function InsertGroup()
  97. {
  98. $this->gadget->CheckPermission('ManageGroups');
  99. @list($title, $home, $title_view, $view_type, $published) = $this->gadget->request->fetchAll('post');
  100. $model = $this->gadget->model->loadAdmin('Group');
  101. $model->InsertGroup($title, $home, $title_view, $view_type, (bool)$published);
  102. return $this->gadget->session->pop();
  103. }
  104. /**
  105. * Insert menu
  106. *
  107. * @access public
  108. * @return array Response array (notice or error)
  109. */
  110. function InsertMenu()
  111. {
  112. $this->gadget->CheckPermission('ManageMenus');
  113. @list($pid, $gid, $gadget, $permission, $title, $url, $variables, $options, $symbol, $target,
  114. $order, $status, $image
  115. ) = $this->gadget->request->fetchAll('post');
  116. if (is_null($url)) {
  117. $url = serialize($this->gadget->request->fetch('5:array', 'post'));
  118. } else {
  119. // parse & encode given url
  120. $url = Jaws_XSS::filterURL($url);
  121. }
  122. if (is_null($permission)) {
  123. $permission = serialize($this->gadget->request->fetch('3:array', 'post'));
  124. }
  125. if (is_null($variables)) {
  126. $variables = serialize($this->gadget->request->fetch('6:array', 'post'));
  127. }
  128. // parse & serialize menu options
  129. parse_str(Jaws_XSS::filterURL($options), $options);
  130. $options = serialize($options);
  131. $mData = array(
  132. 'pid' => $pid,
  133. 'gid' => $gid,
  134. 'gadget' => $gadget,
  135. 'permission' => $permission,
  136. 'title' => $title,
  137. 'url' => $url,
  138. 'variables' => $variables,
  139. 'options' => $options,
  140. 'symbol' => $symbol,
  141. 'target' => $target,
  142. 'order' => $order,
  143. 'status' => (int)$status,
  144. 'image' => $image
  145. );
  146. $model = $this->gadget->model->loadAdmin('Menu');
  147. $model->InsertMenu($mData);
  148. return $this->gadget->session->pop();
  149. }
  150. /**
  151. * Update group
  152. *
  153. * @access public
  154. * @return array Response array (notice or error)
  155. */
  156. function UpdateGroup()
  157. {
  158. $this->gadget->CheckPermission('ManageGroups');
  159. @list(
  160. $gid, $title, $home, $title_view, $view_type, $published
  161. ) = $this->gadget->request->fetchAll('post');
  162. $model = $this->gadget->model->loadAdmin('Group');
  163. $model->UpdateGroup($gid, $title, $home, $title_view, $view_type, (bool)$published);
  164. return $this->gadget->session->pop();
  165. }
  166. /**
  167. * Update menu
  168. *
  169. * @access public
  170. * @return array Response array (notice or error)
  171. */
  172. function UpdateMenu()
  173. {
  174. $this->gadget->CheckPermission('ManageMenus');
  175. @list($mid, $pid, $gid, $gadget, $permission, $title, $url, $variables, $options, $symbol, $target,
  176. $order, $status, $image
  177. ) = $this->gadget->request->fetchAll('post');
  178. if (is_null($url)) {
  179. $url = serialize($this->gadget->request->fetch('6:array', 'post'));
  180. } else {
  181. // parse & encode given url
  182. $url = Jaws_XSS::filterURL($url);
  183. }
  184. if (is_null($permission)) {
  185. $permission = serialize($this->gadget->request->fetch('4:array', 'post'));
  186. }
  187. if (is_null($variables)) {
  188. $variables = serialize($this->gadget->request->fetch('7:array', 'post'));
  189. }
  190. // parse & serialize menu options
  191. parse_str(Jaws_XSS::filterURL($options), $options);
  192. $options = serialize($options);
  193. $mData = array(
  194. 'pid' => $pid,
  195. 'gid' => $gid,
  196. 'gadget' => $gadget,
  197. 'permission' => $permission,
  198. 'title' => $title,
  199. 'url' => $url,
  200. 'variables' => $variables,
  201. 'options' => $options,
  202. 'symbol' => $symbol,
  203. 'target' => $target,
  204. 'order' => $order,
  205. 'status' => (int)$status,
  206. 'image' => $image
  207. );
  208. $model = $this->gadget->model->loadAdmin('Menu');
  209. $model->UpdateMenu($mid, $mData);
  210. return $this->gadget->session->pop();
  211. }
  212. /**
  213. * Delete an group
  214. *
  215. * @access public
  216. * @return array Response array (notice or error)
  217. */
  218. function DeleteGroup()
  219. {
  220. $this->gadget->CheckPermission('ManageGroups');
  221. @list($gid) = $this->gadget->request->fetchAll('post');
  222. $model = $this->gadget->model->loadAdmin('Group');
  223. $model->DeleteGroup($gid);
  224. return $this->gadget->session->pop();
  225. }
  226. /**
  227. * Delete an menu
  228. *
  229. * @access public
  230. * @return array Response array (notice or error)
  231. */
  232. function DeleteMenu()
  233. {
  234. $this->gadget->CheckPermission('ManageMenus');
  235. @list($mid) = $this->gadget->request->fetchAll('post');
  236. $model = $this->gadget->model->loadAdmin('Menu');
  237. $result = $model->DeleteMenu($mid);
  238. if ($result) {
  239. $this->gadget->session->push($this::t('NOTICE_MENU_DELETED'), RESPONSE_NOTICE);
  240. }
  241. return $this->gadget->session->pop();
  242. }
  243. /**
  244. * Get menu data
  245. *
  246. * @access public
  247. * @return array Menu data array
  248. */
  249. function GetParentMenus()
  250. {
  251. @list($gid, $mid) = $this->gadget->request->fetchAll('post');
  252. $result[] = array('pid'=> 0,
  253. 'title'=>'\\');
  254. $model = $this->gadget->model->loadAdmin('Menu');
  255. $model->GetParentMenus(0, $gid, $mid, $result);
  256. return $result;
  257. }
  258. /**
  259. * function for change gid, pid and order of menus
  260. *
  261. * @access public
  262. * @return array Response array (notice or error)
  263. */
  264. function MoveMenu()
  265. {
  266. $this->gadget->CheckPermission('ManageMenus');
  267. @list($mid, $new_gid, $old_gid, $new_pid, $old_pid,
  268. $new_order, $old_order
  269. ) = $this->gadget->request->fetchAll('post');
  270. $model = $this->gadget->model->loadAdmin('Menu');
  271. $model->MoveMenu($mid, $new_gid, $old_gid, $new_pid, $old_pid, $new_order, $old_order);
  272. return $this->gadget->session->pop();
  273. }
  274. /**
  275. * Get a list of URLs of a gadget
  276. *
  277. * @access public
  278. * @return array URLs array on success or empty array on failure
  279. */
  280. function GetPublicURList()
  281. {
  282. @list($request) = $this->gadget->request->fetchAll('post');
  283. if ($request == 'url') {
  284. $urls[] = array('url' => '/',
  285. 'title' => $this::t('REFERENCES_FREE_LINK'));
  286. $urls[] = array('url' => '',
  287. 'title' => $this::t('REFERENCES_NO_LINK'));
  288. return $urls;
  289. } else {
  290. if (Jaws_Gadget::IsGadgetUpdated($request)) {
  291. $objGadget = Jaws_Gadget::getInstance($request);
  292. if (!Jaws_Error::IsError($objGadget)) {
  293. $links = $objGadget->hook->load('Menu')->Execute();
  294. if (!Jaws_Error::IsError($links)) {
  295. array_unshift(
  296. $links,
  297. array(
  298. 'url' => '/',
  299. 'title' => $this::t('REFERENCES_FREE_LINK')
  300. ),
  301. array(
  302. 'url' => '',
  303. 'title' => $this::t('REFERENCES_NO_LINK')
  304. )
  305. );
  306. foreach ($links as $key => $link) {
  307. if (is_array($link['url'])) {
  308. $links[$key]['url'] = serialize($link['url']);
  309. } else {
  310. $links[$key]['url'] = rawurldecode($link['url']);
  311. }
  312. // serialize variables
  313. if (isset($link['variables'])) {
  314. $links[$key]['variables'] = serialize($link['variables']);
  315. }
  316. // query options
  317. if (isset($link['options'])) {
  318. $links[$key]['options'] = http_build_query($link['options']);
  319. }
  320. // serialize permission
  321. if (isset($link['permission'])) {
  322. $links[$key]['permission'] = serialize($link['permission']);
  323. }
  324. }
  325. return $links;
  326. }
  327. }
  328. }
  329. }
  330. return array();
  331. }
  332. }