56 var $version =
'0.2.5';
62 var $compiled_path =
'./files/cache/js_filter_compiled/';
82 function __construct($path, $xml_file)
84 if(substr($path, -1) !==
'/')
88 $this->xml_file = sprintf(
"%s%s", $path, $xml_file);
89 $this->js_file = $this->_getCompiledFileName($this->xml_file);
98 if(!file_exists($this->xml_file))
102 if(!file_exists($this->js_file))
106 else if(filemtime($this->xml_file) > filemtime($this->js_file))
125 $xml_obj = parent::parse($buff);
127 $attrs = $xml_obj->filter->attrs;
128 $rules = $xml_obj->filter->rules;
131 $filter_name = $attrs->name;
132 $confirm_msg_code = $attrs->confirm_msg_code;
133 $module = $attrs->module;
135 $extend_filter = $attrs->extend_filter;
138 $field_node = $xml_obj->filter->form->node;
139 if($field_node && !is_array($field_node))
141 $field_node = array($field_node);
144 $parameter_param = $xml_obj->filter->parameter->param;
145 if($parameter_param && !is_array($parameter_param))
147 $parameter_param = array($parameter_param);
150 $response_tag = $xml_obj->filter->response->tag;
151 if($response_tag && !is_array($response_tag))
153 $response_tag = array($response_tag);
160 $this->js_file .=
'.nocache.js';
163 list($module_name, $method) = explode(
'.', $extend_filter);
166 if($module_name && $method)
169 $oExtendFilter =
getModel($module_name);
172 if(method_exists($oExtendFilter, $method))
175 $extend_filter_list = $oExtendFilter->{$method}(TRUE);
176 $extend_filter_count = count($extend_filter_list);
179 for($i = 0; $i < $extend_filter_count; $i++)
181 $name = $extend_filter_list[$i]->name;
182 $lang_value = $extend_filter_list[$i]->lang;
185 $lang->{$name} = $lang_value;
193 $target_list = array();
194 $target_type_list = array();
198 $js_messages = array();
203 if($rules && $rules->rule)
205 if(!is_array($rules->rule))
207 $rules->rule = array($rules->rule);
209 foreach($rules->rule as $r)
211 if($r->attrs->type ==
'regex')
213 $js_rules[] =
"v.cast('ADD_RULE', ['{$r->attrs->name}', {$r->body}]);";
219 $node_count = count($field_node);
222 foreach($field_node as $key => $node)
224 $attrs = $node->attrs;
225 $target = trim($attrs->target);
232 $rule = trim($attrs->rule ? $attrs->rule : $attrs->filter);
233 $equalto = trim($attrs->equalto);
237 if($attrs->required ==
'true')
239 $field[] =
'required:true';
241 if($attrs->minlength > 0)
243 $field[] =
'minlength:' . $attrs->minlength;
245 if($attrs->maxlength > 0)
247 $field[] =
'maxlength:' . $attrs->maxlength;
251 $field[] =
"equalto:'{$attrs->equalto}'";
255 $field[] =
"rule:'{$rule}'";
258 $fields[] =
"'{$target}': {" . implode(
',', $field) .
"}";
260 if(!in_array($target, $target_list))
262 $target_list[] = $target;
264 if(!$target_type_list[$target])
266 $target_type_list[$target] = $filter;
272 $rule_types = array(
'homepage' =>
'homepage',
'email_address' =>
'email');
274 for($i = 0; $i < $extend_filter_count; $i++)
276 $filter_item = $extend_filter_list[$i];
277 $target = trim($filter_item->name);
285 $type = $filter_item->type;
286 $rule = $rule_types[$type] ? $rule_types[$type] :
'';
287 $required = ($filter_item->required ==
'true');
292 $field[] =
'required:true';
296 $field[] =
"rule:'{$rule}'";
298 $fields[] =
"\t\t'{$target}' : {" . implode(
',', $field) .
"}";
300 if(!in_array($target, $target_list))
302 $target_list[] = $target;
304 if(!$target_type_list[$target])
306 $target_type_list[$target] = $type;
311 $rename_params = array();
312 $parameter_count = count($parameter_param);
316 foreach($parameter_param as $key => $param)
318 $attrs = $param->attrs;
319 $name = trim($attrs->name);
320 $target = trim($attrs->target);
323 if($name && $target && ($name != $target))
325 $rename_params[] =
"'{$target}':'{$name}'";
327 if($name && !in_array($name, $target_list))
329 $target_list[] = $name;
334 for($i = 0; $i < $extend_filter_count; $i++)
336 $filter_item = $extend_filter_list[$i];
337 $target = $name = trim($filter_item->name);
338 if(!$name || !$target)
343 if(!in_array($name, $target_list))
345 $target_list[] = $name;
351 $response_count = count($response_tag);
352 $responses = array();
353 for($i = 0; $i < $response_count; $i++)
355 $attrs = $response_tag[$i]->attrs;
356 $name = $attrs->name;
357 $responses[] =
"'{$name}'";
361 $target_count = count($target_list);
362 for($i = 0; $i < $target_count; $i++)
364 $target = $target_list[$i];
365 if(!$lang->{$target})
367 $lang->{$target} = $target;
369 $text = preg_replace(
'@\r?\n@',
'\\n', addslashes($lang->{$target}));
370 $js_messages[] =
"v.cast('ADD_MESSAGE',['{$target}','{$text}']);";
384 foreach($lang->filter as $key => $val)
390 $val = preg_replace(
'@\r?\n@',
'\\n', addslashes($val));
391 $js_messages[] = sprintf(
"v.cast('ADD_MESSAGE',['%s','%s']);", $key, $val);
394 $callback_func = $xml_obj->filter->response->attrs->callback_func;
397 $callback_func =
"filterAlertMessage";
401 if($confirm_msg_code)
403 $confirm_msg = $lang->{$confirm_msg_code};
407 $jsdoc[] =
"function {$filter_name}(form){ return legacy_filter('{$filter_name}', form, '{$module}', '{$act}', {$callback_func}, [" . implode(
',', $responses) .
"], '" . addslashes($confirm_msg) .
"', {" . implode(
',', $rename_params) .
"}) };";
408 $jsdoc[] =
'(function($){';
409 $jsdoc[] =
"\tvar v=xe.getApp('validator')[0];if(!v)return false;";
410 $jsdoc[] =
"\t" .
'v.cast("ADD_FILTER", ["' . $filter_name .
'", {' . implode(
',', $fields) .
'}]);';
411 $jsdoc[] =
"\t" . implode(
"\n\t", $js_rules);
412 $jsdoc[] =
"\t" . implode(
"\n\t", $js_messages);
413 $jsdoc[] =
'})(jQuery);';
414 $jsdoc = implode(
"\n", $jsdoc);
425 function _getCompiledFileName($xml_file)
427 return sprintf(
'%s%s.%s.compiled.js', $this->compiled_path, md5($this->version . $xml_file),
Context::getLangType());
writeFile($filename, $buff, $mode="w")