PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/assets/plugins/managermanager/functions/fields.inc.php

https://github.com/good-web-master/modx.evo.custom
PHP | 466 lines | 280 code | 132 blank | 54 comment | 31 complexity | 1a09896f9869a4af7b19c5c6c3ed341d MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-1.0, GPL-2.0, MIT, BSD-3-Clause
  1. <?php
  2. //---------------------------------------------------------------------------------
  3. // mm_renameField
  4. // Change the label for an element
  5. //---------------------------------------------------------------------------------
  6. function mm_renameField($field, $newlabel, $roles='', $templates='', $newhelp='') {
  7. global $mm_fields, $modx;
  8. $e = &$modx->Event;
  9. // if the current page is being edited by someone in the list of roles, and uses a template in the list of templates
  10. if ($e->name == 'OnDocFormRender' && useThisRule($roles, $templates)) {
  11. $output = " // ----------- Rename field -------------- \n";
  12. switch ($field) {
  13. // Exceptions
  14. case 'keywords':
  15. $output .= '$j("select[name*=keywords]").siblings("span.warning").empty().prepend("'.jsSafe($newlabel).'");';
  16. break;
  17. case 'metatags':
  18. $output .= '$j("select[name*=metatags]").siblings("span.warning").empty().prepend("'.jsSafe($newlabel).'");';
  19. break;
  20. case 'hidemenu':
  21. case 'show_in_menu':
  22. $output .= '$j("input[name=hidemenucheck]").siblings("span.warning").empty().prepend("'.jsSafe($newlabel).'");';
  23. break;
  24. case 'which_editor':
  25. $output .= '$j("#which_editor").prev("span.warning").empty().prepend("'.jsSafe($newlabel).'");';
  26. break;
  27. // Ones that follow the regular pattern
  28. default:
  29. if (isset($mm_fields[$field])) {
  30. $fieldtype = $mm_fields[$field]['fieldtype'];
  31. $fieldname = $mm_fields[$field]['fieldname'];
  32. $output .= '$j("'.$fieldtype.'[name='.$fieldname.']").parents("td").prev("td").children("span.warning").empty().prepend("'.jsSafe($newlabel).'");';
  33. }
  34. break;
  35. } // end switch
  36. $e->output($output . "\n");
  37. // If new help has been supplied, do that too
  38. if ($newhelp != '') {
  39. mm_changeFieldHelp($field, $newhelp, $roles, $templates);
  40. }
  41. } // end if
  42. } // end function
  43. //---------------------------------------------------------------------------------
  44. // mm_hideFields
  45. // Hide a field
  46. //---------------------------------------------------------------------------------
  47. function mm_hideFields($fields, $roles='', $templates='') {
  48. global $mm_fields, $modx;
  49. $e = &$modx->Event;
  50. // if we've been supplied with a string, convert it into an array
  51. $fields = makeArray($fields);
  52. // if the current page is being edited by someone in the list of roles, and uses a template in the list of templates
  53. if ($e->name == 'OnDocFormRender' && useThisRule($roles, $templates)) {
  54. $e->output(" // ----------- Hide fields -------------- \n");
  55. foreach ($fields as $field) {
  56. $output = '';
  57. switch ($field) {
  58. // Exceptions
  59. case 'keywords':
  60. $output .= '$j("select[name*=keywords]").parent("td").hide();';
  61. break;
  62. case 'metatags':
  63. $output .= '$j("select[name*=metatags]").parent("td").hide()';
  64. break;
  65. case 'hidemenu':
  66. case 'show_in_menu':
  67. $output .= '$j("input[name=hidemenucheck]").parent("td").hide();';
  68. break;
  69. case 'menuindex':
  70. $output .= '$j("input[name=menuindex]").parents("table").parent("td").prev("td").children("span.warning").hide();' ."\n";
  71. $output .= '$j("input[name=menuindex]").parent("td").hide();';
  72. break;
  73. case 'which_editor':
  74. $output .= '$j("select#which_editor").prev("span.warning").hide();' . "\n";
  75. $output .= '$j("select#which_editor").hide();';
  76. break;
  77. case 'content':
  78. $output .= '$j("#sectionContentHeader, #sectionContentBody").hide();'; // For 1.0.0
  79. $output .= '$j("#ta").parent("div").parent("div").hide().prev("div").hide();'."\n"; // For 1.0.1
  80. break;
  81. case 'pub_date':
  82. $output .= '$j("input[name=pub_date]").parents("tr").next("tr").hide(); '."\n";
  83. $output .= '$j("input[name=pub_date]").parents("tr").hide(); ';
  84. break;
  85. case 'unpub_date':
  86. $output .= '$j("input[name=unpub_date]").parents("tr").next("tr").hide(); '."\n";
  87. $output .= '$j("input[name=unpub_date]").parents("tr").hide(); ';
  88. break;
  89. // Ones that follow the regular pattern
  90. default:
  91. if (isset($mm_fields[$field])) { // Check the fields exist, so we're not writing JS for elements that don't exist
  92. $output .= '$j("'.$mm_fields[$field]['fieldtype'].'[name='.$mm_fields[$field]['fieldname'].']").parents("tr").hide().next("tr").find("td[colspan=2]").parent("tr").hide(); ';
  93. }
  94. break;
  95. } // end switch
  96. $e->output($output . "\n");
  97. } // end foreach
  98. } // end if
  99. } // end function
  100. //---------------------------------------------------------------------------------
  101. // mm_changeFieldHelp
  102. // Change the help text of a field
  103. //---------------------------------------------------------------------------------
  104. function mm_changeFieldHelp($field, $helptext='', $roles='', $templates='') {
  105. global $mm_fields, $modx;
  106. $e = &$modx->Event;
  107. if ($helptext=='') {
  108. return;
  109. }
  110. // if the current page is being edited by someone in the list of roles, and uses a template in the list of templates
  111. if ($e->name == 'OnDocFormRender' && useThisRule($roles, $templates)) {
  112. $output = " // ----------- Change field help -------------- \n";
  113. switch ($field) {
  114. // Ones that follow the regular pattern
  115. default:
  116. // What type is this field?
  117. if (isset($mm_fields[$field])) {
  118. $fieldtype = $mm_fields[$field]['fieldtype'];
  119. $fieldname = $mm_fields[$field]['fieldname'];
  120. // Give the help button an ID, and modify the alt/title text
  121. $output .= '$j("'.$fieldtype.'[name='.$fieldname.']").siblings("img[style:contains(\'cursor:help\')]").attr("id", "'.$fieldname.'-help").attr("alt", "'.jsSafe($helptext).'").attr("title", "'.jsSafe($helptext).'"); ';
  122. } else {
  123. break;
  124. }
  125. break;
  126. } // end switch
  127. $e->output($output . "\n");
  128. } // end if
  129. } // end function
  130. //---------------------------------------------------------------------------------
  131. // mm_moveFieldsToTab
  132. // Move a field to a different tab
  133. //---------------------------------------------------------------------------------
  134. function mm_moveFieldsToTab($fields, $newtab, $roles='', $templates='') {
  135. global $modx, $mm_fields;
  136. $e = &$modx->Event;
  137. // if we've been supplied with a string, convert it into an array
  138. $fields = makeArray($fields);
  139. // if the current page is being edited by someone in the list of roles, and uses a template in the list of templates
  140. if ($e->name == 'OnDocFormRender' && useThisRule($roles, $templates)) {
  141. $output = " // ----------- Move field to tab -------------- \n";
  142. // If it's one of the default tabs, we need to get the capitalisation right
  143. switch ($newtab) {
  144. case 'general':
  145. case 'settings':
  146. case 'access':
  147. case 'meta': // version 1.0.0 only, removed in 1.0.1
  148. $newtab = ucfirst($newtab);
  149. break;
  150. }
  151. // Make sure the new tab exists in the DOM
  152. $output .= "if ( \$j('#tab".$newtab."').length > 0) { \n";
  153. $output .= 'var ruleHtml = \'<tr style="height: 10px"><td colspan="2"><div class="split"></div></td></tr>\'; ';
  154. // Try and identify any URL type TVs
  155. $output .= '$j("select[id$=_prefix]").each( function() { $j(this).parents("tr:first").addClass("urltv"); } ); ';
  156. // Go through each field that has been supplied
  157. foreach ($fields as $field) {
  158. switch ($field) {
  159. case 'content':
  160. $output .= '$j("#content_body").appendTo("#tab'.$newtab.'");'. "\n";
  161. $output .= '$j("#content_header").hide();' . "\n";
  162. break;
  163. // We can't move these fields because they belong in a particular place
  164. case 'keywords':
  165. case 'metatags':
  166. case 'which_editor':
  167. case 'hidemenu':
  168. case 'show_in_menu':
  169. case 'menuindex':
  170. // Do nothing
  171. break;
  172. case 'pub_date':
  173. $output .= 'var helpline = $j("input[name=pub_date]").parents("tr").next("tr").appendTo("#tab'.$newtab.'>table:first"); ' . "\n";
  174. $output .= '$j(helpline).before($j("input[name=pub_date]").parents("tr")); ' . "\n";
  175. $output .= 'helpline.after(ruleHtml); '. "\n";
  176. break;
  177. case 'unpub_date':
  178. $output .= 'var helpline = $j("input[name=unpub_date]").parents("tr").next("tr").appendTo("#tab'.$newtab.'>table:first"); ' . "\n";
  179. $output .= '$j(helpline).before($j("input[name=unpub_date]").parents("tr")); ' . "\n";
  180. $output .= 'helpline.after(ruleHtml); '. "\n";
  181. break;
  182. default:
  183. // What type is this field?
  184. if (isset($mm_fields[$field])) {
  185. $fieldtype = $mm_fields[$field]['fieldtype'];
  186. $fieldname = $mm_fields[$field]['fieldname'];
  187. $output .= '
  188. var toMove = $j("'.$fieldtype.'[name=\''.$fieldname.'\']").parents("tr:not(.urltv)");
  189. toMove.find("script").remove();
  190. toMove.next("tr").find("td[colspan=2]").parents("tr").remove(); // Get rid of line after, if there is one
  191. var movedTV = toMove.appendTo("#tab'.$newtab.'>table:first"); // Move the table row
  192. movedTV.after(ruleHtml); // Insert a rule after
  193. movedTV.find("td[width]").attr("width",""); // Remove widths from label column
  194. $j("[name^='.$fieldname.']:first").parents("td").removeAttr( "style" ); // This prevents an IE6/7 bug where the moved field would not be visible until you switched tabs
  195. ';
  196. }
  197. break;
  198. } // end switch
  199. } // end foreach
  200. $output .= "}";
  201. $e->output($output . "\n");
  202. } // end if
  203. } // end function
  204. //---------------------------------------------------------------------------------
  205. // mm_requireFields
  206. // Make fields required. Currently works with text fields only.
  207. // In the future perhaps this could deal with other elements.
  208. // Originally version by Jelle Jager AKA TobyL - Make fields required
  209. // Updated by ncrossland to utilise simpler field handline of MM 0.3.5+; bring jQuery code into line; add indication to required fields
  210. //---------------------------------------------------------------------------------
  211. function mm_requireFields($fields, $roles='', $templates=''){
  212. global $mm_fields, $modx;
  213. $e = &$modx->Event;
  214. // if we've been supplied with a string, convert it into an array
  215. $fields = makeArray($fields);
  216. // if the current page is being edited by someone in the list of roles, and uses a template in the list of templates
  217. if ($e->name == 'OnDocFormRender' && useThisRule($roles, $templates)) {
  218. $output = " // ----------- Require field -------------- \n";
  219. $output .= '
  220. $j("head").append("<style>.mmRequired { background-image: none !important; background-color: #ff9999 !important; } .requiredIcon { color: #ff0000; font-weight: bold; margin-left: 3px; cursor: help; }</style>");
  221. var requiredHTML = "<span class=\"requiredIcon\" title=\"Required\">*</span>";
  222. ';
  223. $submit_js = '';
  224. $load_js = '';
  225. foreach ($fields as $field) {
  226. //ignore for now
  227. switch ($field) {
  228. // fields for which this doesn't make sense - in my opinion anyway :)
  229. case 'keywords':
  230. case 'metatags':
  231. case 'hidemenu':
  232. case 'which_editor':
  233. case 'template':
  234. case 'menuindex':
  235. case 'show_in_menu':
  236. case 'parent':
  237. case 'is_folder':
  238. case 'is_richtext':
  239. case 'log':
  240. case 'searchable':
  241. case 'cacheable':
  242. case 'clear_cache':
  243. case 'content_type':
  244. case 'content_dispo':
  245. case 'which_editor':
  246. $output .='';
  247. break;
  248. // Pub/unpub dates don't have a type attribute on their input tag in 1.0.2, so add this. Won't do any harm to other versions
  249. case 'pub_date':
  250. case 'unpub_date':
  251. $load_js .= '
  252. $j("#pub_date, #unpub_date").each(function() { this.type = "text"; }); // Cant use jQuery attr function as datepicker class clashes with jQuery methods
  253. ';
  254. // no break, because we want to do the things below too.
  255. // Ones that follow the regular pattern
  256. default:
  257. // What type is this field?
  258. $fieldname = $mm_fields[$field]['fieldname'];
  259. // What jQuery selector should we use for this fieldtype?
  260. switch ($mm_fields[$field]['fieldtype']) {
  261. case 'textarea':
  262. $selector = "textarea[name=$fieldname]";
  263. break;
  264. case 'input': // If it's an input, we only want to do something if it's a text field
  265. $selector = "input[type=text][name=$fieldname]";
  266. break;
  267. default: // all other input types, do nothing
  268. $selector = '';
  269. break;
  270. }
  271. // If we've found something we want to use
  272. if (!empty($selector)) {
  273. $submit_js .= '
  274. // The element we are targetting ('.$fieldname.')
  275. var $sel = $j("'.$selector.'");
  276. // Check if its valid
  277. if($j.trim($sel.val()) == ""){ // If it is empty
  278. // Find the label (this will be easier in Evo 1.1 with more semantic code)
  279. var lbl = $sel.parent("td").prev("td").children("span.warning").text().replace($j(requiredHTML).text(), "");
  280. // Add the label to the errors array. Would be nice to say which tab it is on, but no
  281. // easy way of doing this in 1.0.x as no semantic link between tabs and tab body
  282. errors.push(lbl);
  283. // Add an event so the hilight is removed upon focussing
  284. $sel.addClass("mmRequired").focus(function(){
  285. $j(this).removeClass("mmRequired");
  286. });
  287. }
  288. ';
  289. $load_js .= '
  290. // Add an indicator this is required ('.$fieldname.')
  291. var $sel = $j("'.$selector.'");
  292. // Find the label (this will be easier in Evo 1.1 with more semantic code)
  293. var $lbl = $sel.parent("td").prev("td").children("span.warning").append(requiredHTML);
  294. ';
  295. }
  296. break;
  297. }
  298. }
  299. $output .= $load_js . '
  300. $j("#mutate").submit(function(){
  301. var errors = [];
  302. var msg = "";
  303. '.$submit_js.'
  304. if(errors.length > 0){
  305. var errMsg = errors.length + " required fields are missing:\n\n ";
  306. for (var i=0; i<errors.length; i++) {
  307. errMsg += " - " + errors[i] + " \n";
  308. }
  309. errMsg += " \nPlease correct the indicated fields.";
  310. alert(errMsg);
  311. return false;
  312. } else {
  313. return true;
  314. }
  315. });
  316. ';
  317. $e->output($output . "\n");
  318. } // end if
  319. } // end function
  320. ?>