Ejemplo n.º 1
0
 /**
  * Builds the form.
  */
 function _build()
 {
     $app =& Dataface_Application::getInstance();
     $mainQuery =& $app->getQuery();
     if ($this->_built) {
         return true;
     }
     $r =& $this->_relationship->_schema;
     $t =& $this->_parentTable;
     $fkCols =& $this->_relatedRecord->getForeignKeyValues();
     if (PEAR::isError($fkCols)) {
         $fkCols->addUserInfo(df_translate('scripts.Dataface.ExistingRelatedRecordForm._build.ERROR_GETTING_FOREIGN_KEY_COLS', "Error getting foreign key columns while building Related Record Form on line " . __LINE__ . " of file " . __FILE__, array('line' => __LINE__, 'file' => __FILE__)));
         echo $fkCols->toString();
         return $fkCols;
     }
     $factory = new HTML_QuickForm('factory');
     $fkeys = $this->_relationship->getForeignKeyValues();
     // Values of foreign keys (fields involved in where and join clauses)
     $table = $this->_relationship->getDomainTable();
     // The name of the table holding related records.
     if (!isset($table) || PEAR::isError($table)) {
         $table = $r['selected_tables'][0];
     }
     // It is possible for getDomainTable() to return an error if no foreign
     // keys are specified.  In this case, we will just use the table associated
     // with the first selected column.
     $relatedTableObject =& Dataface_Table::loadTable($table);
     // The Dataface_Table object for the related records.
     $tkey_names = array_keys($relatedTableObject->keys());
     // The names of the key fields for the related record.
     // The main table that holds the related records
     $options = $this->_relationship->getAddableValues($this->_record);
     if (!$options) {
         return PEAR::raiseError('There are no records that can be added to this relationship.', DATAFACE_E_NOTICE);
     }
     $select =& $this->addElement('select', 'select', df_translate('scripts.Dataface.ExistingRelatedRecordForm._build.LABEL_SELECT', 'Select'), $options, array('class' => 'record_selector'));
     $permissions = $this->_record->getPermissions(array('relationship' => $this->_relationshipName));
     if (isset($permissions['add existing related record']) and $permissions['add existing related record']) {
         // We are allowed to add a new related record, so we will create a mask to allow this.
         $mask = array('edit' => 1);
     }
     // Now we still need to add fields so that the user can specify information about the relationship.
     // ie: some fields of the join table may be descriptive.
     foreach (array_keys($fkCols) as $fkTable) {
         if ($fkTable == $table) {
             // This table is the main domain table... we don't want to input any data for this table.
             continue;
         }
         $qfFactory = new Dataface_QuickForm($fkTable, $this->_parentTable->db);
         $tableRef =& Dataface_Table::loadTable($fkTable);
         $recordRef = new Dataface_Record($fkTable, array());
         $recordRef->setValues($fkCols[$fkTable]);
         $currFieldnames = array_keys($tableRef->fields());
         foreach ($currFieldnames as $currFieldname) {
             if (isset($fkCols[$fkTable][$currFieldname])) {
                 // this value is bound, and should not be changed.
                 continue;
             }
             $field =& $tableRef->getField($currFieldname);
             //$el = $qfFactory->_buildWidget($field, array_merge($mask, $this->_record->getPermissions(array('field'=>$this->_relationshipName.'.'.$currFieldname))));
             $el = $qfFactory->_buildWidget($field, $recordRef->getPermissions(array('field' => $currFieldname, 'recordmask' => $mask)));
             // To Do: Make it work with groups
             $this->addElement($el);
             /*
              *
              * If there are any validation options set for the field, we must add these rules to the quickform
              * element.
              *
              */
             $validators = $field['validators'];
             foreach ($validators as $vname => $validator) {
                 /*
                  *
                  * $validator['arg'] would be specified in the INI file.
                  * Example ini file listing:
                  * -------------------------
                  * [FirstName]
                  * widget:label = First name
                  * widget:description = Enter your first name
                  * validators:regex = "/[0-9a-zA-Z/"
                  *
                  * This would result in $validator['arg'] = "/[0-9a-zA-Z/" in this section
                  * and $vname == "regex".  Hence it would mean that a regular expression validator
                  * is being placed on this field so that only Alphanumeric characters are accepted.
                  * Please see documentation for HTML_QuickForm PEAR class for more information
                  * about QuickForm validators.
                  *
                  */
                 $this->addRule($field['name'], $validator['message'], $vname, $validator['arg'], 'client');
             }
             unset($field);
         }
         unset($tableRef);
         unset($qfFactory);
     }
     $keyEls = array();
     $keyDefaults = array();
     foreach (array_keys($this->_parentTable->keys()) as $key) {
         $keyEls[] = $factory->addElement('hidden', $key);
     }
     $this->addGroup($keyEls, '__keys__');
     $keyvals = array();
     foreach (array_keys($this->_parentTable->keys()) as $key) {
         $keyvals[$key] = $this->_record->getValueAsString($key);
     }
     $this->setDefaults(array('__keys__' => $keyvals));
     $this->addElement('hidden', '-table');
     $this->addElement('hidden', '-relationship');
     $this->addElement('hidden', '-action');
     $this->addElement('submit', 'Save', 'Save');
     $this->setDefaults(array('-table' => $this->_parentTable->tablename, '-relationship' => $this->_relationshipName, '-action' => "existing_related_record"));
     // Set the return page
     $returnPage = @$_SERVER['HTTP_REFERER'];
     if (isset($mainQuery['-redirect'])) {
         $returnPage = $mainQuery['-redirect'];
     } else {
         if (isset($mainQuery['--redirect'])) {
             $returnPage = $mainQuery['--redirect'];
         }
     }
     if (!$returnPage) {
         $returnPage = $app->url('-action=related_records_list&-relationship=' . $this->_relationshipname);
     }
     $this->addElement('hidden', '--redirect');
     $this->setDefaults(array('--redirect' => $returnPage));
     /*
      * There may be some default values specified in the relationship schema.
      */
     if (isset($r['existing'])) {
         $this->setDefaults($r['existing']);
     }
     $this->_built = true;
 }
Ejemplo n.º 2
0
 function _buildWidget($field)
 {
     // notice that we pass $field by value here- so we can make changes without changing it
     // throughout the rest of the application.
     $res =& parent::_buildWidget($field);
     if (is_a($res, 'HTML_QuickForm_element') and is_array($this->_dest_translatedFields) and !in_array($field['name'], $this->_dest_translatedFields)) {
         $res->freeze();
     }
     if ($field['widget']['type'] != 'hidden') {
         $res->setProperty('translation', $this->records[$this->sourceLanguage]->display($field['name']));
     }
     return $res;
 }