Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
form_question_rule_type_email.inc
1 <?php
18 require_once dirname(__FILE__).'/../../form_question_rule/form_question_rule.inc';
19 require_once SQ_FUDGE_PATH.'/general/www.inc';
20 
21 
34 {
35 
36 
43  function __construct($assetid=0)
44  {
45  parent::__construct($assetid);
46 
47  }//end constructor
48 
49 
59  public static function getOperators()
60  {
61  return Array(
62  1 => translate('core_form_rule_email_true'),
63  0 => translate('core_form_rule_email_false'),
64  );
65 
66  }//end getOperators()
67 
68 
78  function generateJSCode(&$q_asset, $rule_data)
79  {
80  ob_start(); // buffer this please
81 
82  $operator = array_get_index($rule_data, 'operator', 1);
83  $function_id = preg_replace('/[^a-zA-Z0-9]/', '_', $q_asset->id);
84 
85  $local = '\da-zA-Z-_+'; // allowed in the first char
86  $local_middle = $local.'.\w'; // allowed in the rest of the username
87  $regex_code = '/^(['.$local.']['.$local_middle.'\']*['.$local.']@[\da-zA-Z][\'-.\w]*[\da-zA-Z]\.[a-zA-Z]{2,7})$/';
88  ?>
89  function sq_form_validate_email_<?php echo $function_id; ?>(answer)
90  {
91  <?php
92  if (!$q_asset->attr('is_required')) {
93  ?>
94  if (answer.length == 0) {
95  return true;
96  }
97  <?php
98  }
99  ?>
100  answer.replace(/,/, "");
101  return answer.match(<?php echo $regex_code ?>);
102 
103  }//end sq_form_validate_email()
104 
105  if (<?php echo ($operator == 1) ? '!' : ''; ?>sq_form_validate_email_<?php echo $function_id; ?>(form.elements["q<?php echo $q_asset->id; ?>"].value)) {
106  submission_errors[submission_errors.length] = "<?php
107  if (empty($rule_data['custom_text'])) {
108  echo addslashes($this->defaultError($q_asset, $rule_data));
109  } else {
110  echo addslashes($rule_data['custom_text']);
111  }
112  ?>";
113  }
114  <?php
115 
116  $contents = ob_get_contents();
117  ob_end_clean();
118 
119  return $contents;
120 
121  }//end generateJSCode()
122 
123 
136  function evaluate($answer, $rule_data)
137  {
138  $operator = array_get_index($rule_data, 'operator', 1);
139 
140  $valid = valid_email($answer);
141  return ($valid == $operator);
142 
143  }//end evaluate()
144 
145 
155  function ruleDescription(&$q_asset, $rule_data)
156  {
157  $error_str = translate('core_form_rule_email_desc_'.(array_get_index($rule_data, 'operator', 1) ? 'true' : 'false'), $q_asset->attr('name'));
158 
159  return $error_str;
160 
161  }//end ruleDescription()
162 
163 
164 }//end class
165 ?>