XpressEngine Core  1.11.2
 All Classes Namespaces Files Functions Variables Pages
XmlJsFilter.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) NAVER <http://www.navercorp.com> */
3 
49 class XmlJsFilter extends XmlParser
50 {
51 
56  var $version = '0.2.5';
57 
62  var $compiled_path = './files/cache/js_filter_compiled/'; // / directory path for compiled cache file
67  var $xml_file = NULL;
68 
73  var $js_file = NULL; // /
74 
82  function __construct($path, $xml_file)
83  {
84  if(substr($path, -1) !== '/')
85  {
86  $path .= '/';
87  }
88  $this->xml_file = sprintf("%s%s", $path, $xml_file);
89  $this->js_file = $this->_getCompiledFileName($this->xml_file);
90  }
91 
96  function compile()
97  {
98  if(!file_exists($this->xml_file))
99  {
100  return;
101  }
102  if(!file_exists($this->js_file))
103  {
104  $this->_compile();
105  }
106  else if(filemtime($this->xml_file) > filemtime($this->js_file))
107  {
108  $this->_compile();
109  }
110  Context::loadFile(array($this->js_file, 'body', '', null));
111  }
112 
117  function _compile()
118  {
119  global $lang;
120 
121  // read xml file
122  $buff = FileHandler::readFile($this->xml_file);
123 
124  // xml parsing
125  $xml_obj = parent::parse($buff);
126 
127  $attrs = $xml_obj->filter->attrs;
128  $rules = $xml_obj->filter->rules;
129 
130  // XmlJsFilter handles three data; filter_name, field, and parameter
131  $filter_name = $attrs->name;
132  $confirm_msg_code = $attrs->confirm_msg_code;
133  $module = $attrs->module;
134  $act = $attrs->act;
135  $extend_filter = $attrs->extend_filter;
136 
137 
138  $field_node = $xml_obj->filter->form->node;
139  if($field_node && !is_array($field_node))
140  {
141  $field_node = array($field_node);
142  }
143 
144  $parameter_param = $xml_obj->filter->parameter->param;
145  if($parameter_param && !is_array($parameter_param))
146  {
147  $parameter_param = array($parameter_param);
148  }
149 
150  $response_tag = $xml_obj->filter->response->tag;
151  if($response_tag && !is_array($response_tag))
152  {
153  $response_tag = array($response_tag);
154  }
155 
156  // If extend_filter exists, result returned by calling the method
157  if($extend_filter)
158  {
159  // If extend_filter exists, it changes the name of cache not to use cache
160  $this->js_file .= '.nocache.js';
161 
162  // Separate the extend_filter
163  list($module_name, $method) = explode('.', $extend_filter);
164 
165  // contibue if both module_name and methos exist.
166  if($module_name && $method)
167  {
168  // get model object of the module
169  $oExtendFilter = getModel($module_name);
170 
171  // execute if method exists
172  if(method_exists($oExtendFilter, $method))
173  {
174  // get the result
175  $extend_filter_list = $oExtendFilter->{$method}(TRUE);
176  $extend_filter_count = count($extend_filter_list);
177 
178  // apply lang_value from the result to the variable
179  for($i = 0; $i < $extend_filter_count; $i++)
180  {
181  $name = $extend_filter_list[$i]->name;
182  $lang_value = $extend_filter_list[$i]->lang;
183  if($lang_value)
184  {
185  $lang->{$name} = $lang_value;
186  }
187  }
188  }
189  }
190  }
191 
192  // search the field to be used for entering language
193  $target_list = array();
194  $target_type_list = array();
195 
196  // javascript contents
197  $js_rules = array();
198  $js_messages = array();
199 
200  $fields = array();
201 
202  // create custom rule
203  if($rules && $rules->rule)
204  {
205  if(!is_array($rules->rule))
206  {
207  $rules->rule = array($rules->rule);
208  }
209  foreach($rules->rule as $r)
210  {
211  if($r->attrs->type == 'regex')
212  {
213  $js_rules[] = "v.cast('ADD_RULE', ['{$r->attrs->name}', {$r->body}]);";
214  }
215  }
216  }
217 
218  // generates a field, which is a script of the checked item
219  $node_count = count($field_node);
220  if($node_count)
221  {
222  foreach($field_node as $key => $node)
223  {
224  $attrs = $node->attrs;
225  $target = trim($attrs->target);
226 
227  if(!$target)
228  {
229  continue;
230  }
231 
232  $rule = trim($attrs->rule ? $attrs->rule : $attrs->filter);
233  $equalto = trim($attrs->equalto);
234 
235  $field = array();
236 
237  if($attrs->required == 'true')
238  {
239  $field[] = 'required:true';
240  }
241  if($attrs->minlength > 0)
242  {
243  $field[] = 'minlength:' . $attrs->minlength;
244  }
245  if($attrs->maxlength > 0)
246  {
247  $field[] = 'maxlength:' . $attrs->maxlength;
248  }
249  if($equalto)
250  {
251  $field[] = "equalto:'{$attrs->equalto}'";
252  }
253  if($rule)
254  {
255  $field[] = "rule:'{$rule}'";
256  }
257 
258  $fields[] = "'{$target}': {" . implode(',', $field) . "}";
259 
260  if(!in_array($target, $target_list))
261  {
262  $target_list[] = $target;
263  }
264  if(!$target_type_list[$target])
265  {
266  $target_type_list[$target] = $filter;
267  }
268  }
269  }
270 
271  // Check extend_filter_item
272  $rule_types = array('homepage' => 'homepage', 'email_address' => 'email');
273 
274  for($i = 0; $i < $extend_filter_count; $i++)
275  {
276  $filter_item = $extend_filter_list[$i];
277  $target = trim($filter_item->name);
278 
279  if(!$target)
280  {
281  continue;
282  }
283 
284  // get the filter from the type of extend filter item
285  $type = $filter_item->type;
286  $rule = $rule_types[$type] ? $rule_types[$type] : '';
287  $required = ($filter_item->required == 'true');
288 
289  $field = array();
290  if($required)
291  {
292  $field[] = 'required:true';
293  }
294  if($rule)
295  {
296  $field[] = "rule:'{$rule}'";
297  }
298  $fields[] = "\t\t'{$target}' : {" . implode(',', $field) . "}";
299 
300  if(!in_array($target, $target_list))
301  {
302  $target_list[] = $target;
303  }
304  if(!$target_type_list[$target])
305  {
306  $target_type_list[$target] = $type;
307  }
308  }
309 
310  // generates parameter script to create dbata
311  $rename_params = array();
312  $parameter_count = count($parameter_param);
313  if($parameter_count)
314  {
315  // contains parameter of the default filter contents
316  foreach($parameter_param as $key => $param)
317  {
318  $attrs = $param->attrs;
319  $name = trim($attrs->name);
320  $target = trim($attrs->target);
321 
322  //if($name && $target && ($name != $target)) $js_doc[] = "\t\tparams['{$name}'] = params['{$target}']; delete params['{$target}'];";
323  if($name && $target && ($name != $target))
324  {
325  $rename_params[] = "'{$target}':'{$name}'";
326  }
327  if($name && !in_array($name, $target_list))
328  {
329  $target_list[] = $name;
330  }
331  }
332 
333  // Check extend_filter_item
334  for($i = 0; $i < $extend_filter_count; $i++)
335  {
336  $filter_item = $extend_filter_list[$i];
337  $target = $name = trim($filter_item->name);
338  if(!$name || !$target)
339  {
340  continue;
341  }
342 
343  if(!in_array($name, $target_list))
344  {
345  $target_list[] = $name;
346  }
347  }
348  }
349 
350  // generates the response script
351  $response_count = count($response_tag);
352  $responses = array();
353  for($i = 0; $i < $response_count; $i++)
354  {
355  $attrs = $response_tag[$i]->attrs;
356  $name = $attrs->name;
357  $responses[] = "'{$name}'";
358  }
359 
360  // writes lang values of the form field
361  $target_count = count($target_list);
362  for($i = 0; $i < $target_count; $i++)
363  {
364  $target = $target_list[$i];
365  if(!$lang->{$target})
366  {
367  $lang->{$target} = $target;
368  }
369  $text = preg_replace('@\r?\n@', '\\n', addslashes($lang->{$target}));
370  $js_messages[] = "v.cast('ADD_MESSAGE',['{$target}','{$text}']);";
371  }
372 
373  // writes the target type
374  /*
375  $target_type_count = count($target_type_list);
376  if($target_type_count) {
377  foreach($target_type_list as $target => $type) {
378  //$js_doc .= sprintf("target_type_list[\"%s\"] = \"%s\";\n", $target, $type);
379  }
380  }
381  */
382 
383  // writes error messages
384  foreach($lang->filter as $key => $val)
385  {
386  if(!$val)
387  {
388  $val = $key;
389  }
390  $val = preg_replace('@\r?\n@', '\\n', addslashes($val));
391  $js_messages[] = sprintf("v.cast('ADD_MESSAGE',['%s','%s']);", $key, $val);
392  }
393 
394  $callback_func = $xml_obj->filter->response->attrs->callback_func;
395  if(!$callback_func)
396  {
397  $callback_func = "filterAlertMessage";
398  }
399 
400  $confirm_msg = '';
401  if($confirm_msg_code)
402  {
403  $confirm_msg = $lang->{$confirm_msg_code};
404  }
405 
406  $jsdoc = array();
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);
415 
416  // generates the js file
417  FileHandler::writeFile($this->js_file, $jsdoc);
418  }
419 
425  function _getCompiledFileName($xml_file)
426  {
427  return sprintf('%s%s.%s.compiled.js', $this->compiled_path, md5($this->version . $xml_file), Context::getLangType());
428  }
429 
430 }
431 /* End of file XmlJsFilter.class.php */
432 /* Location: ./classes/xml/XmlJsFilter.class.php */
loadFile($args)
writeFile($filename, $buff, $mode="w")
readFile($filename)
getModel($module_name)
Definition: func.inc.php:145