31 import(
'HTML/QuickForm.php');
32 import(
'Dataface/Table.php');
33 import(
'Dataface/Vocabulary.php');
34 import(
'Dataface/QueryBuilder.php');
35 import(
'Dataface/QueryTool.php');
36 import(
'Dataface/IO.php');
37 import(
'Dataface/SkinTool.php');
38 import(
'HTML/QuickForm/Renderer/Dataface.php');
39 import(
'Dataface/PermissionsTool.php');
40 import(
'Dataface/FormTool.php');
44 $GLOBALS[
'HTML_QUICKFORM_ELEMENT_TYPES'][
'htmlarea'] = array(
'HTML/QuickForm/htmlarea.php',
'HTML_QuickForm_htmlarea');
45 $GLOBALS[
'HTML_QUICKFORM_ELEMENT_TYPES'][
'table'] = array(
'HTML/QuickForm/table.php',
'HTML_QuickForm_table');
46 $GLOBALS[
'HTML_QUICKFORM_ELEMENT_TYPES'][
'calendar'] = array(
'HTML/QuickForm/calendar.php',
'HTML_QuickForm_calendar');
47 $GLOBALS[
'HTML_QUICKFORM_ELEMENT_TYPES'][
'time'] = array(
'HTML/QuickForm/time.php',
'HTML_QuickForm_time');
48 $GLOBALS[
'HTML_QUICKFORM_ELEMENT_TYPES'][
'webcam'] = array(
'HTML/QuickForm/webcam.php',
'HTML_QuickForm_webcam');
49 $GLOBALS[
'HTML_QUICKFORM_ELEMENT_TYPES'][
'portal'] = array(
'HTML/QuickForm/portal.php',
'HTML_QuickForm_portal');
50 define(
'QUICKFORM_AMBIGUOUS_FIELD_ERROR', 2);
51 define(
'QUICKFORM_NO_SUCH_FIELD_ERROR',3);
178 $appQuery =&
$app->getQuery();
179 if ( !isset($lang) && !isset($this->_lang) ){
180 $this->_lang =
$app->_conf[
'lang'];
181 }
else if ( isset($lang) ){
182 $this->_lang = $lang;
187 $this->tablename = $this->_record->_table->tablename;
188 $this->_table =& $this->_record->_table;
200 }
else if ( $query ){
201 $this->_record =& df_get_record(
$tablename, $query);
219 $formname =
"new_".$tablename.
"_record_form";
221 $formname =
"existing_".$tablename.
"_record_form";
225 if ( !
$db and defined(
'DATAFACE_DB_HANDLE') ){
227 $db = DATAFACE_DB_HANDLE;
233 $this->_query = is_array($query) ? $query : array();
235 if ( !isset( $this->_query[
'-cursor'] ) ){
236 $this->_query[
'-cursor'] = 0;
242 parent::HTML_QuickForm($formname,
'post', df_absolute_url(
$_SERVER[
'PHP_SELF']),
'',array(
'accept-charset'=>
$app->_conf[
'ie']),self::$TRACK_SUBMIT);
245 $this->_fields =& $this->_table->formFields(
false,
true);
251 $this->_requiredNote =
'';
254 if ( is_array($fieldnames) ){
259 $this->_fieldnames = array();
261 if ( isset($this->_fields[$fieldname]) ){
274 return ( isset( $_POST[
'__keys__']) and isset( $_POST[
'-table']) );
282 $query = $_POST[
'__keys__'];
284 if ( is_array($query) ){
285 foreach ( array_keys($query) as $postKey ){
286 if ( $query[$postKey]{0} !=
'=' ){
287 $query[$postKey] =
'='.$query[$postKey];
291 $io->read($query, $record);
299 $curr =& $qt->loadCurrent();
313 $this->submitLabel = $label;
334 $keys = array_keys($arr2);
335 foreach (
$keys as $key){
336 if ( !isset( $arr1[ $key ] ) ){
337 $arr1[ $key ] = $arr2[ $key ];
338 }
else if ( is_array( $arr1[ $key ] ) and is_array( $arr2[ $key ] ) ){
357 if ( func_num_args() > 1 ){
363 $permissions = func_get_arg(1);
377 $el =& $formTool->buildWidget($this->_record,
$field, $this,
$field[
'name'], $this->_new, $permissions);
409 if ( $this->_isBuilt ){
422 if ($this->_fieldnames === null or !is_array($this->_fieldnames) or count($this->_fieldnames)==0 ){
432 $this->_fieldnames = array();
433 foreach ($this->_fields as
$field){
434 if ( isset($this->tab) and ($this->tab != @$field[
'tab']) )
continue;
437 $this->_fieldnames[] = $field[
'name'];
446 $this->_isBuilt =
true;
449 if ( !$this->_record || !is_a($this->_record,
'Dataface_Record') ){
452 'Cannot build quickform with no record',
453 'Attempt to build quickform with no record set.'
459 $relationships =& $this->_table->relationships();
464 $groups = $formTool->groupFields($this->_fields);
465 foreach ( $groups as $sectionName =>
$fields ){
467 $group =& $this->_record->_table->getFieldgroup($sectionName);
470 $group = array(
'label'=>df_translate(
'scripts.Dataface_QuickForm.LABEL_EDIT_DETAILS',
'Edit Details'),
'order'=>1);
477 if ( !in_array($field[
'name'], $this->_fieldnames) )
continue;
483 $name = $field[
'name'];
485 $widget =& $field[
'widget'];
487 $vocabulary = $field[
'vocabulary'];
506 $headerel =& $this->addElement(
'header', $group[
'label'], $group[
'label']);
507 $headerel->setFieldDef($group);
518 $el = $this->
_buildWidget($field, $this->_record->getPermissions(array(
'field'=>$name)));
522 'scripts.Dataface.QuickForm._build.ERROR_FAILED_TO_BUILD_WIDGET',
523 "Failed to build widget for field $name ",
524 array(
'name'=>$name,
'line'=>0,
'file'=>
'_')
550 $factory =
new HTML_QuickForm(
'factory');
554 $keyDefaults = array();
555 foreach ( array_keys($this->_table->keys()) as $key ){
556 $keyEls[] = $factory->addElement(
'hidden', $key);
559 $this->addGroup($keyEls,
'__keys__');
568 $this->addElement(
'hidden',
'-new');
570 $this->setDefaults(array(
'-new'=>$this->_new));
573 $saveButtonLabel = df_translate(
'tables.'.$this->_table->tablename.
'.save_button_label',
'');
574 if ( !$saveButtonLabel ) $saveButtonLabel = df_translate(
'save_button_label',
'Save');
575 $this->addElement(
'submit',
'--session:save',$saveButtonLabel);
579 if ( $this->_new and !$this->overrideNoQuery){
581 $this->addElement(
'hidden',
'--no-query',1);
597 if ( $this->isSubmitted() and !$this->_new){
604 $key_vals = $this->exportValues(
'__keys__');
605 $query = $key_vals[
'__keys__'];
609 }
else if ( !$this->_new ){
616 foreach ( array_keys($this->_table->keys()) as $key ){
617 $keyDefaults[$key] = $this->_record->strval($key);
621 $this->setConstants( array(
'__keys__'=>$keyDefaults) );
627 foreach ( array_keys($this->_fields) as $key ){
628 $defaultValue = $this->_table->getDefaultValue($key);
629 if ( isset($defaultValue) ){
634 $defaults[$key] = $defaultValue;
639 $this->setDefaults($defaults);
675 $res = $formTool->pullField($this->_record,
$field, $this,
$fieldname, $this->_new);
739 'scripts.Dataface.QuickForm.push.ERROR_PUSHING_DATA',
740 "Error pushing data onto field $field in QuickForm::push()",
741 array(
'field'=>$field,
'line'=>0,
'file'=>
'_')
744 throw new Exception($res->toString(), E_USER_ERROR);
763 if ( $this->isSubmitted() ){
765 $res =
$app->fireEvent(
'Dataface_QuickForm_before_validate');
768 $this->_errors[] = $res->getMessage();
776 $rec =
new Dataface_Record($this->_record->_table->tablename, $this->getSubmitValues());
777 $rec->pouch = $this->_record->pouch;
778 foreach ($this->_fieldnames as
$field){
792 $params = array(
'message'=>df_translate(
'scripts.GLOBAL.MESSAGE.PERMISSION_DENIED',
"Permission Denied"));
795 $res = $rec->validate($field, $el->getValue(), $params );
806 $this->_errors[$el->getName()] = $params[
'message'];
837 $res = $formTool->pushField($this->_record,
$field, $this,
$fieldname, $this->_new);
860 if ( !isset($element) ) $element =& $formTool->getElement($this,
$field,
$fieldname);
862 $res = $formTool->pushValue($this->_record,
$field, $this, $element, $metaValues);
871 if ( $this->_resultSet->found()>0 ||
$this->_new ){
890 echo $formTool->display($this);
892 echo
"<p>".df_translate(
'scripts.GLOBAL.INSUFFICIENT_PERMISSIONS_TO_VIEW_RECORD',
'Sorry you have insufficient permissions to view this record.').
"</p>";
897 echo
"<p>".df_translate(
'scripts.GLOBAL.NO_RECORDS_MATCHED_REQUEST',
'No records matched your request.').
"</p>";
904 if ( isset($this->_record) ){
905 echo
"<ul id=\"quick-form-tabs\">";
906 foreach ( $this->_record->_table->getTabs() as
$tab ){
907 echo
"<li><a href=\"".$this->app->url(
'-tab='.
$tab).
"\">".
$tab.
"</a></li>";
916 if ( $fieldName && isset($this->_fields[$fieldName]) && isset($this->_fields[$fieldName][
'widget'][
'description']) ){
917 $widget = $this->_fields[$fieldName][
'widget'];
918 $description = $widget[
'description'];
924 $o =
"<div class=\"field\">
925 <label>{label}</label>
926 <!-- BEGIN required --><span style=\"color: #ff0000\" class=\"fieldRequired\" title=\"required\">*</span><!-- END required -->
927 <!-- BEGIN error --><div class=\"fieldError\" style=\"color: #ff0000\">{error}</div><!-- END error -->
928 <div class=\"formHelp\">$description</div>
937 $atts =& $this->_table->attributes();
938 $formname = $this->getAttribute(
'name');
940 <script language=
"javascript" type=
"text/javascript"><!--
945 document.{$formname}.elements[element_name].focus();
946 document.{$formname}.elements[element_name].select();
953 <legend>{$atts[
'label']}</legend>
954 <table width=
"100%" class=
"Dataface_QuickForm-table-wrapper">
964 $name = $this->_formatFieldName($name);
965 $group =& $this->_table->getFieldgroup($name);
968 $o =
"<tr><td colspan=\"2\"><fieldset class=\"fieldgroup\" style=\"border: 1px solid #8cacbb; margin: 0.5em;\">
969 <legend>".$group[
'label'].
"</legend>
970 <div class=\"formHelp\">".$group[
'description'].
"</div>
971 <table width=\"100%\" border=\"0\" class=\"Dataface_QuickForm-group-table-wrapper\">
975 </fieldset></td></tr>";
980 $name = $this->_formatFieldName($name);
981 $group =& $this->_table->getField($name);
983 $context = array(
'group'=>&$group,
'content'=>
'{content}');
986 $skinTool->display($context,
'Dataface_Quickform_group.html');
987 $o = ob_get_contents();
994 $groupName = $this->_formatFieldName($groupName);
995 $group =& $this->_table->getFieldgroup($groupName);
999 'scripts.Dataface.QuickForm.getGroupElementTemplate.ERROR_GETTING_FIELD_GROUP',
1000 "Error getting field group '$groupName' in QuickForm::getGroupElementTemplate() ",
1001 array(
'groupname'=>$groupName,
'line'=>0,
'file'=>
'_')
1010 if ( $group[
'element-description-visible'] ){
1011 $description =
"<div class=\"formHelp\" style=\"display: inline\">".$group[
'description'].
"</div>";
1014 if ( $group[
'element-lavel-visible'] ){
1018 if ( $fieldName && isset($this->_fields[$fieldName]) && isset($this->_fields[$fieldName][
'widget'][
'description']) ){
1019 $widget = $this->fields[$fieldName][
'widget'];
1020 $description = $widget[
'description'];
1026 $o =
"<div class=\"field\">
1027 <label>{label}</label>
1028 <!-- BEGIN required --><span style=\"color: #ff0000\" class=\"fieldRequired\" title=\"required\">*</span><!-- END required -->
1029 <!-- BEGIN error --><div class=\"fieldError\" style=\"color: #ff0000\">{error}</div><!-- END error -->
1030 <div class=\"formHelp\">$description</div>
1047 if ( !isset( $values[
'__keys__'] ) ){
1048 throw new Exception(
1050 'scripts.Dataface.QuickForm.save.ERROR_SAVING_RECORD',
1051 "Error saving record in QuickForm::save().\n<br>"
1054 if ( array_keys($values[
'__keys__']) != array_keys($this->_table->keys()) ){
1055 throw new Exception(
1057 'scripts.Dataface.QuickForm.save.ERROR_SAVING_RECORD',
1058 "Error saving record in QuickForm::save().\n<br>"
1065 $this->_record->clearValues();
1068 $res = $this->push();
1070 if ( !$this->_new ){
1071 if ( $this->_record->snapshotExists() ){
1073 $tempRecord =
new Dataface_Record($this->_record->_table->tablename, $this->_record->getSnapshot());
1075 $tempRecord =& $this->_record;
1077 if ( $values[
'__keys__'] != $tempRecord->strvals(array_keys($this->_record->_table->keys())) ){
1078 throw new Exception(
1080 'scripts.Dataface.QuickForm.save.ERROR_SAVING_RECORD',
1081 "Error saving record in QuickForm::save().\n<br>"
1092 'scripts.Dataface.QuickForm.save.ERROR_PUSHING_DATA',
1093 "Error pushing data from form onto table in QuickForm::save() ",
1094 array(
'line'=>0,
'file'=>
"_")
1106 foreach ( $this->_fields as $changedfield ){
1107 if ( $this->_record->valueChanged($changedfield[
'name']) ){
1108 $this->_changed_fields[] = $changedfield[
'name'];
1113 $io->lang = $this->_lang;
1114 if ( $this->_new )
$keys = null;
1115 else $keys = $values[
'__keys__'];
1117 $res = $io->write($this->_record,
$keys,null,
true );
1132 'scripts.Dataface.QuickForm.save.ERROR_SAVING_RECORD',
1133 "Error saving form in QuickForm::save()",
1134 array(
'line'=>0,
'file'=>
"_")
1137 throw new Exception($res->toString(), E_USER_ERROR);
1143 if ( isset( $io->insertIds[$this->tablename]) and $this->_table->getAutoIncrementField() ){
1144 $this->_record->setValue($this->_table->getAutoIncrementField(), $io->insertIds[
$this->tablename]);
1145 $this->_record->setSnapshot();
1161 foreach ($this->_fields as $key=>$value){
1162 if ( strtolower($value[
'Key']) == strtolower(
'PRI') ){
1163 $keys[$key] =& $this->_fields[$key];
1174 return Dataface_Table::_deserialize(
$field);
1185 return Dataface_Table::_serialize(
$field);
1265 $record =& $this->_record;
1266 foreach ($this->_changed_fields as
$fieldname){
1267 $vals[
$fieldname] = $record->htmlValue($fieldname);
1269 $vals[
'__id__'] = $record->getId();
1270 $vals[
'__url__'] = $record->getURL(
'-action=view');