From a63fd0bd456186192a5d99f40de7e9789ddcdebd Mon Sep 17 00:00:00 2001 From: Ilyas Ronef Date: Thu, 17 Oct 2013 20:46:14 +0600 Subject: [PATCH] Initial commit --- README.md | 3 + mm_requirefields/index.html | 2 + mm_requirefields/mm_requirefields.php | 175 ++++++++++++++++++++++++++ 3 files changed, 180 insertions(+) create mode 100644 README.md create mode 100644 mm_requirefields/index.html create mode 100644 mm_requirefields/mm_requirefields.php diff --git a/README.md b/README.md new file mode 100644 index 0000000..a49efb9 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# mm_requireFields + +Visit the following [link](http://code.divandesign.biz/modx/mm_requirefields) to read the documentation, instructions & changelog. \ No newline at end of file diff --git a/mm_requirefields/index.html b/mm_requirefields/index.html new file mode 100644 index 0000000..cfb7d0f --- /dev/null +++ b/mm_requirefields/index.html @@ -0,0 +1,2 @@ +

Unauthorized access

+You're not allowed to access file folder \ No newline at end of file diff --git a/mm_requirefields/mm_requirefields.php b/mm_requirefields/mm_requirefields.php new file mode 100644 index 0000000..d9e8a50 --- /dev/null +++ b/mm_requirefields/mm_requirefields.php @@ -0,0 +1,175 @@ +Event; + + // if the current page is being edited by someone in the list of roles, and uses a template in the list of templates + if ($e->name == 'OnDocFormRender' && useThisRule($roles, $templates)){ + // if we've been supplied with a string, convert it into an array + $fields = makeArray($fields); + + if (count($fields) == 0) return; + + $output = "\n// -------------- mm_requireFields :: Begin ------------- \n"; + + $output .= ' + $j("head").append(""); + var requiredHTML = "*"; + '; + + $submit_js = ''; + $load_js = ''; + + foreach ($fields as $field){ + //ignore for now + switch ($field){ + // fields for which this doesn't make sense - in my opinion anyway :) + case 'keywords': + case 'metatags': + case 'hidemenu': + case 'which_editor': + case 'template': + case 'menuindex': + case 'show_in_menu': + case 'parent': + case 'is_folder': + case 'is_richtext': + case 'log': + case 'searchable': + case 'cacheable': + case 'clear_cache': + case 'content_type': + case 'content_dispo': + case 'which_editor': + $output .=''; + break; + + // 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 + case 'pub_date': + case 'unpub_date': + $load_js .= ' + $j("#pub_date, #unpub_date").each(function() { this.type = "text"; }); // Cant use jQuery attr function as datepicker class clashes with jQuery methods + '; + // no break, because we want to do the things below too. + + // Ones that follow the regular pattern + default: + //if it's tv & it's not used in current template + if ($mm_fields[$field]['tv'] && tplUseTvs($mm_current_page['template'], $field) === false){ + //Go to next field + continue; + } + + // What type is this field? + $fieldname = $mm_fields[$field]['fieldname']; + + // What jQuery selector should we use for this fieldtype? + switch ($mm_fields[$field]['fieldtype']){ + case 'textarea': + $selector = "textarea[name=$fieldname]"; + break; + + case 'input': // If it's an input, we only want to do something if it's a text field + $selector = "input[type=text][name=$fieldname]"; + break; + + default: // all other input types, do nothing + $selector = ''; + break; + } + + // If we've found something we want to use + if (!empty($selector)){ + if ($field == 'content'){ + $label = '$j("#content_header")'; + $tinymcefix = 'if (typeof(tinyMCE) != "undefined" && typeof(tinyMCE.triggerSave) != "undefined"){tinyMCE.triggerSave();}'; + }else{ + $label = '$sel.parents("td:first").prev("td").children("span.warning")'; + $tinymcefix = ''; + } + + $submit_js .= ' + +// The element we are targetting ('.$fieldname.') +var $sel = $j("'.$selector.'"); + +'.$tinymcefix.' +// Check if its valid +if($j.trim($sel.val()) == ""){// If it is empty + // Find the label (this will be easier in Evo 1.1 with more semantic code) + var lbl = '.$label.'.text().replace($j(requiredHTML).text(), ""); + + // Add the label to the errors array. Would be nice to say which tab it is on, but no + // easy way of doing this in 1.0.x as no semantic link between tabs and tab body + errors.push(lbl); + + // Add an event so the hilight is removed upon focussing + $sel.addClass("mmRequired").focus(function(){ + $j(this).removeClass("mmRequired"); + }); +} + '; + + $load_js .= ' + +// Add an indicator this is required ('.$fieldname.') +var $sel = $j("'.$selector.'"); + +// Find the label (this will be easier in Evo 1.1 with more semantic code) +var $lbl = '.$label.'.append(requiredHTML); + + '; + } + break; + } + } + + $output .= $load_js.' +$j("#mutate").submit(function(){ + var errors = []; + +'.$submit_js.' + + if(errors.length > 0){ + var errMsg = errors.length + " required fields are missing:\n\n "; + + for (var i = 0; i < errors.length; i++){ + errMsg += " - " + errors[i] + " \n"; + } + errMsg += " \nPlease correct the indicated fields."; + + alert(errMsg); + return false; + }else{ + return true; + } +}); + '; + + $output .= "\n// -------------- mm_requireFields :: End ------------- \n"; + + $e->output($output . "\n"); + } +} +?> \ No newline at end of file