/formtools/modules/export_manager/global/code/export_groups.php

https://bitbucket.org/lihorne/tedxuw · PHP · 329 lines · 213 code · 60 blank · 56 comment · 12 complexity · a2ecaca30020720898f675edce19e14d MD5 · raw file

  1. <?php
  2. /**
  3. * This file defines all functions for managing export groups within the Export Manager module.
  4. *
  5. * @copyright Encore Web Studios 2011
  6. * @author Encore Web Studios <formtools@encorewebstudios.com>
  7. */
  8. // -------------------------------------------------------------------------------------------------
  9. /**
  10. * Returns all information about an export type group.
  11. *
  12. * @param integer $export_group_id
  13. */
  14. function exp_get_export_group($export_group_id)
  15. {
  16. global $g_table_prefix;
  17. $query = mysql_query("
  18. SELECT *
  19. FROM {$g_table_prefix}module_export_groups
  20. WHERE export_group_id = $export_group_id
  21. ");
  22. $export_group_info = mysql_fetch_assoc($query);
  23. // get any custom list of clients, if this is a Private export type
  24. $query = mysql_query("
  25. SELECT account_id
  26. FROM {$g_table_prefix}module_export_group_clients
  27. WHERE export_group_id = $export_group_id
  28. ");
  29. $account_ids = array();
  30. while ($row = mysql_fetch_assoc($query))
  31. $account_ids[] = $row["account_id"];
  32. $export_group_info["client_ids"] = $account_ids;
  33. return $export_group_info;
  34. }
  35. /**
  36. * Returns an array of all export type groups in the database.
  37. *
  38. * @return array
  39. */
  40. function exp_get_export_groups()
  41. {
  42. global $g_table_prefix;
  43. $query = mysql_query("
  44. SELECT *
  45. FROM {$g_table_prefix}module_export_groups
  46. ORDER BY list_order
  47. ");
  48. $infohash = array();
  49. while ($field = mysql_fetch_assoc($query))
  50. {
  51. $export_group_id = $field["export_group_id"];
  52. $field["num_export_types"] = exp_get_num_export_types($export_group_id);
  53. $infohash[] = $field;
  54. }
  55. return $infohash;
  56. }
  57. /**
  58. * Adds a new export type group to the database.
  59. *
  60. * @param array $info
  61. */
  62. function exp_add_export_group($info)
  63. {
  64. global $g_table_prefix, $L;
  65. $info = ft_sanitize($info);
  66. $group_name = $info["group_name"];
  67. $icon = $info["icon"];
  68. $visibility = $info["visibility"];
  69. // get the next highest order count
  70. $query = mysql_query("SELECT count(*) as c FROM {$g_table_prefix}module_export_groups");
  71. $result = mysql_fetch_assoc($query);
  72. $order = $result["c"] + 1;
  73. // define the default options
  74. $access_type = "admin";
  75. $action = "new_window";
  76. $action_button_text = "{\$LANG.word_display}";
  77. mysql_query("
  78. INSERT INTO {$g_table_prefix}module_export_groups (group_name, access_type, visibility,
  79. icon, action, action_button_text, list_order)
  80. VALUES ('$group_name', '$access_type', '$visibility', '$icon', '$action', '$action_button_text',
  81. $order)
  82. ");
  83. return array(true, $L["notify_export_group_added"]);
  84. }
  85. /**
  86. * Updates an export type group.
  87. *
  88. * @param array $info
  89. * @return array
  90. */
  91. function exp_update_export_group($info)
  92. {
  93. global $g_table_prefix, $L;
  94. $info = ft_sanitize($info);
  95. $export_group_id = $info["export_group_id"];
  96. $visibility = $info["visibility"];
  97. $group_name = $info["group_name"];
  98. $icon = $info["icon"];
  99. $action = $info["action"];
  100. $action_button_text = $info["action_button_text"];
  101. $popup_height = $info["popup_height"];
  102. $popup_width = $info["popup_width"];
  103. $headers = isset($info["headers"]) ? $info["headers"] : "";
  104. $smarty_template = $info["smarty_template"];
  105. mysql_query("
  106. UPDATE {$g_table_prefix}module_export_groups
  107. SET visibility = '$visibility',
  108. group_name = '$group_name',
  109. icon = '$icon',
  110. action = '$action',
  111. action_button_text = '$action_button_text',
  112. popup_height = '$popup_height',
  113. popup_width = '$popup_width',
  114. headers = '$headers',
  115. smarty_template = '$smarty_template'
  116. WHERE export_group_id = $export_group_id
  117. ");
  118. return array(true, $L["notify_export_group_updated"]);
  119. }
  120. function exp_update_export_group_permissions($info)
  121. {
  122. global $g_table_prefix, $L;
  123. $export_group_id = $info["export_group_id"];
  124. $access_type = $info["access_type"];
  125. $form_view_mapping = $info["form_view_mapping"];
  126. $selected_client_ids = (isset($info["selected_client_ids"])) ? $info["selected_client_ids"] : array();
  127. $forms_and_views = "";
  128. if ($form_view_mapping != "all") {
  129. $form_ids = (isset($info["form_ids"])) ? $info["form_ids"] : array();
  130. $view_ids = (isset($info["view_ids"])) ? $info["view_ids"] : array();
  131. $forms_and_views = implode(",", $form_ids) . "|" . implode(",", $view_ids);
  132. }
  133. mysql_query("
  134. UPDATE {$g_table_prefix}module_export_groups
  135. SET access_type = '$access_type',
  136. form_view_mapping = '$form_view_mapping',
  137. forms_and_views = '$forms_and_views'
  138. WHERE export_group_id = $export_group_id
  139. ");
  140. // now update the list of clients that may have been manually assigned to this (private) export group. If
  141. // it private, that's cool! Just clear out the old dud data
  142. mysql_query("DELETE FROM {$g_table_prefix}module_export_group_clients WHERE export_group_id = $export_group_id");
  143. foreach ($selected_client_ids as $account_id)
  144. {
  145. mysql_query("
  146. INSERT INTO {$g_table_prefix}module_export_group_clients (export_group_id, account_id)
  147. VALUES ($export_group_id, $account_id)
  148. ");
  149. }
  150. return array(true, $L["notify_export_group_updated"]);
  151. }
  152. /**
  153. * Deletes an export group and any associated Export types.
  154. *
  155. * @param integer $export_group_id
  156. */
  157. function exp_delete_export_group($export_group_id)
  158. {
  159. global $g_table_prefix, $L;
  160. mysql_query("DELETE FROM {$g_table_prefix}module_export_groups WHERE export_group_id = $export_group_id");
  161. mysql_query("DELETE FROM {$g_table_prefix}module_export_types WHERE export_group_id = $export_group_id");
  162. mysql_query("DELETE FROM {$g_table_prefix}module_export_group_clients WHERE export_group_id = $export_group_id");
  163. // now make sure there aren't any gaps in the export group ordering
  164. exp_check_export_group_order();
  165. return array(true, $L["notify_export_group_deleted"]);
  166. }
  167. /**
  168. * This can be called after deleting an export group, or whenever is needed to ensure that the
  169. * order of the export groups are consistent, accurate & don't have any gaps.
  170. */
  171. function exp_check_export_group_order()
  172. {
  173. global $g_table_prefix;
  174. $query = mysql_query("
  175. SELECT export_group_id
  176. FROM {$g_table_prefix}module_export_groups
  177. ORDER BY list_order ASC
  178. ");
  179. $ordered_groups = array();
  180. while ($row = mysql_fetch_assoc($query))
  181. $ordered_groups[] = $row["export_group_id"];
  182. $order = 1;
  183. foreach ($ordered_groups as $export_group_id)
  184. {
  185. mysql_query("
  186. UPDATE {$g_table_prefix}module_export_groups
  187. SET list_order = $order
  188. WHERE export_group_id = $export_group_id
  189. ");
  190. $order++;
  191. }
  192. }
  193. /**
  194. * Called by the administrator on the Export Type Groups page. It reorders the export groups, which determines
  195. * the order in which they appear in the client and admin pages.
  196. *
  197. * @param array $info
  198. */
  199. function exp_reorder_export_groups($info)
  200. {
  201. global $g_table_prefix, $L;
  202. $sortable_id = $info["sortable_id"];
  203. $export_group_ids = explode(",", $info["{$sortable_id}_sortable__rows"]);
  204. $order = 1;
  205. foreach ($export_group_ids as $export_group_id)
  206. {
  207. mysql_query("
  208. UPDATE {$g_table_prefix}module_export_groups
  209. SET list_order = $order
  210. WHERE export_group_id = $export_group_id
  211. ");
  212. $order++;
  213. }
  214. return array(true, $L["notify_export_group_reordered"]);
  215. }
  216. /**
  217. * This returns the IDs of the previous and next export groups, for the << prev, next >> navigation.
  218. *
  219. * @param integer $export_group_id
  220. * @return hash prev_id => the previous export group ID (or empty string)
  221. * next_id => the next export group ID (or empty string)
  222. */
  223. function ft_get_export_group_prev_next_links($export_group_id)
  224. {
  225. global $g_table_prefix;
  226. $query = mysql_query("
  227. SELECT export_group_id
  228. FROM {$g_table_prefix}module_export_groups
  229. ORDER BY list_order ASC
  230. ");
  231. $sorted_ids = array();
  232. while ($row = mysql_fetch_assoc($query))
  233. {
  234. $sorted_ids[] = $row["export_group_id"];
  235. }
  236. $current_index = array_search($export_group_id, $sorted_ids);
  237. $return_info = array("prev_id" => "", "next_id" => "");
  238. if ($current_index === 0)
  239. {
  240. if (count($sorted_ids) > 1)
  241. $return_info["next_id"] = $sorted_ids[$current_index+1];
  242. }
  243. else if ($current_index === count($sorted_ids)-1)
  244. {
  245. if (count($sorted_ids) > 1)
  246. $return_info["prev_id"] = $sorted_ids[$current_index-1];
  247. }
  248. else
  249. {
  250. $return_info["prev_id"] = $sorted_ids[$current_index-1];
  251. $return_info["next_id"] = $sorted_ids[$current_index+1];
  252. }
  253. return $return_info;
  254. }
  255. function exp_deserialized_export_group_mapping($str)
  256. {
  257. $form_ids = array();
  258. $view_ids = array();
  259. if (!empty($str))
  260. {
  261. list($form_ids, $view_ids) = explode("|", $str);
  262. $form_ids = explode(",", $form_ids);
  263. $view_ids = explode(",", $view_ids);
  264. }
  265. return array(
  266. "form_ids" => $form_ids,
  267. "view_ids" => $view_ids
  268. );
  269. }