/** * Indexes a record so that it is searchable in the index. * If the index table does not exist yet, this will create it. * @param Dataface_Record &$record The record to be indexed. * @param string $lang The 2-digit language code representing the language * that this record is stored in. */ function indexRecord(&$record, $lang = '*') { $app =& Dataface_Application::getInstance(); if ($lang == '*') { // If the language specified is '*', that means we will // be indexing all languages. $this->indexRecord($record, $app->_conf['lang']); if (is_array($app->_conf['languages'])) { import('Dataface/IO.php'); $io = new Dataface_IO($record->_table->tablename); foreach (array_keys($app->_conf['languages']) as $lang) { if ($lang == $app->_conf['lang']) { continue; } $io->lang = $lang; $io->read($record->getId(), $record); $this->indexRecord($record, $lang); } } return true; } if (!isset($lang)) { $lang = $app->_conf['lang']; } $del =& $record->_table->getDelegate(); if (isset($del) and method_exists($del, 'getSearchableText')) { $searchable_text = $del->getSearchableText($record); if (!is_string($searchable_text)) { // If this method returns anything other than a string, // then we do not index the record... we just return false. return false; } } else { // The getSearchableText() method is not defined, so we will // just produce a concatenation of all text fields in the // record and index those. $fields = $record->_table->getCharFields(true); $searchable_text = implode(', ', $record->strvals($fields)); } // Add soundex information //$words = explode(' ', $searchable_text); //echo $searchable_text; $searchable_text = preg_replace('/<[^>]*?>/', ' ', $searchable_text); $words = preg_split('/((^\\p{P}+)|(\\p{P}*\\s+\\p{P}*)|(\\p{P}+$))/', $searchable_text, -1, PREG_SPLIT_NO_EMPTY); $soundexAddons = array(); foreach ($words as $word) { //echo htmlspecialchars("[Word $word] -> [".soundex(trim($word))."]"); $soundexAddons[] = soundex(trim($word)); } $searchable_text .= '[/////]: ' . implode(' ', $soundexAddons); $searchable_text = strip_tags($searchable_text); $sql = "\n\t\t\treplace into dataface__index \n\t\t\t(`record_id`,`table`,`record_url`,`record_title`,`record_description`,`lang`,`searchable_text`)\n\t\t\tvalues\n\t\t\t(\n\t\t\t'" . addslashes($record->getId()) . "',\n\t\t\t'" . addslashes($record->_table->tablename) . "',\n\t\t\t'" . addslashes($record->getPublicLink()) . "',\n\t\t\t'" . addslashes($record->getTitle()) . "',\n\t\t\t'" . addslashes(strip_tags($record->getDescription())) . "',\n\t\t\t'" . addslashes($lang) . "',\n\t\t\t'" . addslashes($searchable_text) . "'\n\t\t\t)"; if (!@xf_db_query($sql, df_db())) { $this->createIndexTable(); if (!xf_db_query($sql, df_db())) { trigger_error(xf_db_error(df_db()), E_USER_ERROR); } } return true; }
function test_values() { $record = new Dataface_Record('Profiles', array()); $io = new Dataface_IO('Profiles'); $io->read(array('id' => 10), $record); $this->assertEquals('10', $record->val('id')); $rr = new Dataface_RelatedRecord($record, 'appointments'); $this->assertEquals(10, $rr->val('profileid')); $this->assertEquals(10, $rr->getValue('profileid')); }
/** * Loads the parent record for this form. This may be called in static context. * This method is called in the constructor if no valid record is supplied. */ function &getRecord() { if (Dataface_ImportForm::formSubmitted()) { $record = new Dataface_Record($_POST['-table'], array()); $io = new Dataface_IO($_POST['-table']); $io->read($_POST['__keys__'], $record); return $record; } else { $app =& Dataface_Application::getInstance(); $qt =& Dataface_QueryTool::loadResult($app->_currentTable); $record =& $qt->loadCurrent(); return $record; } }
/** * Indexes a record so that it is searchable in the index. * If the index table does not exist yet, this will create it. * @param Dataface_Record &$record The record to be indexed. * @param string $lang The 2-digit language code representing the language * that this record is stored in. */ function indexRecord(&$record, $lang = '*') { $app =& Dataface_Application::getInstance(); if ($lang == '*') { // If the language specified is '*', that means we will // be indexing all languages. $this->indexRecord($record, $app->_conf['lang']); if (is_array($app->_conf['languages'])) { import('Dataface/IO.php'); $io = new Dataface_IO($record->_table->tablename); foreach (array_keys($app->_conf['languages']) as $lang) { if ($lang == $app->_conf['lang']) { continue; } $io->lang = $lang; $io->read($record->getId(), $record); $this->indexRecord($record, $lang); } } return true; } if (!isset($lang)) { $lang = $app->_conf['lang']; } $del =& $record->_table->getDelegate(); if (isset($del) and method_exists($del, 'getSearchableText')) { $searchable_text = $del->getSearchableText($record); if (!is_string($searchable_text)) { // If this method returns anything other than a string, // then we do not index the record... we just return false. return false; } } else { // The getSearchableText() method is not defined, so we will // just produce a concatenation of all text fields in the // record and index those. $fields = $record->_table->getCharFields(true); $searchable_text = implode(', ', $record->strvals($fields)); } $sql = "\n\t\t\treplace into dataface__index \n\t\t\t(`record_id`,`table`,`record_url`,`record_title`,`record_description`,`lang`,`searchable_text`)\n\t\t\tvalues\n\t\t\t(\n\t\t\t'" . addslashes($record->getId()) . "',\n\t\t\t'" . addslashes($record->_table->tablename) . "',\n\t\t\t'" . addslashes($record->getPublicLink()) . "',\n\t\t\t'" . addslashes($record->getTitle()) . "',\n\t\t\t'" . addslashes(strip_tags($record->getDescription())) . "',\n\t\t\t'" . addslashes($lang) . "',\n\t\t\t'" . addslashes($searchable_text) . "'\n\t\t\t)"; if (!@mysql_query($sql, df_db())) { $this->createIndexTable(); if (!mysql_query($sql, df_db())) { trigger_error(mysql_error(df_db()), E_USER_ERROR); } } return true; }
/** * Gets an HTML diff output between the records at $id1 and $id2 * respectively, where $id1 and $id2 are history ids from the history__id * column of the history table. * @param string $tablename The name of the base table. * @param integer $id1 The id number of the first record (from the history__id column) * @param integer $id2 The id of the second record (from the history__id column) * @param string $fieldname Optional name of a field to return. * @returns mixed Either the value of the specified field name if $fieldname is specified, * or a Dataface_Record object whose field values are formatted diffs. */ function getDiffs($tablename, $id1, $id2 = null, $fieldname = null) { import('Text/Diff.php'); import('Text/Diff/Renderer/inline.php'); $htablename = $tablename . '__history'; if (!Dataface_Table::tableExists($htablename)) { return PEAR::raiseError(df_translate('scripts.Dataface.HistoryTool.getDiffs.ERROR_HISTORY_TABLE_DOES_NOT_EXIST', "History table for '{$tablename}' does not exist, so we cannot obtain changes for records of that table.", array('tablename' => $tablename)), DATAFACE_E_ERROR); } $rec1 = df_get_record($htablename, array('history__id' => $id1)); if (!isset($id2)) { // The 2nd id wasn't provided so we assume we want to know the diffs // against the current state of the record. $table =& Dataface_Table::loadTable($tablename); $query = $rec1->strvals(array_keys($table->keys())); $io = new Dataface_IO($tablename); $io->lang = $rec1->val('history__language'); $rec2 = new Dataface_Record($tablename, array()); $io->read($query, $rec2); } else { $rec2 = df_get_record($htablename, array('history__id' => $id2)); } $vals1 = $rec1->strvals(); $vals2 = $rec2->strvals(); $vals_diff = array(); $renderer = new Text_Diff_Renderer_inline(); foreach ($vals2 as $key => $val) { $diff = new Text_Diff(explode("\n", @$vals1[$key]), explode("\n", $val)); $vals_diff[$key] = $renderer->render($diff); } $diff_rec = new Dataface_Record($htablename, $vals_diff); if (isset($fieldname)) { return $diff_rec->val($fieldname); } return $diff_rec; }
function &df_get_record($table, $query, $io = null) { import('Dataface/Record.php'); import('Dataface/IO.php'); $record = new Dataface_Record($table, array()); if (!isset($io)) { $io = new Dataface_IO($table); } $query['-limit'] = 1; if (@$query['-cursor'] > 0) { $query['-skip'] = $query['-cursor']; } $res = $io->read($query, $record); if (PEAR::isError($res)) { //print_r($query); //echo $res->toString(); $null = null; return $null; } return $record; }
function &getRecord() { if (Dataface_QuickForm::formSubmitted()) { $record = new Dataface_Record($_POST['-table'], array()); $io = new Dataface_IO($_POST['-table']); $query = $_POST['__keys__']; if (is_array($query)) { foreach (array_keys($query) as $postKey) { if ($query[$postKey][0] != '=') { $query[$postKey] = '=' . $query[$postKey]; } } } $io->read($query, $record); return $record; } else { $app =& Dataface_Application::getInstance(); $qt =& Dataface_QueryTool::loadResult($app->_currentTable); $curr =& $qt->loadCurrent(); return $curr; } }
/** * * 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; }
/** * 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 loadRecords() { $keyMissing = true; if (isset($this->_record)) { $keyMissing = false; foreach (array_keys($this->_table->keys()) as $key) { if (!$this->_record->val($key)) { // the current record is missing a primary key. // we need to reload the record. $keyMissing = true; break; } } } if ($keyMissing) { return PEAR::raiseError(df_translate('scripts.Dataface.TranslationForm.ERROR_NO_RECORD_FOUND', "No record was found to be translated."), E_USER_ERROR); } // Now we want to load all of the translations for the current record for use on this // translation form. $query = array(); foreach (array_keys($this->_table->keys()) as $key) { $query[$key] = '=' . $this->_record->strval($key); } $io = new Dataface_IO($this->_table->tablename); foreach ($this->translatableLanguages as $lang) { $io->lang = $lang; $record = new Dataface_Record($this->_table->tablename, array()); $io->read($query, $record); $this->records[$lang] =& $record; unset($record); } unset($this->_record); $this->_record =& $this->records[$this->destinationLanguage]; }
function test_import_data() { $t =& $this->_table1; /* * For our first test we will try to import data directly into a table. * We do not worry about relationships here. */ $data = '<?xml version="1.0"?> <dataface> <Profiles> <fname>John</fname> <lname>Smith</lname> <title>Professor</title> </Profiles> <Profiles> <fname>Julia</fname> <lname>Vaughn</lname> <title>Assistant</title> </Profiles> </dataface>'; $io = new Dataface_IO('Profiles', $this->db); $record = null; // First we try to import the data into a temporary import table. $importTablename = $io->importData($record, $data); $res = xf_db_query("SELECT * FROM `{$importTablename}`", $this->db); $rows = array(); while ($row = xf_db_fetch_array($res)) { $rows[] = $row; } $this->assertEquals(2, count($rows), "Incorrect number of rows in import table: '{$importTablename}'"); $this->assertEquals('John', $rows[0]['fname']); $this->assertEquals('Smith', $rows[0]['lname']); $this->assertEquals('Professor', $rows[0]['title']); // now we try to commit the records $records = $io->importData($record, $importTablename, null, null, true); $this->assertEquals(2, count($records)); $this->assertEquals('John Smith', $records[0]->val('fname') . ' ' . $records[0]->val('lname')); $this->assertEquals('Julia Vaughn', $records[1]->val('fname') . ' ' . $records[1]->val('lname')); $this->assertTrue($records[0]->val('id') > 0); $this->assertTrue($records[1]->val('id') > 0); //Now let's try to imort some records into a relationship /* * Now we attempt to import data into a one-to-many relationship */ $data = ' <dataface> <Appointments> <position>Trucker</position> <startdate>2003-11-12</startdate> <enddate>2004-05-06</enddate> <salary>1234.56</salary> </Appointments> <Appointments> <position>Director</position> <startdate>2002-01-02</startdate> <enddate>2005-02-03</enddate> <salary>5678.57</salary> </Appointments> </dataface>'; $record = new Dataface_Record('Profiles', array()); $io->read(array('id' => 10), $record); $importTablename = $io->importData($record, $data, 'xml', 'appointments'); $res = xf_db_query("SELECT * FROM `{$importTablename}`", $this->db); if (!$res) { trigger_error("Error selecting records from import table '{$importTablename}'. A mysql error occurred: " . xf_db_error($this->db) . "\n" . Dataface_Error::printStackTrace(), E_USER_ERROR); } $this->assertEquals(2, xf_db_num_rows($res)); $rows = array(); while ($row = xf_db_fetch_array($res)) { $rows[] = $row; } $this->assertEquals('Trucker', $rows[0]['position']); $this->assertEquals('Director', $rows[1]['position']); // now to commit this import $records = $io->importData($record, $importTablename, 'xml', 'appointments', true); if (PEAR::isError($records)) { trigger_error($records->toString() . Dataface_Error::printStackTrace(), E_USER_ERROR); } $this->assertEquals(2, count($records)); $this->assertEquals('dataface_relatedrecord', strtolower(get_class($records[0]))); $this->assertEquals(10, $records[0]->val('profileid')); $this->assertEquals('Trucker', $records[0]->val('Appointments.position')); //print_r($records[0]->getValues()); $res = xf_db_query("select * from `Appointments`", $this->db); $rows = array(); while ($row = xf_db_fetch_array($res)) { $rows[] = $row; } $this->assertEquals(10, $rows[3]['profileid']); $this->assertEquals('Trucker', $rows[3]['position']); $this->assertEquals('Director', $rows[4]['position']); /* * * Finally we try to import data into a many-to-many relationship. * */ $data = ' <dataface> <Courses> <dept>Math</dept> <coursenumber>332</coursenumber> </Courses> <Courses> <dept>CMPT</dept> <coursenumber>475</coursenumber> </Courses> </dataface>'; $importTablename = $io->importData($record, $data, 'xml', 'courses'); if (PEAR::isError($importTablename)) { trigger_error($importTablename->toString() . Dataface_Error::printStackTrace(), E_USER_ERROR); } $res = xf_db_query("SELECT * FROM `{$importTablename}`", $this->db); if (!$res) { trigger_error("Error selecting records from import table '{$importTablename}'. A mysql error occurred: " . xf_db_error($this->db) . "\n" . Dataface_Error::printStackTrace(), E_USER_ERROR); } $this->assertEquals(2, xf_db_num_rows($res)); $rows = array(); while ($row = xf_db_fetch_array($res)) { $rows[] = $row; } $this->assertEquals('Math', $rows[0]['dept']); $this->assertEquals('CMPT', $rows[1]['dept']); $records = $io->importData($record, $importTablename, 'xml', 'courses', true); if (PEAR::isError($records)) { trigger_error($records->toString() . Dataface_Error::printStackTrace(), E_USER_ERROR); } $this->assertEquals(2, count($records)); foreach ($records as $rec) { $this->assertEquals('dataface_relatedrecord', strtolower(get_class($rec))); } //echo "Records: $records"; $this->assertEquals('Math', $records[0]->val('dept')); $this->assertEquals('CMPT', $records[1]->val('dept')); $res = xf_db_query("SELECT * FROM Courses c inner join Student_Courses sc on c.id=sc.courseid inner join Profiles p on p.id=sc.studentid where p.id='10'", $this->db); if (!$res) { trigger_error(xf_db_error($this->db) . Dataface_Error::printStackTrace(), E_USER_ERROR); } $this->assertEquals(2, xf_db_num_rows($res)); $course1 = xf_db_fetch_array($res); $course2 = xf_db_fetch_array($res); $this->assertEquals(10, $course1['studentid']); $this->assertTrue($course1['courseid'] > 0); $this->assertEquals(10, $course2['studentid']); $this->assertTrue($course2['courseid'] > 0); $this->assertEquals('Math', $course1['dept']); $this->assertEquals('CMPT', $course2['dept']); $this->assertEquals('John Smith', $course1['fname'] . ' ' . $course1['lname']); }
/** * Removes the given related record from its relationship. * * @param Dataface_RelatedRecord &$related_record The related record to be removed. * @param boolean $delete If true then the record will also be deleted from * the database. * @since 0.6.1 */ function removeRelatedRecord(&$related_record, $delete = false, $secure = false) { if ($secure && !$related_record->_record->checkPermission('remove related record', array('relationship' => $related_record->_relationshipName))) { // Use security to check to see if we are allowed to delete this // record. //echo $related_record->_record->_table->getDelegate()->getRoles(array('relationship'=>$related_record->_relationshipName));exit; return Dataface_Error::permissionDenied(df_translate('scripts.Dataface.IO.removeRelatedRecord.PERMISSION_DENIED', 'Could not remove record "' . $related_record->getTitle() . '" from relationship "' . $related_record->_relationshipName . '" of record "' . $related_record->_record->getTitle() . '" because you have insufficient permissions.', array('title' => $related_record->getTitle(), 'relationship' => $related_record->_relationshipName, 'parent' => $related_record->_record->getTitle()))); } $res = $this->fireEvent('beforeRemoveRelatedRecord', $related_record); if (PEAR::isError($res)) { return $res; } /* * First we need to find out which table is the domain table. The domain table * is the table that actually contains the records of interest. The rest of * the tables are referred to as 'join' tables. */ $domainTable = $related_record->_relationship->getDomainTable(); if (PEAR::isError($domainTable)) { /* * Dataface_Relationship::getDomainTable() throws an error if there are * no join tables. We account for that by explicitly setting the domain * table to the first table in the list. */ $domainTable = $related_record->_relationship->_schema['selected_tables'][0]; } /* * Next we construct an IO object to write to the domain table. */ $domainIO = new Dataface_IO($domainTable); $domainTable =& Dataface_Table::loadTable($domainTable); // reference to the Domain table Dataface_Table object. /* * Begin building queries. */ $query = array(); // query array to build the query to delete the record. $absVals = array(); // same as query array except the keys are absolute field names (ie: Tablename.Fieldname) $currKeyNames = array_keys($domainTable->keys()); // Names of key fields in the domain table foreach ($currKeyNames as $keyName) { $query[$keyName] = $related_record->val($keyName); $absVals[$domainTable->tablename . '.' . $keyName] = $query[$keyName]; } $fkeys = $related_record->_relationship->getForeignKeyValues($absVals, null, $related_record->_record); $warnings = array(); $confirmations = array(); foreach (array_keys($fkeys) as $currTable) { // For each table in the relationship we go through and delete its record. $io = new Dataface_IO($currTable); $record = new Dataface_Record($currTable, array()); $res = $io->read($fkeys[$currTable], $record); //patch for Innodb foreign keys with ON DELELE CASCADE // Contributed by Optik if (!$io->recordExists($record, null, $currTable)) { $warnings[] = df_translate('scripts.Dataface.IO.removeRelatedRecord.ERROR_RECORD_DOESNT_EXIST', "Failed to delete entry for record '" . $record->getTitle() . "' in table '{$currTable}' because record doesn't exist.", array('title' => $record->getTitle(), 'currTable' => $currTable)); unset($record); unset($io); continue; } // -- end patch for Innodb foreign keys if ($currTable == $domainTable->tablename and !$delete) { // Unless we have specified that we want the domain table record // deleted, we leave it alone! unset($record); unset($io); continue; } // Let's figure out whether we need to use security for deleting this // record. // If security is on, and it is the domain table, and the user doesn't // have the 'delete related record' permission then we need to use // security if ($currTable == $domainTable->tablename and $secure and !$related_record->_record->checkPermission('delete related record', array('relationship' => $related_record->_relationshipName))) { $useSecurity = true; } else { $useSecurity = false; } $res = $io->delete($record, $useSecurity); if (PEAR::isError($res) && Dataface_Error::isError($res)) { //$this->logError($res); return $res; } else { if (PEAR::isError($res)) { $warnings[] = $res; } else { $confirmations[] = df_translate('Successfully deleted record', "Successfully deleted entry for record '" . $record->getTitle() . "' in table '{$currTable}'", array('title' => $record->getTitle(), 'table' => $currTable)); } } $record->__destruct(); unset($record); unset($b); unset($io); } $res = $this->fireEvent('afterRemoveRelatedRecord', $related_record); if (PEAR::isError($res)) { return $res; } if (count($warnings) > 0) { return PEAR::raiseError(@implode("\n", $warnings), DATAFACE_E_WARNING); } if (count($confirmations) == 0) { return false; } return true; }
function processForm($values) { ini_set('max_execution_time', 900); import('Dataface/IO.php'); import('Dataface/TranslationTool.php'); $tt = new Dataface_TranslationTool(); $app =& Dataface_Application::getInstance(); $query =& $app->getQuery(); if (strlen($values['-sourceLanguage']) != 2 || strlen($values['-destinationLanguage']) != 2) { trigger_error('Invalid input for languages. Expected a 2 digit language code.', E_USER_ERROR); } $values['-limit'] = 500; //$qt = new Dataface_QueryTool($this->table->tablename, $app->db(), $values); //$qt->loadSet(); //$it =& $qt->iterator(); $q = $query; $q['-limit'] = 9999; if (@$q['--limit']) { $q['-limit'] = $q['--limit']; } $it =& df_get_records($this->table->tablename, $q); $keycols = array_keys($this->table->keys()); $cols = $this->table->getTranslation($values['-destinationLanguage']); if (!is_array($cols)) { trigger_error('Could not find any columns to be translated in table ' . $values['-destinationLanguage'] . Dataface_Error::printStackTrace(), E_USER_ERROR); } $babelfish = $this->getTranslator(); //new babelfish(); if (isset($app->_conf['google_translate_url'])) { $babelfish->google_url_webpage = $app->_conf['google_translate_url']; } $ioSrc = new Dataface_IO($this->table->tablename); $ioSrc->lang = $values['-sourceLanguage']; $languageCodes = new I18Nv2_Language('en'); $ioDest = new Dataface_IO($this->table->tablename); $ioDest->lang = $values['-destinationLanguage']; $count = 0; $to_be_translated = array(); $destObjects = array(); while ($it->hasNext()) { $curr =& $it->next(); $translationInfo =& $tt->getTranslationRecord($curr, $values['-destinationLanguage']); if ($translationInfo and $translationInfo->val('translation_status') == TRANSLATION_STATUS_NEEDS_UPDATE_MACHINE) { $t_needsUpdate = true; } else { $t_needsUpdate = false; } $translation_text = array(); $keyvals = $curr->vals($keycols); $srcObject = new Dataface_Record($this->table->tablename, array()); $destObject = new Dataface_Record($this->table->tablename, array()); $ioSrc->read($keyvals, $srcObject); $ioDest->read($keyvals, $destObject); $keyvalsQuery = $keyvals; foreach ($keyvals as $key => $val) { $keyvalsQuery[$key] = '=' . $keyvals[$key]; } $qb = new Dataface_QueryBuilder($this->table->tablename, $keyvalsQuery); $sql = "select * from `" . $this->table->tablename . "_" . $values['-destinationLanguage'] . "` " . $qb->_where(); $res = mysql_query($sql, $app->db()); if (!$res) { trigger_error(mysql_error($app->db()) . 'SQL : ' . $sql . " Stacktrace:" . Dataface_Error::printStackTrace(), E_USER_ERROR); } $queryResult = mysql_fetch_array($res); if (!$queryResult) { $queryResult = array(); } foreach ($cols as $col) { if (in_array($col, $keycols)) { continue; } if (!$this->table->isText($col) and !$this->table->isChar($col)) { continue; } if (!isset($queryResult[$col]) || $t_needsUpdate) { //$updateRequired = true; } else { continue; } $translation_text[$col] = $srcObject->getValue($col); } if (count($translation_text) > 0) { $to_be_translated[] =& $translation_text; $destObjects[] =& $destObject; } unset($curr); unset($srcObject); unset($destObject); unset($qb); unset($translatedRecord); unset($translation_text); unset($translationInfo); } $translated = $this->translate($to_be_translated, $values['-sourceLanguage'], $values['-destinationLanguage'], $babelfish); if (PEAR::isError($translated)) { return $translated; } foreach ($translated as $rowid => $row) { if ($translated[$rowid] == $to_be_translated[$rowid]) { continue; } $update = false; foreach ($row as $col => $val) { if (strlen(trim($val)) === 0) { continue; } $destObjects[$rowid]->setValue($col, $val); $update = true; } if ($update) { $res = $ioDest->write($destObjects[$rowid]); if (PEAR::isError($res)) { trigger_error($res->toString() . Dataface_Error::printStackTrace(), E_USER_ERROR); } $tt->setTranslationStatus($destObjects[$rowid], $ioDest->lang, TRANSLATION_STATUS_MACHINE); } } }
function test_destination_tables() { $record = new Dataface_Record('Profiles', array()); $io = new Dataface_IO('Profiles'); $io->read(array('id' => 10), $record); $relationship =& $record->_table->getRelationship('appointments'); $destinationTables =& $relationship->getDestinationTables(); $this->assertEquals(1, count($destinationTables)); $this->assertEquals('Appointments', $destinationTables[0]->tablename); //$this->assertEquals(array('Appointments'), $destinationTables); require_once 'dataface-public-api.php'; $registration =& df_get_record('Registrations', array('RegistrationID' => 1)); $products =& $registration->getRelationshipIterator('Products'); $product =& $products->next(); $destTables =& $product->_relationship->getDestinationTables(); $this->assertEquals(2, count($destTables)); }