18 require_once SQ_INCLUDE_PATH.
'/asset.inc';
19 require_once SQ_LIB_PATH.
'/html_form/html_form.inc';
54 var $section_name =
'';
75 var $extra_data = Array();
82 var $submission_errors = NULL;
89 var $active_form = NULL;
96 var $failed_rules = Array();
108 if (!$formid || empty($data)) {
111 $form = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($formid);
113 if (is_null($form))
return;
114 $this->_formid = $formid;
115 $this->_questionid = $data[
'questionid'];
120 $asset_attributes = $GLOBALS[
'SQ_SYSTEM']->am->getAssetTypeAttributes(get_class_lower($this), Array(
'name',
'type',
'default_val',
'attrid'));
122 foreach ($asset_attributes as $attribute_name => $asset_attribute) {
123 $this->vars[$attribute_name][
'type'] = $asset_attribute[
'type'];
124 $this->vars[$attribute_name][
'attrid'] = $asset_attribute[
'attrid'];
125 if (isset($data[
'attributes'][$attribute_name])) {
126 $this->vars[$attribute_name][
'value'] = (($asset_attribute[
'type'] ==
'serialise') ? unserialize($data[
'attributes'][$attribute_name]) : $data[
'attributes'][$attribute_name]);
128 $this->vars[$attribute_name][
'value'] = (($asset_attribute[
'type'] ==
'serialise') ? unserialize($asset_attribute[
'default_val']) : $asset_attribute[
'default_val']);
133 $this->
id = $formid.
':q'.$this->_questionid;
134 $this->name = $this->vars[
'name'][
'value'];
135 $this->short_name = $this->name;
136 $this->status = $form->status;
137 $this->version =
'0.1';
140 $this->vars[
'question_type_code'][
'value'] = get_class_lower($this);
141 $this->vars[
'question_type_code'][
'type'] =
'text';
162 if ($name ==
'name' && empty($value)) {
166 return parent::setAttrValue($name, $value);
186 if (!$this->
id)
return TRUE;
189 $form = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($this->_formid);
191 if (!$GLOBALS[
'SQ_REVERT_TO_SYSTEM_VERSION'] && !$form->writeAccess(
'attributes')) {
192 trigger_localised_error(
'CORE0121', E_USER_WARNING, $this->name, $this->
id);
196 $save_vars = Array();
200 $saved_vars = $this->vars;
202 unset($saved_vars[
'question_type_code']);
205 $GLOBALS[
'SQ_SYSTEM']->changeDatabaseConnection(
'db2');
206 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'BEGIN');
209 $sql =
'SELECT name, default_val FROM '.SQ_TABLE_RUNNING_PREFIX.
'ast_attr ';
210 $where =
'type_code = :type_code';
211 $where = $GLOBALS[
'SQ_SYSTEM']->constructRollbackWhereClause($where);
217 }
catch (Exception $e) {
218 throw new Exception(
'Can not get default val and name base on type code due to the following database error '.$e->getMessage());
222 foreach ($existing as $name => $default_val) {
223 if ($saved_vars[$name][
'value'] == $default_val[
'0'][
'default_val']) {
224 unset($saved_vars[$name]);
229 foreach ($saved_vars as $attr_id => $saved_var) {
230 $saved_vars_ser[$attr_id] = (($saved_vars[$attr_id][
'type'] ==
'serialise') ? serialize($saved_vars[$attr_id][
'value']) : $saved_vars[$attr_id][
'value']);
234 $questions = $form->attr(
'questions');
235 $questions[$this->_questionid][
'attributes'] = $saved_vars_ser;
237 $form->setAttrValue(
'questions', $questions);
239 if (!$form->saveAttributes()) {
240 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'ROLLBACK');
241 $GLOBALS[
'SQ_SYSTEM']->restoreDatabaseConnection();
246 if (!$dont_run_updated && !$this->
_updated()) {
247 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'ROLLBACK');
248 $GLOBALS[
'SQ_SYSTEM']->restoreDatabaseConnection();
252 unset($this->_tmp[
'vars_set']);
255 $GLOBALS[
'SQ_SYSTEM']->doTransaction(
'COMMIT');
256 $GLOBALS[
'SQ_SYSTEM']->restoreDatabaseConnection();
258 $GLOBALS[
'SQ_SYSTEM']->am->updateLock($this->_formid,
'attributes');
284 $form = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($this->_formid);
285 return $form->_updated($update_parents);
316 return $this->
attr(
'name');
338 switch (strtolower($field)) {
340 return $this->
attr(
'name');
345 return $this->
attr(
'note');
368 $options = $this->getOptions();
369 if (is_array($offset)) {
371 foreach ($offset as $o) {
372 if (isset($options[$o])) $answers[] = $options[$o];
376 if (isset($options[$offset]))
return $options[$offset];
393 if (!$answer)
return FALSE;
395 $options = $this->getOptions();
396 if (in_array($answer, $options)) {
397 return array_search($answer, $options);
412 if ($this instanceof $types)
return TRUE;
430 'form_question_type_select',
431 'form_question_type_tickbox_list',
432 'form_question_type_option_list',
433 'form_question_type_country',
450 if (empty($this->_formid))
return $asset;
452 $asset = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($this->_formid);
466 if (!$this->section_name) {
468 $this->section_name = $section->name;
470 return $this->section_name;
489 foreach ($rules as $k => $rule) {
490 $type_code =
'form_question_rule_type_'.$rule[
'rule_code'];
492 $GLOBALS[
'SQ_SYSTEM']->am->includeAsset($type_code);
493 $rule_asset =
new $type_code();
494 $code .= $rule_asset->generateJSCode($this, $rule);
531 function &
addRule($rule_code, $operator, $value)
533 $rules = $this->
attr(
'rules');
534 $rules[] = Array(
'rule_code' => $rule_code,
'operator' => $operator,
'value' => $value);
552 $rules = $this->
attr(
'rules');
553 foreach (array_keys($rules) as $rule_index) {
554 if ($rules[$rule_index] == $deleted_rule) {
555 unset($rules[$rule_index]);
574 return $this->
attr(
'rules');
631 if (isset($_POST[
'q'.$this->
id])) {
633 if(isset($parent) && $parent->attr(
'disable_keyword_replacements')) {
634 $value = $_POST[
'q'.$this->id];
637 if (is_array($value)) {
638 foreach ($value as $idx => $val) {
639 $keywords = extract_keywords($val);
640 foreach ($keywords as $keyword) {
641 $pattern =
'/(%*)'.$keyword.
'(%*)/';
642 $value[$idx] = preg_replace($pattern, $keyword, $val);
646 $keywords = extract_keywords($value);
647 foreach ($keywords as $keyword) {
648 $pattern =
'/(%*)'.$keyword.
'(%*)/';
649 $value = preg_replace($pattern, $keyword, $value);
654 $this->
setValue($_POST[
'q'.$this->
id]);
672 $this->value = $value;
687 $this->extra_data = $data;
709 if (is_null($answer)) $answer = $this->
getValue();
710 if ($mute_errors === FALSE) {
711 $this->failed_rules = Array();
717 if (!$this->
attr(
'is_required') && $answer ===
'') {
721 foreach ($rules as $k=>$rule) {
722 $type_code =
'form_question_rule_type_'.$rule[
'rule_code'];
724 $GLOBALS[
'SQ_SYSTEM']->am->includeAsset($type_code);
725 $rule_asset =
new $type_code();
726 $rule_ok = $rule_asset->evaluate($answer, $rule, $this);
731 $custom_text = array_get_index($rule,
'custom_text',
'');
732 if (!empty($custom_text)) {
733 $this->failed_rules[] = $rule[
'custom_text'];
735 $this->failed_rules[] = $rule_asset->defaultError($this, $rule);
774 require_once SQ_FUDGE_PATH.
'/general/text.inc';
778 if (is_null($this->value) && $this->
attr(
'sticky')) {
783 if (is_null($this->value)) {
785 $default_value = $this->
attr(
'default');
787 if (is_null($default_value)) {
791 if (!is_array($default_value)) {
792 $keywords = retrieve_keywords_replacements($default_value);
793 $replacements = Array();
795 foreach ($keywords as $keyword) {
796 if (preg_match(
'|^current_user_|', $keyword)) {
797 $user_keyword = preg_replace(
'|^current_user_|',
'asset_', $keyword);
798 $user = $GLOBALS[
'SQ_SYSTEM']->user;
801 if ($GLOBALS[
'SQ_SYSTEM']->userPublic()) {
802 $replacements[$keyword] =
'';
804 $replacements[$keyword] = $user->getKeywordReplacement($user_keyword);
811 replace_keywords($default_value, $replacements);
812 replace_global_keywords($default_value);
820 return $default_value;
823 if (is_array($this->value))
return $this->value;
852 return $this->extra_data;
867 echo
'<text_q id="'.addslashes($this->
id).
'" name="'.htmlspecialchars($this->
attr(
'name')).
'">';
871 $contents = ob_get_contents();
887 return $this->failed_rules;
906 $form = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($this->_formid);
907 if (is_null($form))
return FALSE;
909 return $form->readAccess($assetids);
928 $form = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($this->_formid);
929 if (is_null($form))
return FALSE;
931 return $form->writeAccess($lock_type, $assetids);
950 $form = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($this->_formid);
951 if (is_null($form))
return FALSE;
953 return $form->adminAccess($lock_type, $assetids);
973 if (empty($lock_type) || $this->canLiveEdit($lock_type)) {
974 return $this->
checkAccess(SQ_PERMISSION_WRITE, $lock_type);
998 $form = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($this->_formid);
999 if (is_null($form))
return FALSE;
1001 return $form->checkAccess($perm, $lock_type, $assetids);
1019 $form = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($this->_formid);
1020 if (is_null($form))
return FALSE;
1022 return $form->_checkPermissionAccess($perm, $assetids);
1038 $_SESSION[
'SQ_FORM_STICKY'][$GLOBALS[
'SQ_SYSTEM']->user->id][$cookie_name] = $value;
1052 unset($_SESSION[
'SQ_FORM_STICKY'][$GLOBALS[
'SQ_SYSTEM']->user->id][$cookie_name]);
1067 if (!isset($_SESSION[
'SQ_FORM_STICKY'][$GLOBALS[
'SQ_SYSTEM']->user->id][$cookie_name])) {
1071 $value = $_SESSION[
'SQ_FORM_STICKY'][$GLOBALS[
'SQ_SYSTEM']->user->id][$cookie_name];
1092 list($question_name) = make_valid_web_paths(Array($this->name));
1093 return $question_name;
1125 label($this->
attr(
'name'),
'q'.str_replace(
':',
'_', $this->
id));
1126 $label = ob_get_contents();