/** * Adds a value to a valuelist. This only works for valuelists * that are pulled from the database. * @param Dataface_Table The table to add the valuelist to. * @param string $valuelistName The name of the valuelist. * @param string $value The value to add. * @param string $key The key to add. * @param boolean $checkPerms If true, this will first check permissions * before adding the value. * @returns mixed May return a permission denied error if there is insufficient * permissions. */ function addValueToValuelist(&$table, $valuelistName, $value, $key = null, $checkPerms = false) { import('Dataface/ConfigTool.php'); $configTool =& Dataface_ConfigTool::getInstance(); $conf = $configTool->loadConfig('valuelists', $table->tablename); $relname = $valuelistName . '__valuelist'; //$conf = array($relname=>$conf); $table->addRelationship($relname, $conf[$valuelistName]); $rel =& $table->getRelationship($relname); $fields =& $rel->fields(); if (count($fields) > 1) { $valfield = $fields[1]; $keyfield = $fields[0]; } else { $valfield = $fields[0]; $keyfield = $fields[0]; } $record = new Dataface_Record($table->tablename); $rrecord = new Dataface_RelatedRecord($record, $relname); if ($checkPerms and !$rrecord->checkPermission('edit', array('field' => $valfield))) { return Dataface_Error::permissionDenied(); } $rrecord->setValue($valfield, $value); if (isset($key) and isset($keyfield)) { if ($checkPerms and !$rrecord->checkPermission('edit', array('field' => $keyfield))) { return Dataface_Error::permissionDenied(); } $rrecord->setValue($keyfield, $key); } import('Dataface/IO.php'); $io = new Dataface_IO($table->tablename); $res = $io->addRelatedRecord($rrecord); if (PEAR::isError($res)) { return $res; } return array('key' => $rrecord->val($keyfield), 'value' => $rrecord->val($valfield)); }
/** * * 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; }
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 test_add_related_record() { $io = new Dataface_IO('Profiles', $this->db); $record = new Dataface_Record('Profiles', array()); $io->read(array('id' => 10), $record); $relatedRecord = new Dataface_RelatedRecord($record, 'courses', array('dept' => 'MATH', 'coursenumber' => 268)); ob_start(); $res = $io->addRelatedRecord($relatedRecord); $buffer = ob_get_contents(); ob_end_clean(); $this->assertEquals(' beforeAddRelatedRecord beforeAddNewRelatedRecord afterAddNewRelatedRecord afterAddRelatedRecord', $buffer); $this->assertTrue(!PEAR::isError($res)); $cio = new Dataface_IO('Courses', $this->db); $course = new Dataface_Record('Courses', array()); $cio->read(array('dept' => 'MATH', 'coursenumber' => 268), $course); $this->assertTrue($course->val('id') > 0, "Course ID inserted must have id > 0"); $this->assertTrue('MATH', $course->val('dept')); $this->assertTrue('268', $course->val('coursenumber')); unset($record); $record = new Dataface_Record('Profiles', array()); $io->read(array('id' => 10), $record); $it =& $record->getRelationshipIterator('courses'); $found = false; while ($it->hasNext()) { $nex =& $it->next(); if ($nex->val('dept') == 'MATH' and $nex->val('coursenumber') == 268) { $found = true; } unset($nex); } $this->assertTrue($found, "Added course did not show up in list of related records."); }
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.'); }