|
| 1 | +<?php |
| 2 | + |
| 3 | + |
| 4 | +namespace FormTools\Modules\ExportManager; |
| 5 | + |
| 6 | +use FormTools\Core; |
| 7 | +use PDO; |
| 8 | + |
| 9 | +class ExportGroups |
| 10 | +{ |
| 11 | + /** |
| 12 | + * Returns all information about an export type group. |
| 13 | + * |
| 14 | + * @param integer $export_group_id |
| 15 | + */ |
| 16 | + public function getExportGroup($export_group_id) |
| 17 | + { |
| 18 | + $db = Core::$db; |
| 19 | + |
| 20 | + $db->query(" |
| 21 | + SELECT * |
| 22 | + FROM {PREFUX}module_export_groups |
| 23 | + WHERE export_group_id = :export_group_id |
| 24 | + "); |
| 25 | + $db->bind("export_group_id", $export_group_id); |
| 26 | + $db->execute(); |
| 27 | + |
| 28 | + $export_group_info = $db->fetch(); |
| 29 | + |
| 30 | + // get any custom list of clients, if this is a Private export type |
| 31 | + $db->query(" |
| 32 | + SELECT account_id |
| 33 | + FROM {PREFIX}module_export_group_clients |
| 34 | + WHERE export_group_id = :export_group_id |
| 35 | + "); |
| 36 | + $db->bind("export_group_id", $export_group_id); |
| 37 | + $db->execute(); |
| 38 | + |
| 39 | + $export_group_info["client_ids"] = $db->fetchAll(PDO::FETCH_COLUMN); |
| 40 | + |
| 41 | + return $export_group_info; |
| 42 | + } |
| 43 | + |
| 44 | + |
| 45 | + /** |
| 46 | + * Returns an array of all export type groups in the database. |
| 47 | + * |
| 48 | + * @return array |
| 49 | + */ |
| 50 | + public static function getExportGroups() |
| 51 | + { |
| 52 | + $db = Core::$db; |
| 53 | + |
| 54 | + $db->query(" |
| 55 | + SELECT * |
| 56 | + FROM {PREFIX}module_export_groups |
| 57 | + ORDER BY list_order |
| 58 | + "); |
| 59 | + $db->execute(); |
| 60 | + $results = $db->fetchAll(); |
| 61 | + |
| 62 | + $infohash = array(); |
| 63 | + foreach ($results as $field) { |
| 64 | + $field["num_export_types"] = ExportGroups::getNumExportTypes($field["export_group_id"]); |
| 65 | + $infohash[] = $field; |
| 66 | + } |
| 67 | + |
| 68 | + return $infohash; |
| 69 | + } |
| 70 | + |
| 71 | + |
| 72 | + /** |
| 73 | + * Adds a new export type group to the database. |
| 74 | + * |
| 75 | + * @param array $info |
| 76 | + */ |
| 77 | + public function addExportGroup($info) |
| 78 | + { |
| 79 | + $db = Core::$db; |
| 80 | + // $L; |
| 81 | + |
| 82 | + // get the next highest order count |
| 83 | + $db->query("SELECT count(*) FROM {PREFIX}module_export_groups"); |
| 84 | + $db->execute(); |
| 85 | + $order = $db->fetch(PDO::FETCH_COLUMN) + 1; |
| 86 | + |
| 87 | + // define the default options |
| 88 | + |
| 89 | + $db->query(" |
| 90 | + INSERT INTO {PREFIX}module_export_groups (group_name, access_type, visibility, icon, action, action_button_text, list_order) |
| 91 | + VALUES (:group_name, :access_type, :visibility, :icon, :action, :action_button_text, :order) |
| 92 | + "); |
| 93 | + $db->bindAll(array( |
| 94 | + "group_name" => $info["group_name"], |
| 95 | + "access_type" => "admin", |
| 96 | + "visibility" => $info["visibility"], |
| 97 | + "icon" => $info["icon"], |
| 98 | + "action" => "new_window", |
| 99 | + "action_button_text" => "{\$LANG.word_display}", |
| 100 | + "order" => $order |
| 101 | + )); |
| 102 | + $db->execute(); |
| 103 | + |
| 104 | + return array(true, $L["notify_export_group_added"]); |
| 105 | + } |
| 106 | + |
| 107 | + |
| 108 | + /** |
| 109 | + * Updates an export type group. |
| 110 | + * |
| 111 | + * @param array $info |
| 112 | + * @return array |
| 113 | + */ |
| 114 | + public function updateExportGroup($info) |
| 115 | + { |
| 116 | + $db = Core::$db; |
| 117 | + |
| 118 | + $db->query(" |
| 119 | + UPDATE {PREFIX}module_export_groups |
| 120 | + SET visibility = :visibility, |
| 121 | + group_name = :group_name, |
| 122 | + icon = :icon, |
| 123 | + action = :action, |
| 124 | + action_button_text = :action_button_text, |
| 125 | + popup_height = :popup_height, |
| 126 | + popup_width = :popup_width, |
| 127 | + headers = :headers, |
| 128 | + smarty_template = :smarty_template |
| 129 | + WHERE export_group_id = :export_group_id |
| 130 | + "); |
| 131 | + $db->bindAll(array( |
| 132 | + "visibility" => $info["visibility"], |
| 133 | + "group_name" => $info["group_name"], |
| 134 | + "icon" => $info["icon"], |
| 135 | + "action" => $info["action"], |
| 136 | + "action_button_text" => $info["action_button_text"], |
| 137 | + "popup_height" => $info["popup_height"], |
| 138 | + "popup_width" => $info["popup_width"], |
| 139 | + "headers" => isset($info["headers"]) ? $info["headers"] : "", |
| 140 | + "smarty_template" => $info["smarty_template"], |
| 141 | + "export_group_id" => $info["export_group_id"] |
| 142 | + )); |
| 143 | + $db->execute(); |
| 144 | + |
| 145 | + return array(true, $L["notify_export_group_updated"]); |
| 146 | + } |
| 147 | + |
| 148 | + |
| 149 | + public function exp_update_export_group_permissions($info) |
| 150 | + { |
| 151 | + $db = Core::$db; |
| 152 | + |
| 153 | + $form_view_mapping = $info["form_view_mapping"]; |
| 154 | + $selected_client_ids = (isset($info["selected_client_ids"])) ? $info["selected_client_ids"] : array(); |
| 155 | + |
| 156 | + $forms_and_views = ""; |
| 157 | + if ($form_view_mapping != "all") { |
| 158 | + $form_ids = (isset($info["form_ids"])) ? $info["form_ids"] : array(); |
| 159 | + $view_ids = (isset($info["view_ids"])) ? $info["view_ids"] : array(); |
| 160 | + $forms_and_views = implode(",", $form_ids) . "|" . implode(",", $view_ids); |
| 161 | + } |
| 162 | + |
| 163 | + $db->query(" |
| 164 | + UPDATE {PREFIX}module_export_groups |
| 165 | + SET access_type = :access_type, |
| 166 | + form_view_mapping = :form_view_mapping, |
| 167 | + forms_and_views = :forms_and_views |
| 168 | + WHERE export_group_id = :export_group_id |
| 169 | + "); |
| 170 | + $db->bindAll(array( |
| 171 | + "access_type" => $info["access_type"], |
| 172 | + "form_view_mapping" => $form_view_mapping, |
| 173 | + "forms_and_views" => $forms_and_views, |
| 174 | + "export_group_id" => $info["export_group_id"] |
| 175 | + )); |
| 176 | + $db->execute(); |
| 177 | + |
| 178 | + // now update the list of clients that may have been manually assigned to this (private) export group. If |
| 179 | + // it private, that's cool! Just clear out the old dud data |
| 180 | + $db->query(" |
| 181 | + DELETE FROM {PREFIX}module_export_group_clients |
| 182 | + WHERE export_group_id = :export_group_id |
| 183 | + "); |
| 184 | + $db->bind("export_group_id", $info["export_group_id"]); |
| 185 | + $db->execute(); |
| 186 | + |
| 187 | + foreach ($selected_client_ids as $account_id) { |
| 188 | + $db->query(" |
| 189 | + INSERT INTO {PREFIX}module_export_group_clients (export_group_id, account_id) |
| 190 | + VALUES (:export_group_id, :account_id) |
| 191 | + "); |
| 192 | + $db->bindAll(array( |
| 193 | + "export_group_id" => $info["export_group_id"], |
| 194 | + "account_id" => $account_id |
| 195 | + )); |
| 196 | + $db->execute(); |
| 197 | + } |
| 198 | + |
| 199 | + return array(true, $L["notify_export_group_updated"]); |
| 200 | + } |
| 201 | + |
| 202 | + |
| 203 | + /** |
| 204 | + * Deletes an export group and any associated Export types. |
| 205 | + * |
| 206 | + * @param integer $export_group_id |
| 207 | + */ |
| 208 | + public function deleteExportGroup($export_group_id) |
| 209 | + { |
| 210 | + // $L |
| 211 | + |
| 212 | + $db->query("DELETE FROM {PREFIX}module_export_groups WHERE export_group_id = :export_group_id"); |
| 213 | + $db->bind("export_group_id", $export_group_id); |
| 214 | + $db->execute(); |
| 215 | + |
| 216 | + $db->query("DELETE FROM {PREFIX}module_export_types WHERE export_group_id = :export_group_id"); |
| 217 | + $db->bind("export_group_id", $export_group_id); |
| 218 | + $db->execute(); |
| 219 | + |
| 220 | + $db->query("DELETE FROM {PREFIX}module_export_group_clients WHERE export_group_id = :export_group_id"); |
| 221 | + $db->bind("export_group_id", $export_group_id); |
| 222 | + $db->execute(); |
| 223 | + |
| 224 | + // now make sure there aren't any gaps in the export group ordering |
| 225 | + ExportGroups::checkExportGroupOrder(); |
| 226 | + |
| 227 | + return array(true, $L["notify_export_group_deleted"]); |
| 228 | + } |
| 229 | + |
| 230 | + |
| 231 | + /** |
| 232 | + * This can be called after deleting an export group, or whenever is needed to ensure that the |
| 233 | + * order of the export groups are consistent, accurate & don't have any gaps. |
| 234 | + */ |
| 235 | + public function checkExportGroupOrder() |
| 236 | + { |
| 237 | + $db = Core::$db; |
| 238 | + |
| 239 | + $db->query(" |
| 240 | + SELECT export_group_id |
| 241 | + FROM {PREFIX}module_export_groups |
| 242 | + ORDER BY list_order ASC |
| 243 | + "); |
| 244 | + $db->execute(); |
| 245 | + $ordered_groups = $db->fetchAll(PDO::FETCH_COLUMN); |
| 246 | + |
| 247 | + $order = 1; |
| 248 | + foreach ($ordered_groups as $export_group_id) { |
| 249 | + $db->query(" |
| 250 | + UPDATE {PREFIX}module_export_groups |
| 251 | + SET list_order = :list_order |
| 252 | + WHERE export_group_id = :export_group_id |
| 253 | + "); |
| 254 | + $db->bindAll(array( |
| 255 | + "list_order" => $order, |
| 256 | + "export_group_id" => $export_group_id |
| 257 | + )); |
| 258 | + $order++; |
| 259 | + } |
| 260 | + } |
| 261 | + |
| 262 | + |
| 263 | + /** |
| 264 | + * Called by the administrator on the Export Type Groups page. It reorders the export groups, which determines |
| 265 | + * the order in which they appear in the client and admin pages. |
| 266 | + * |
| 267 | + * @param array $info |
| 268 | + */ |
| 269 | + public function reorderExportGroups($info) |
| 270 | + { |
| 271 | + $db = Core::$db; |
| 272 | + |
| 273 | + // $L |
| 274 | + |
| 275 | + $sortable_id = $info["sortable_id"]; |
| 276 | + $export_group_ids = explode(",", $info["{$sortable_id}_sortable__rows"]); |
| 277 | + |
| 278 | + $order = 1; |
| 279 | + foreach ($export_group_ids as $export_group_id) { |
| 280 | + $db->query(" |
| 281 | + UPDATE {PREFIX}module_export_groups |
| 282 | + SET list_order = :order |
| 283 | + WHERE export_group_id = :export_group_id |
| 284 | + "); |
| 285 | + $db->bindAll(array( |
| 286 | + "list_order" => $order, |
| 287 | + "export_group_id" => $export_group_id |
| 288 | + )); |
| 289 | + $db->execute(); |
| 290 | + |
| 291 | + $order++; |
| 292 | + } |
| 293 | + |
| 294 | + return array(true, $L["notify_export_group_reordered"]); |
| 295 | + } |
| 296 | + |
| 297 | + |
| 298 | + /** |
| 299 | + * This returns the IDs of the previous and next export groups, for the << prev, next >> navigation. |
| 300 | + * |
| 301 | + * @param integer $export_group_id |
| 302 | + * @return array prev_id => the previous export group ID (or empty string) |
| 303 | + * next_id => the next export group ID (or empty string) |
| 304 | + */ |
| 305 | + public function getExportGroupPrevNextLinks($export_group_id) |
| 306 | + { |
| 307 | + $db = Core::$db; |
| 308 | + |
| 309 | + $db->query(" |
| 310 | + SELECT export_group_id |
| 311 | + FROM {PREFIX}module_export_groups |
| 312 | + ORDER BY list_order ASC |
| 313 | + "); |
| 314 | + $db->execute(); |
| 315 | + |
| 316 | + $sorted_ids = $db->fetchAll(PDO::FETCH_COLUMN); |
| 317 | + $current_index = array_search($export_group_id, $sorted_ids); |
| 318 | + |
| 319 | + $return_info = array("prev_id" => "", "next_id" => ""); |
| 320 | + if ($current_index === 0) { |
| 321 | + if (count($sorted_ids) > 1) { |
| 322 | + $return_info["next_id"] = $sorted_ids[$current_index + 1]; |
| 323 | + } |
| 324 | + } else if ($current_index === count($sorted_ids)-1) { |
| 325 | + if (count($sorted_ids) > 1) |
| 326 | + $return_info["prev_id"] = $sorted_ids[$current_index-1]; |
| 327 | + } else { |
| 328 | + $return_info["prev_id"] = $sorted_ids[$current_index-1]; |
| 329 | + $return_info["next_id"] = $sorted_ids[$current_index+1]; |
| 330 | + } |
| 331 | + |
| 332 | + return $return_info; |
| 333 | + } |
| 334 | + |
| 335 | + |
| 336 | + public function deserializedExportGroupMapping($str) |
| 337 | + { |
| 338 | + $form_ids = array(); |
| 339 | + $view_ids = array(); |
| 340 | + |
| 341 | + if (!empty($str)) { |
| 342 | + list($form_ids, $view_ids) = explode("|", $str); |
| 343 | + $form_ids = explode(",", $form_ids); |
| 344 | + $view_ids = explode(",", $view_ids); |
| 345 | + } |
| 346 | + |
| 347 | + return array( |
| 348 | + "form_ids" => $form_ids, |
| 349 | + "view_ids" => $view_ids |
| 350 | + ); |
| 351 | + } |
| 352 | +} |
0 commit comments