function id2record($idstring) { $pairs = explode('&', $idstring); foreach ($pairs as $pair) { list($attname, $attval) = explode('=', $pair); $attname = urldecode($attname); $attval = urldecode($attval); $colVals[$attname] = $attval; } $rrecord = new Dataface_RelatedRecord($this->record, $this->relationship->_name); $rrecord->setValues($colVals); return $rrecord; }
function ¤t() { $rec = new Dataface_RelatedRecord($this->_record, $this->_relationshipName, $this->_records[current($this->_keys)]); $rec->setValues($this->_record->_relatedMetaValues[$this->_relationshipName][$this->_where][$this->_sort][current($this->_keys)]); return $rec; }
/** * * Saves the related record. * * @param values Associative array of values received from the submitted form. * */ function save($values) { import('Dataface/LinkTool.php'); $colVals = array(); /* * In case some values were not submitted, we will use the defaults (as specified in the relationships.ini * file for this relationship to fill in the blanks. */ if (isset($this->_relationship->_schema['new'])) { foreach ($this->_relationship->_schema['new'] as $key => $value) { if (!isset($values[$key])) { $values[$key] = $value; } } } $io = new Dataface_IO($values['-table']); // for writing the related record $record = new Dataface_Record($values['-table'], array()); // The parent record... not the record being inserted. $io->read($values['__keys__'], $record); // We submitted the keys to the parent record in the form // so that we can load the parent record of this related record. // convert groups foreach (array_keys($values) as $key) { if (isset($this->_groups[$key])) { foreach ($values[$key] as $fieldkey => $fieldval) { $values[$fieldkey] = $fieldval; } unset($values[$key]); } } foreach ($values as $key => $value) { // We will go through each submitted value from the form and try // to figure out how it should be stored. if (strpos($key, '-') === 0) { continue; } if ($key == "-Save") { continue; } // We don't parse the "Save" button $fullPath = $this->_relationshipName . '.' . $key; // The full path to the field can be used to obtain information // about the field from the parent table, since most methods // in the table class will take field names of the form // <relationship name>.<fieldname> if (!$this->_parentTable->exists($fullPath)) { /* * If the field in question does not exist then we just skip it. * Perhaps we should throw an error?!! * */ //echo $fullPath.' does not exist in table '.$this->_parentTable->tablename; continue; } // At this point we know that the field exists so lets obtain references // to the useful components for us to work with this field. if (isset($field)) { unset($field); } $field =& $this->_parentTable->getField($fullPath); // Field array with data about the field. if (PEAR::isError($field)) { throw new Exception("Error obtaining field '{$fullPath}' while saving related record.", E_USER_ERROR); } $abs_fieldName = $this->_parentTable->absoluteFieldName($key, $this->_relationship->_schema['selected_tables']); // The absolute fieldname of this field. e.g., of the form <Tablename>.<Fieldname> if (PEAR::isError($abs_fieldName)) { throw new Exception("Error trying to obtain absolute field name for the related field: '{$fullPath}'", E_USER_ERROR); } list($tablename, $fieldname) = explode('.', $abs_fieldName); if (isset($table)) { unset($table); } $table =& Dataface_Table::loadTable($tablename); // Reference to the table object where this field resides if (isset($quickForm)) { unset($quickForm); } $quickForm =& $this->_quickForms[$tablename]; // QuickForm object for this field's table. $el = $this->getElement($key); $metaValues = array(); // The $metaValues array will store the meta values associated with // the current field. A meta value is an associated value that // should be stored in another field. For example the mimetype // is a metavalue for a file upload field. The $metaValues array // be of the form [Column Name] -> [Column Value]. // Get the absolute field name of the field. An absolute field name is // of the form <Tablename>.<Fieldname> $tempVal = $quickForm->pushValue($fieldname, $metaValues, $el); //$serializedValue; // $tempVal will contain the value as submitted by the form.. ready // to be added to a record. // The QuickForm element. // !!! Just changed arg from $abs_fieldName to $fullPath to fix errors... but still don't // !!! fully understand what was going on - or why it was working before!?!?! if ($this->_parentTable->isMetaField($fullPath)) { // If this is a meta field, we don't insert it on its own... // we will wait until its value is supplied by its described // field. unset($tempVal); unset($el); continue; } foreach ($metaValues as $metaKey => $metaValue) { // Set the meta values $colVals[$tablename . '.' . $metaKey] = $metaValue; } $colVals[$abs_fieldName] = $tempVal; // Add the value to the array to be saved in the RelatedRecord // object. // Note that right now, Dataface_RelatedRecord will just ignore // the part of the field name before the period, but in the future, // this extra information may be used to allow multiple fields // with the same name from different tables in a single relationship. unset($tempVal); } //$queryBuilder = new Dataface_QueryBuilder($this->_parentTable->tablename); $relatedRecord = new Dataface_RelatedRecord($record, $this->_relationshipName, array()); $relatedRecord->setValues($colVals); $res = $io->addRelatedRecord($relatedRecord, true); if (PEAR::isError($res)) { return $res; } //$res = $io->performSQL($sql); return $res; }
/** * Returns a record or record value given it's unique URI. * @param string $uri The URI of the data we wish to retrieve. * The URI must be of one of the following forms: * tablename?key1=val1&keyn=valn#fieldname * tablename?key1=val1&keyn=valn * tablename/relationshipname?key1=val1&keyn=valn&relationshipname::relatedkey=relatedval#fieldname * tablename/relationshipname?key1=val1&keyn=valn&relationshipname::relatedkey=relatedval * * Where url encoding is used as in normal HTTP urls. If a field is specified (after the '#') * * @param string $filter The name of a filter to pass the data through. This * is only applicable when a field name is specified. Possible filters * include: * strval - Returns the string value of the field. (aka stringValue, getValueAsString) * display - Returns the display value of the field. (This substitutes valuelist values) * htmlValue - Returns the html value of the field. * preview - Returns the preview value of the field (usually this limits * the length of the output and strips any HTML. * * @returns mixed Either a Dataface_Record object, a Dataface_RelatedRecord object * of a value as stored in the object. The output depends on * the input. If it receives invalid input, it will return a PEAR_Error * object. * * Example usage: * * <code> * // Get record from Users table with UserID=10 * $user =& Dataface_IO::getByID('Users?UserID=10'); * // Dataface_Record object * * // get birthdate of user with UserID=10 * $birthdate =& Dataface_IO::getByID('Users?UserID=10#birthdate'); * // array('year'=>'1978','month'=>'12','day'=>'27', ...) * * // get related record from jobs relationship of user with UserID=10 * // where the jobtitle is "cook" * $job =& Dataface_IO::getByID('Users?UserID=10&jobs::jobtitle=cook"); * // Dataface_RelatedRecord object * * // Get the employers name of the cook job * $employername = Dataface_IO::getByID('Users?UserID=10&jobs::jobtitle=cook#employername'); * // String * * // Add filter, so we get the HTML value of the bio field rather than just * // the raw value. * $bio = Dataface_IO::getByID('Users?UserID=10#bio', 'htmlValue'); * * </code> */ static function &getByID($uri, $filter = null) { if (strpos($uri, '?') === false) { return PEAR::raiseError("Invalid record id: " . $uri); } $uri_parts = df_parse_uri($uri); if (PEAR::isError($uri_parts)) { return $uri_parts; } if (!isset($uri_parts['relationship'])) { // This is just requesting a normal record. // Check to see if this is to be a new record or an existing record if (@$uri_parts['action'] and $uri_parts['action'] == 'new') { $record = new Dataface_Record($uri_parts['table'], array()); $record->setValues($uri_parts['query']); return $record; } foreach ($uri_parts['query'] as $ukey => $uval) { if ($uval and $uval[0] != '=') { $uval = '=' . $uval; } $uri_parts['query'][$ukey] = $uval; } // At this point we are sure that this is requesting an existing record $record =& df_get_record($uri_parts['table'], $uri_parts['query']); if (isset($uri_parts['field'])) { if (isset($filter) and method_exists($record, $filter)) { $val =& $record->{$filter}($uri_parts['field']); return $val; } else { $val =& $record->val($uri_parts['field']); return $val; } } else { return $record; } } else { // This is requesting a related record. $record =& df_get_record($uri_parts['table'], $uri_parts['query']); if (!$record) { return PEAR::raiseError("Could not find any records matching the query"); } // Check to see if we are creating a new record if (@$uri_parts['action'] and $uri_parts['action'] == 'new') { $related_record = new Dataface_RelatedRecord($record, $uri_parts['relationship']); $related_record->setValues($uri_parts['query']); return $related_record; } // At this point we can be sure that we are requesting an existing record. $related_records =& $record->getRelatedRecordObjects($uri_parts['relationship'], 0, 1, $uri_parts['related_where']); if (count($related_records) == 0) { return PEAR::raiseError("Could not find any related records matching the query: " . $uri_parts['related_where']); } if (isset($uri_parts['field'])) { if (isset($filter) and method_exists($related_records[0], $filter)) { $val =& $related_records[0]->{$filter}($uri_parts['field']); return $val; } else { $val =& $related_records[0]->val($uri_parts['field']); return $val; } } else { return $related_records[0]; } } }
function commit(&$grid) { $columnnames = array_keys($grid->columns); if ($this->recordid == '__new__') { // this is a new record - so we must create a new one. $parentObj =& $grid->getParentObject(); if (is_a($parentObj, 'Dataface_Table')) { $record = new Dataface_Record($parentObj->tablename, array()); } else { $record = new Dataface_RelatedRecord($parentObj, $grid->relationship, array()); } } else { $record =& df_get_record_by_id($this->recordid); } $rowdata =& $grid->data[$this->rowid]; $savedata = array(); foreach ($this->params['cells'] as $key) { $savedata[$key] = $rowdata[$key]; } $record->setValues($savedata); if ($this->recordid == '__new__' and is_a($record, 'Dataface_RelatedRecord')) { import('Dataface/IO.php'); $io = new Dataface_IO($parentObj->_table->tablename); $io->addRelatedRecord($record); } else { $record->save(); } }
function testAddRelatedRecord() { $fragrance = df_get_record('fragrances', array('fragrance_id' => '=1')); $this->assertTrue($fragrance instanceof Dataface_Record, 'Loaded fragrance should be a Dataface_Record object.'); $formulasRelationship = $fragrance->table()->getRelationship('formulas'); $this->assertTrue($formulasRelationship instanceof Dataface_Relationship, 'The formulas relationship does not exist or could not be loaded.'); $relatedRecord = new Dataface_RelatedRecord($fragrance, 'formulas'); $this->assertTrue(!$relatedRecord->isDirty('formula_name'), 'Record should not be dirty when it is first created.'); $formula = $relatedRecord->toRecord('formulas'); $this->assertTrue($formula instanceof Dataface_Record, 'Formula should be a Dataface_Record'); $records = $relatedRecord->toRecords(); $this->assertTrue($records[0] instanceof Dataface_Record, 'Formulas record from toRecords() should be of type Dataface_Record.'); $relatedRecord->setValues(array('formula_name' => 'Test formula', 'formula_description' => 'This is just a test formula', 'ingredients' => array(0 => array('ingredient_id' => 3, 'concentration' => 10, 'concentration_units' => 1, 'amount' => 10, 'amount_units' => 2, '__id__' => 'new', '__order__' => 0), '__loaded__' => 1))); $this->assertTrue($relatedRecord->isDirty('formula_name'), 'The formula name should be dirty before it is saved.'); $this->assertTrue($relatedRecord->isDirty('ingredients'), 'The ingredients should be dirty befor it is saved.'); $io = new Dataface_IO('fragrances'); $res = $io->addRelatedRecord($relatedRecord); $this->assertTrue(!PEAR::isError($res), 'The result of saving a related formula should not be an error.'); }