function save($values) { // Which ones were checked $checked = array_keys($values['--related-checkboxes']); // Which ones are currently part of the relationship $default = array_keys($this->getCheckedRecordsDefaults()); // Which ones need to be added? $toAdd = array_diff($checked, $default); // Which ones need to be removed? $toRemove = array_diff($default, $checked); // Now we go through and remove the ones that need to be removed. $io = new Dataface_IO($this->record->_table->tablename); $messages = array(); $successfulRemovals = 0; foreach ($toRemove as $id) { $res = $io->removeRelatedRecord($this->id2record($id)); if (PEAR::isError($res)) { $messages[] = $res->getMessage(); } else { $sucessfulRemovals++; } } // Now we go through and add the ones that need to be added. foreach ($toAdd as $id) { $res = $io->addExistingRelatedRecord($this->id2record($id)); if (PEAR::isError($res)) { $messages[] = $res->getMessage(); } else { $successfulAdditions++; } } array_unshift($messages, df_translate('scripts.Dataface_RelationshipCheckboxForm.MESSAGE_NUM_RECORDS_ADDED', $successfulAdditions . ' records were successfully added to the relationship.', array('num_added' => $successfulAdditions)), df_translate('scripts.Dataface_RelationshipCheckboxForm.MESSAGE_NUM_RECORDS_REMOVED', $successfulRemovals . ' records were successfully removed from the relationship.', array('num_removed' => $successfulRemovals))); $_SESSION['msg'] = '<ul><li>' . implode('</li><li>', $messages) . '</li></ul>'; header('Location: ' . $values['--query']); exit; }
function test_add_existing_related_record_trigger() { import('dataface-public-api.php'); $registration =& df_get_record('Registrations', array('RegistrationID' => 1, 'RegistrantID' => 1)); $product = new Dataface_RelatedRecord($registration, 'Products', array('ProductID' => 3)); $io = new Dataface_IO('Registrations'); $res = $io->addExistingRelatedRecord($product); $this->assertTrue(PEAR::isError($res)); $this->assertEquals("We don't really like this combination of product and registration", $res->getMessage()); }
/** * Saves the record. Ie: creates the necessary join table records to add the * desired record to the relationship. */ function save($values) { //print_r($values);exit; $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['existing'])) { foreach ($this->_relationship->_schema['existing'] as $key => $value) { if (!isset($values[$key])) { $values[$key] = $value; } } } $io = new Dataface_IO($values['-table']); $record = new Dataface_Record($values['-table'], array()); $io->read($values['__keys__'], $record); $idstring = $values['select']; $pairs = explode('&', $idstring); foreach ($pairs as $pair) { list($attname, $attval) = explode('=', $pair); $attname = urldecode($attname); $attval = urldecode($attval); $colVals[$attname] = $attval; } foreach ($values as $key => $value) { if (strpos($key, '-') === 0) { continue; } if ($key == "Save") { continue; } if ($key == "select") { continue; } $fullPath = $values['-relationship'] . '.' . $key; if (!$this->_parentTable->exists($fullPath)) { //echo "Field $fullPath does not exist"; continue; } $metaValues = array(); $abs_fieldName = $this->_parentTable->absoluteFieldName($key, array_merge(array($this->_relationship->getDomainTable()), $this->_relationship->_schema['selected_tables'])); if (PEAR::isError($abs_fieldName)) { continue; } $serializer = new Dataface_Serializer($this->_parentTable->tablename); //echo "Serializing $fullPath\n"; $serializedValue = $serializer->serialize($fullPath, $this->_quickForm->pushValue($fullPath, $metaValues, $this->getElement($key))); $colVals[$abs_fieldName] = $serializedValue; } //print_r($colVals);exit; $relatedRecord = new Dataface_RelatedRecord($record, $values['-relationship'], $colVals); $res = $io->addExistingRelatedRecord($relatedRecord, true); return $res; }
function save($values) { $app = Dataface_Application::getInstance(); // Which ones were checked $checked = array(); foreach ($values['--related-checkboxes'] as $k => $v) { if ($v) { $checked[] = $k; } } // Which ones are currently part of the relationship $default = array_keys($this->getCheckedRecordsDefaults()); // Which ones need to be added? $toAdd = array_diff($checked, $default); // Which ones need to be removed? $toRemove = array_diff($default, $checked); // Now we go through and remove the ones that need to be removed. $io = new Dataface_IO($this->record->_table->tablename); $messages = array(); $successfulRemovals = 0; $successfulAdditions = 0; foreach ($toRemove as $id) { $res = $io->removeRelatedRecord($this->id2record($id), false, true); if (PEAR::isError($res)) { $messages[] = $res->getMessage(); } else { $successfulRemovals++; } } // Now we go through and add the ones that need to be added. foreach ($toAdd as $id) { $res = $io->addExistingRelatedRecord($this->id2record($id), true); if (PEAR::isError($res)) { $messages[] = $res->getMessage(); } else { $successfulAdditions++; } } array_unshift($messages, df_translate('scripts.Dataface_RelationshipCheckboxForm.MESSAGE_NUM_RECORDS_ADDED', $successfulAdditions . ' records were successfully added to the relationship.', array('num_added' => $successfulAdditions)), df_translate('scripts.Dataface_RelationshipCheckboxForm.MESSAGE_NUM_RECORDS_REMOVED', $successfulRemovals . ' records were successfully removed from the relationship.', array('num_removed' => $successfulRemovals))); $_SESSION['--msg'] = '<ul><li>' . implode('</li><li>', $messages) . '</li></ul>'; $url = $values['--query']; $urlparts = parse_url($url); if ($urlparts and $urlparts['host'] and $urlparts['host'] != $_SERVER['HTTP_HOST']) { throw new Exception('Failed to redirect after action due to an invalid query parameter.', E_USER_ERROR); } $app->redirect($values['--query']); }