PageRenderTime 71ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/plugins/glz_custom_fields_code.php

https://github.com/gerhard/glz_custom_fields
PHP | 457 lines | 349 code | 64 blank | 44 comment | 39 complexity | a8c0fb27dd1b77edb02d1eee8daceca0 MD5 | raw file
  1. <?php
  2. // Including helper files. If we can't have classes, we will use includes
  3. require_once('lib/gTxt.php');
  4. require_once('lib/db.php');
  5. require_once('lib/helpers.php');
  6. require_once('lib/callbacks.php');
  7. global $event;
  8. // globals, expensive operations mostly
  9. before_glz_custom_fields();
  10. if (@txpinterface == "admin") {
  11. // INSTALL ROUTINES
  12. // checks if all tables exist and everything is setup properly
  13. add_privs('glz_custom_fields_install', "1");
  14. register_callback("glz_custom_fields_install", "plugin_lifecycle.glz_custom_fields", "installed");
  15. // we'll be doing this only on the pages that we care about, not everywhere
  16. if ( in_array($event, array("article", "prefs", "glz_custom_fields", "plugin_prefs.glz_custom_fields")) ) {
  17. // we need some stylesheets & JS
  18. add_privs('glz_custom_fields_css_js', "1,2,3,4,5,6");
  19. register_callback('glz_custom_fields_css_js', "admin_side", 'head_end');
  20. // we need to make sure that all custom field values will be converted to strings first - think checkboxes & multi-selects etc.
  21. if ( (gps("step") == "edit") || (gps("step") == "create") ) {
  22. add_privs('glz_custom_fields_before_save', "1,2,3,4,5,6");
  23. register_callback('glz_custom_fields_before_save', "article", '', 1);
  24. }
  25. }
  26. // Custom Fields tab under Extensions
  27. add_privs('glz_custom_fields', "1,2");
  28. register_tab("extensions", 'glz_custom_fields', "Custom Fields");
  29. register_callback('glz_custom_fields', "glz_custom_fields");
  30. // plugin preferences
  31. add_privs('plugin_prefs.glz_custom_fields', "1,2");
  32. register_callback('glz_custom_fields_preferences', 'plugin_prefs.glz_custom_fields');
  33. // YES, finally the default custom fields are replaced by the new, pimped ones : )
  34. add_privs('glz_custom_fields_replace', "1,2,3,4,5,6");
  35. register_callback('glz_custom_fields_replace', 'article_ui', 'custom_fields');
  36. // YES, now we have textarea custom fields as well ; )
  37. register_callback('glz_custom_fields_replace', 'article_ui', 'body');
  38. }
  39. // -------------------------------------------------------------
  40. // everything is happening in this function... generates the content for Extensions > Custom Fields
  41. function glz_custom_fields() {
  42. global $event, $all_custom_sets, $glz_notice, $prefs;
  43. // we have $_POST, let's see if there is any CRUD
  44. if ( $_POST ) {
  45. $incoming = stripPost();
  46. // DEBUG
  47. // die(dmp($incoming));
  48. extract($incoming);
  49. // create an empty $value if it's not set in the $_POST
  50. if ( !isset($value) )
  51. $value = '';
  52. // we are deleting a new custom field
  53. if ( gps('delete') ) {
  54. glz_custom_fields_MySQL("delete", $custom_set, PFX."txp_prefs");
  55. glz_custom_fields_MySQL("delete", $custom_set, PFX."txp_lang");
  56. glz_custom_fields_MySQL("delete", $custom_set, PFX."custom_fields");
  57. glz_custom_fields_MySQL("delete", glz_custom_number($custom_set), PFX."textpattern");
  58. $glz_notice[] = glz_custom_fields_gTxt("deleted", array('{custom_set_name}' => $custom_set_name));
  59. }
  60. // we are resetting one of the mighty 10
  61. if ( gps('reset') ) {
  62. glz_custom_fields_MySQL("reset", $custom_set, PFX."txp_prefs");
  63. glz_custom_fields_MySQL("delete", $custom_set, PFX."custom_fields");
  64. glz_custom_fields_MySQL("reset", glz_custom_number($custom_set), PFX."textpattern", array(
  65. 'custom_set_type' => $custom_set_type,
  66. 'custom_field' => glz_custom_number($custom_set)
  67. ));
  68. $glz_notice[] = glz_custom_fields_gTxt("reset", array('{custom_set_name}' => $custom_set_name));
  69. }
  70. // we are adding a new custom field
  71. if ( gps("custom_field_number") ) {
  72. $custom_set_name = gps("custom_set_name");
  73. // if no name was specified, abort
  74. if ( !$custom_set_name )
  75. $glz_notice[] = glz_custom_fields_gTxt("no_name");
  76. else {
  77. $custom_set_name = glz_clean_string($custom_set_name);
  78. $name_exists = glz_check_custom_set_name($all_custom_sets, $custom_set_name);
  79. // if name doesn't exist
  80. if ( $name_exists == FALSE ) {
  81. glz_custom_fields_MySQL("new", $custom_set_name, PFX."txp_prefs", array(
  82. 'custom_field_number' => $custom_field_number,
  83. 'custom_set_type' => $custom_set_type,
  84. 'custom_set_position' => $custom_set_position
  85. ));
  86. glz_custom_fields_MySQL("new", $custom_set_name, PFX."txp_lang", array(
  87. 'custom_field_number' => $custom_field_number,
  88. 'lang' => $GLOBALS['prefs']['language']
  89. ));
  90. glz_custom_fields_MySQL("new", $custom_set_name, PFX."textpattern", array(
  91. 'custom_field_number' => $custom_field_number,
  92. 'custom_set_type' => $custom_set_type
  93. ));
  94. // there are custom fields for which we do not need to touch custom_fields table
  95. if ( !in_array($custom_set_type, array("textarea", "text_input")) ) {
  96. glz_custom_fields_MySQL("new", $custom_set_name, PFX."custom_fields", array(
  97. 'custom_field_number' => $custom_field_number,
  98. 'value' => $value
  99. ));
  100. }
  101. $glz_notice[] = glz_custom_fields_gTxt("created", array('{custom_set_name}' => $custom_set_name));
  102. }
  103. // name exists, abort
  104. else
  105. $glz_notice[] = glz_custom_fields_gTxt("exists", array('{custom_set_name}' => $custom_set_name));
  106. }
  107. }
  108. // we are editing an existing custom field
  109. if ( gps('save') ) {
  110. if ( !empty($custom_set_name) ) {
  111. $custom_set_name = glz_clean_string($custom_set_name);
  112. $name_exists = glz_check_custom_set_name($all_custom_sets, $custom_set_name, $custom_set);
  113. // if name doesn't exist we'll need to create a new custom_set
  114. if ( $name_exists == FALSE ) {
  115. glz_custom_fields_MySQL("update", $custom_set, PFX."txp_prefs", array(
  116. 'custom_set_name' => $custom_set_name,
  117. 'custom_set_type' => $custom_set_type,
  118. 'custom_set_position' => $custom_set_position
  119. ));
  120. // custom sets need to be changed based on their type
  121. glz_custom_fields_MySQL("update", $custom_set, PFX."textpattern", array(
  122. 'custom_set_type' => $custom_set_type,
  123. 'custom_field' => glz_custom_number($custom_set)
  124. ));
  125. // for textareas we do not need to touch custom_fields table
  126. if ( $custom_set_type != "textarea" ) {
  127. glz_custom_fields_MySQL("delete", $custom_set, PFX."custom_fields");
  128. glz_custom_fields_MySQL("new", $custom_set_name, PFX."custom_fields", array(
  129. 'custom_set' => $custom_set,
  130. 'value' => $value
  131. ));
  132. }
  133. $glz_notice[] = glz_custom_fields_gTxt("updated", array('{custom_set_name}' => $custom_set_name));
  134. }
  135. // name exists, abort
  136. else
  137. $glz_notice[] = glz_custom_fields_gTxt("exists", array('{custom_set_name}' => $custom_set_name));
  138. }
  139. else
  140. $glz_notice[] = glz_custom_fields_gTxt('no_name');
  141. }
  142. // need to re-fetch data since things modified
  143. $all_custom_sets = glz_custom_fields_MySQL("all");
  144. }
  145. pagetop("Custom Fields");
  146. // the table with all custom fields follows
  147. echo
  148. n.'<div class="listtables">'.n.
  149. ' <table class="txp-list glz_custom_fields">'.n.
  150. ' <thead>'.n.
  151. ' <tr>'.n.
  152. ' <th>Position</th>'.n.
  153. ' <th>Name</th>'.n.
  154. ' <th>Type</th>'.n.
  155. ' <th>&nbsp;</th>'.n.
  156. ' </tr>'.n.
  157. ' </thead>'.n.
  158. ' <tbody>'.n;
  159. // looping through all our custom fields to build the table
  160. $i = 0;
  161. foreach ( $all_custom_sets as $custom => $custom_set ) {
  162. // first 10 fields cannot be deleted, just reset
  163. if ( $i < 10 ) {
  164. // can't reset a custom field that is not set
  165. $reset_delete = ( $custom_set['name'] ) ?
  166. glz_form_buttons("reset", "Reset", $custom, htmlspecialchars($custom_set['name']), $custom_set['type'], '', 'return confirm(\'By proceeding you will RESET ALL data in `textpattern` and `custom_fields` tables for `'.$custom.'`. Are you sure?\');') :
  167. NULL;
  168. }
  169. else {
  170. $reset_delete = glz_form_buttons("delete", "Delete", $custom, htmlspecialchars($custom_set['name']), $custom_set['type'], '', 'return confirm(\'By proceeding you will DELETE ALL data in `textpattern` and `custom_fields` tables for `'.$custom.'`. Are you sure?\');');
  171. }
  172. $edit = glz_form_buttons("edit", "Edit", $custom, htmlspecialchars($custom_set['name']), $custom_set['type'], $custom_set['position']);
  173. echo
  174. ' <tr>'.n.
  175. ' <td class="custom_set_position">'.$custom_set['position'].'</td>'.n.
  176. ' <td class="custom_set_name">'.$custom_set['name'].'</td>'.n.
  177. ' <td class="type">'.(($custom_set['name']) ? glz_custom_fields_gTxt($custom_set['type']) : '').'</td>'.n.
  178. ' <td class="events">'.$reset_delete.sp.$edit.'</td>'.n.
  179. ' </tr>'.n;
  180. $i++;
  181. }
  182. echo
  183. ' </tbody>'.n.
  184. ' </table>'.n;
  185. '</div>'.n;
  186. // the form where custom fields are being added/edited
  187. $legend = gps('edit') ?
  188. 'Edit '.gps('custom_set') :
  189. 'Add new custom field';
  190. $custom_field = gps('edit') ?
  191. '<input name="custom_set" value="'.gps('custom_set').'" type="hidden" />' :
  192. '<input name="custom_field_number" value="'.glz_custom_next($all_custom_sets).'" type="hidden" />';
  193. $custom_set = gps('edit') ?
  194. gps('custom_set') :
  195. NULL;
  196. $custom_name = gps('edit') ?
  197. gps('custom_set_name') :
  198. NULL;
  199. $custom_set_position = gps('edit') ?
  200. gps('custom_set_position') :
  201. NULL;
  202. $arr_custom_set_types = glz_custom_set_types();
  203. $custom_set_types = NULL;
  204. foreach ( $arr_custom_set_types as $custom_type_group => $custom_types ) {
  205. $custom_set_types .= '<optgroup label="'.ucfirst($custom_type_group).'">'.n;
  206. foreach ($custom_types as $custom_type) {
  207. $selected = ( gps('edit') && gps('custom_set_type') == $custom_type ) ?
  208. ' selected="selected"' :
  209. NULL;
  210. $custom_set_types .= '<option value="'.$custom_type.'"'.$selected.'>'.glz_custom_fields_gTxt($custom_type).'</option>'.n;
  211. }
  212. $custom_set_types .= '</optgroup>'.n;
  213. }
  214. // fetching the values for this custom field
  215. if ( gps('edit') ) {
  216. if ( $custom_set_type == "text_input" )
  217. $arr_values = glz_custom_fields_MySQL('all_values', glz_custom_number($custom_set), '', array('custom_set_name' => $custom_set_name, 'status' => 4));
  218. else
  219. $arr_values = glz_custom_fields_MySQL("values", $custom_set, '', array('custom_set_name' => $custom_set_name));
  220. $values = ( $arr_values ) ?
  221. implode("\r\n", $arr_values) :
  222. '';
  223. }
  224. else
  225. $values = '';
  226. $action = gps('edit') ?
  227. '<input name="save" value="Save" type="submit" class="submit" />' :
  228. '<input name="add_new" value="Add new" type="submit" class="submit" />';
  229. // this needs to be different for a script
  230. $value = ( isset($custom_set_type) && $custom_set_type == "custom-script" ) ?
  231. '<input type="text" name="value" id="value" value="'.$values.'" class="left"/><span class="right"><em>Relative path from your website\'s public folder</em></span>' :
  232. '<textarea name="value" id="value" class="left">'.$values.'</textarea><span class="right"><em>Each value on a separate line</em> <br /><em>One {default} value allowed</em></span>';
  233. // ok, all is set, let's build the form
  234. echo
  235. '<form method="post" action="index.php" id="add_edit_custom_field">'.n.
  236. '<input name="event" value="glz_custom_fields" type="hidden" />'.n.
  237. $custom_field.n.
  238. '<fieldset>'.n.
  239. ' <legend>'.$legend.'</legend>'.n.
  240. ' <p class="clearfix">
  241. <label for="custom_set_name" class="left">Name:</label>
  242. <input type="text" name="custom_set_name" value="'.htmlspecialchars($custom_name).'" id="custom_set_name" class="left" />
  243. <span class="right"><em>Only word characters allowed</em></span>
  244. </p>'.n.
  245. ' <p class="clearfix">
  246. <label for="custom_set_type" class="left">Type:</label>
  247. <select name="custom_set_type" id="custom_set_type" class="left">
  248. '. $custom_set_types.'
  249. </select>
  250. </p>'.n.
  251. ' <p class="clearfix">
  252. <label for="custom_set_position" class="left">Position:</label>
  253. <input type="text" name="custom_set_position" value="'.htmlspecialchars($custom_set_position).'" id="custom_set_position" class="left" />
  254. <span class="right"><em>Automatically assigned if blank</em></span>
  255. </p>'.n.
  256. ' <p class="clearfix">
  257. <label for="value" class="left">Value:</label>
  258. '. $value.'
  259. </p>'.n.
  260. ' '.$action.n.
  261. '</fieldset>'.n.
  262. '</form>'.n;
  263. }
  264. // -------------------------------------------------------------
  265. // glz_custom_fields preferences
  266. function glz_custom_fields_preferences() {
  267. global $event, $glz_notice;
  268. if ( $_POST && gps('save') ) {
  269. glz_custom_fields_MySQL("update_plugin_preferences", $_POST['glz_custom_fields_prefs']);
  270. $glz_notice[] = glz_custom_fields_gTxt("preferences_updated");
  271. // need to re-fetch from db because this has changed since $prefs has been populated
  272. }
  273. $current_preferences = glz_custom_fields_MySQL('plugin_preferences');
  274. pagetop("glz_custom_fields Preferences");
  275. // custom_fields
  276. $arr_values_ordering = array(
  277. 'ascending' => "Ascending",
  278. 'descending' => "Descending",
  279. 'custom' => "As entered"
  280. );
  281. $values_ordering = '<select name="glz_custom_fields_prefs[values_ordering]" id="glz_custom_fields_prefs_values_ordering">';
  282. foreach ( $arr_values_ordering as $value => $title ) {
  283. $selected = ($current_preferences['values_ordering'] == $value) ? ' selected="selected"' : '';
  284. $values_ordering .= "<option value=\"$value\"$selected>$title</option>";
  285. }
  286. $values_ordering .= "</select>";
  287. $multiselect_size = '<input type="text" name="glz_custom_fields_prefs[multiselect_size]" id="glz_custom_fields_prefs_multiselect_size" value="'.$current_preferences['multiselect_size'].'" />';
  288. $custom_scripts_path_error = ( @fopen($current_preferences['custom_scripts_path'], "r") ) ?
  289. '' :
  290. '<br /><em class="red">Folder does not exist, please create it.</em>';
  291. // jquery.datePicker
  292. $datepicker_url_error = ( @fopen($current_preferences['datepicker_url']."/datePicker.js", "r") ) ?
  293. '' :
  294. '<br /><em class="red">Folder does not exist, please create it.</em>';
  295. $arr_date_format = array("dd/mm/yyyy", "mm/dd/yyyy", "yyyy-mm-dd", "dd mm yy");
  296. $date_format = '<select name="glz_custom_fields_prefs[datepicker_format]" id="glz_custom_fields_prefs_datepicker_format">';
  297. foreach ( $arr_date_format as $format ) {
  298. $selected = ($current_preferences['datepicker_format'] == $format) ? ' selected="selected"' : '';
  299. $date_format .= "<option value=\"$format\"$selected>$format</option>";
  300. }
  301. $date_format .= "</select>";
  302. $arr_days = array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
  303. $first_day = '<select name="glz_custom_fields_prefs[datepicker_first_day]" id="glz_custom_fields_prefs_datepicker_first_day">';
  304. foreach ( $arr_days as $key => $day ) {
  305. $selected = ($current_preferences['datepicker_first_day'] == $key) ? ' selected="selected"' : '';
  306. $first_day .= "<option value=\"$key\"$selected>$day</option>";
  307. }
  308. $first_day .= "</select>";
  309. $start_date = '<input type="text" name="glz_custom_fields_prefs[datepicker_start_date]" id="glz_custom_fields_prefs_datepicker_start_date" value="'.$current_preferences['datepicker_start_date'].'" />';
  310. // jquery.timePicker
  311. $timepicker_url_error = ( @fopen($current_preferences['timepicker_url']."/timePicker.js", "r") ) ?
  312. '' :
  313. '<br /><em class="red">Folder does not exist, please create it.</em>';
  314. $arr_time_format = array('true' => "24 hours", 'false' => "12 hours");
  315. $show_24 = '<select name="glz_custom_fields_prefs[timepicker_show_24]" id="glz_custom_fields_prefs_timepicker_show_24">';
  316. foreach ( $arr_time_format as $value => $title ) {
  317. $selected = ($current_preferences['timepicker_show_24'] == $value) ? ' selected="selected"' : '';
  318. $show_24 .= "<option value=\"$value\"$selected>$title</option>";
  319. }
  320. $show_24 .= "</select>";
  321. $out = <<<EOF
  322. <form action="index.php" method="post">
  323. <table id="list" class="glz_custom_fields_prefs" cellpadding="0" cellspacing="0" align="center">
  324. <tbody>
  325. <tr class="heading">
  326. <td colspan="2"><h2 class="pref-heading">Custom Fields</h2></td>
  327. </tr>
  328. <tr>
  329. <th scope="row"><label for="glz_custom_fields_prefs_values_ordering">Order for custom field values</th>
  330. <td>{$values_ordering}</td>
  331. </tr>
  332. <tr>
  333. <th scope="row"><label for="glz_custom_fields_prefs_multiselect_size">Multi-select field size</th>
  334. <td>{$multiselect_size}</td>
  335. </tr>
  336. <tr>
  337. <th scope="row"><label for="glz_custom_fields_prefs_custom_scripts_path">Custom scripts path</th>
  338. <td><input type="text" name="glz_custom_fields_prefs[custom_scripts_path]" id="glz_custom_fields_prefs_custom_scripts_path" value="{$current_preferences['custom_scripts_path']}" />{$custom_scripts_path_error}</td>
  339. </tr>
  340. <tr class="heading">
  341. <td colspan="2"><h2 class="pref-heading left">Date Picker</h2> <a href="http://www.kelvinluck.com/assets/jquery/datePicker/v2/demo/index.html" title="A flexible unobtrusive calendar component for jQuery" class="right">jQuery datePicker</a></td>
  342. </tr>
  343. <tr>
  344. <th scope="row"><label for="glz_custom_fields_prefs_datepicker_url">Date Picker plugin URL</th>
  345. <td><input type="text" name="glz_custom_fields_prefs[datepicker_url]" id="glz_custom_fields_prefs_datepicker_url" value="{$current_preferences['datepicker_url']}" />{$datepicker_url_error}</td>
  346. </tr>
  347. <tr>
  348. <th scope="row"><label for="glz_custom_fields_prefs_datepicker_format">Date format</th>
  349. <td>{$date_format}</td>
  350. </tr>
  351. <tr>
  352. <th scope="row"><label for="glz_custom_fields_prefs_datepicker_first_day">First day of week</th>
  353. <td>{$first_day}</td>
  354. </tr>
  355. <tr>
  356. <th scope="row"><label for="glz_custom_fields_prefs_datepicker_start_date">Start date</th>
  357. <td>{$start_date}<br /><em class="grey">MUST be the same as "Date format"</em></td>
  358. </tr>
  359. <tr class="heading">
  360. <td colspan="2"><h2 class="pref-heading left">Time Picker</h2> <a href="http://labs.perifer.se/timedatepicker/" title="jQuery time picker" class="right">jQuery timePicker</a></td>
  361. </tr>
  362. <tr>
  363. <th scope="row"><label for="glz_custom_fields_prefs_timepicker_url">Time Picker plugin URL</th>
  364. <td><input type="text" name="glz_custom_fields_prefs[timepicker_url]" id="glz_custom_fields_prefs_timepicker_url" value="{$current_preferences['timepicker_url']}" />{$timepicker_url_error}</td>
  365. </tr>
  366. <tr>
  367. <th scope="row"><label for="glz_custom_fields_prefs_timepicker_start_time">Start time</th>
  368. <td><input type="text" name="glz_custom_fields_prefs[timepicker_start_time]" id="glz_custom_fields_prefs_timepicker_start_time" value="{$current_preferences['timepicker_start_time']}" /></td>
  369. </tr>
  370. <tr>
  371. <th scope="row"><label for="glz_custom_fields_prefs_timepicker_end_time">End time</th>
  372. <td><input type="text" name="glz_custom_fields_prefs[timepicker_end_time]" id="glz_custom_fields_prefs_timepicker_end_time" value="{$current_preferences['timepicker_end_time']}" /></td>
  373. </tr>
  374. <tr>
  375. <th scope="row"><label for="glz_custom_fields_prefs_timepicker_step">Step</th>
  376. <td><input type="text" name="glz_custom_fields_prefs[timepicker_step]" id="glz_custom_fields_prefs_timepicker_step" value="{$current_preferences['timepicker_step']}" /></td>
  377. </tr>
  378. <tr>
  379. <th scope="row"><label for="glz_custom_fields_prefs_timepicker_step">Time format</th>
  380. <td>{$show_24}</td>
  381. </tr>
  382. <tr>
  383. <td colspan="2" class="noline">
  384. <input class="publish" type="submit" name="save" value="Save" />
  385. <input type="hidden" name="event" value="plugin_prefs.glz_custom_fields" />
  386. </td>
  387. </tr>
  388. </tbody>
  389. </table>
  390. EOF;
  391. echo $out;
  392. }
  393. ?>