function df_save_record(&$record, $keys = null, $lang = null, $secure = false) { import('Dataface/Record.php'); import('Dataface/IO.php'); $io = new Dataface_IO($record->_table->tablename); if (isset($lang)) { $io->lang = $lang; } $res = $io->write($record, $keys, null, $secure); $io->__destruct(); unset($io); return $res; }
function writeConfigToDB() { import('Dataface/Table.php'); import('Dataface/Record.php'); import('Dataface/IO.php'); if (!is_a($this, 'Dataface_ConfigTool')) { throw new Exception('ConfigWriter methods are only to be used via the Dataface_ConfigTool class.', E_USER_ERROR); } $this->loadAllConfig(); $app =& Dataface_Application::getInstance(); // first let's make copies of the current configuration. $timestamp = time(); foreach ($this->configTypes as $type) { $res = xf_db_query("CREATE TABLE `__" . addslashes($type) . "__" . $timestamp . "` SELECT * FROM `__" . addslashes($type) . "__`", $app->db()); if (!$res) { throw new Exception("Failed to make backup of table '__" . $type . "__'." . xf_db_error($app->db()), E_USER_ERROR); } } $res = xf_db_query("CREATE TABLE `__properties__" . $timestamp . "` SELECT * FROM `__properties__`", $app->db()); if (!$res) { throw new Exception("Failed to make backup of table '__properties__'.", $app->db()); } // Now that we have made our backups, we can continue to write the configuration to the database. //print_r($this->config); foreach ($this->configTypes as $type) { $res = xf_db_query("DELETE FROM `__" . addslashes($type) . "__`", $app->db()); if (!$res) { throw new Exception("Failed to delete all records from table '__" . $type . "__'", $app->db()); } foreach ($this->config[$type] as $tablename => $tableConfig) { foreach ($tableConfig as $sectionname => $section) { $tableObj =& Dataface_Table::loadTable('__' . $type . '__'); $record = new Dataface_Record('__' . $type . '__', array()); $record->useMetaData = false; // some of the field names begin with '__' which would conflict with dataface's handling of MetaData fields. foreach (array_keys($tableObj->fields()) as $fieldname) { $record->setValue($fieldname, @$section[$fieldname]); unset($section[$fieldname]); } $record->setValue('name', $sectionname); $record->setValue('table', $tablename); //echo nl2br("Section name: $sectionname\nTable: $tablename\n"); //print_r($record->strvals()); echo nl2br("\nWriting section: {$sectionname} : "); print_r($record->strvals()); // now that we have created the record, we write the record $io = new Dataface_IO('__' . $type . '__'); $res = $io->write($record); if (PEAR::isError($res)) { throw new Exception($res->toString(), E_USER_ERROR); } else { if (!$res) { throw new Exception("Failure to write to database for unknown reason.", E_USER_ERROR); } } // now for the rest of the properties. foreach ($section as $propertyName => $propertyValue) { $res = xf_db_query("\n\t\t\t\t\t\t\tINSERT INTO \n\t\t\t\t\t\t\t `__properties__` \n\t\t\t\t\t\t\t (`parent_id`,`parent_type`,`property_name`,`property_value`)\n\t\t\t\t\t\t\tVALUES\n\t\t\t\t\t\t\t ('" . $record->val($type . '_id') . "', \n\t\t\t\t\t\t\t '" . addslashes($type) . "',\n\t\t\t\t\t\t\t '" . addslashes($propertyName) . "',\n\t\t\t\t\t\t\t '" . addslashes($propertyValue) . "')", $app->db()); if (!$res) { throw new Exception("Failed to add property '{$propertyName}' to table '__properties__' with value '{$propertyValue}'" . xf_db_error($app->db()), E_USER_ERROR); } } unset($tableObj); unset($record); unset($io); } } } }
function test_add_existing_related_record() { $io = new Dataface_IO('Profiles', $this->db); $record = new Dataface_Record('Profiles', array()); $io->read(array('id' => 10), $record); $cio = new Dataface_IO('Courses'); $course = new Dataface_Record('Courses', array('dept', 'PSYC', 'coursenumber' => 400, 'id' => 1001)); $cio->write($course); unset($course); $course = new Dataface_Record('Courses', array()); $cio->read(array('id' => '1001'), $course); $this->assertTrue('PSYC', $course->val('dept')); $relatedCourse = new Dataface_RelatedRecord($record, 'courses', array('id' => 1001)); ob_start(); $res = $io->addExistingRelatedRecord($relatedCourse); $buffer = ob_get_contents(); ob_end_clean(); $this->assertEquals(' beforeAddRelatedRecord beforeAddExistingRelatedRecord afterAddExistingRelatedRecord afterAddRelatedRecord', $buffer); $this->assertTrue($res, "Failed to add existing course"); $this->assertTrue(!PEAR::isError($res), "Error occurred while adding existing course"); // Now check to make sure that the record is in the list of related records 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('id') == 1001) { $found = true; } unset($nex); } $this->assertTrue($found, "Added course did not show up in list of related records."); }
function save($values) { // First let's find out if we should SAVE the data or if we should just be // storing it in the session or if we are saving the data to the database if (!$this->_new) { // Make sure that the correct form is being submitted. if (!isset($values['__keys__'])) { throw new Exception(df_translate('scripts.Dataface.QuickForm.save.ERROR_SAVING_RECORD', "Error saving record in QuickForm::save().\n<br>"), E_USER_ERROR); } if (array_keys($values['__keys__']) != array_keys($this->_table->keys())) { throw new Exception(df_translate('scripts.Dataface.QuickForm.save.ERROR_SAVING_RECORD', "Error saving record in QuickForm::save().\n<br>"), E_USER_ERROR); } } if ($this->_new) { $this->_record->clearValues(); } $res = $this->push(); if (!$this->_new) { if ($this->_record->snapshotExists()) { $tempRecord = new Dataface_Record($this->_record->_table->tablename, $this->_record->getSnapshot()); } else { $tempRecord =& $this->_record; } if ($values['__keys__'] != $tempRecord->strvals(array_keys($this->_record->_table->keys()))) { throw new Exception(df_translate('scripts.Dataface.QuickForm.save.ERROR_SAVING_RECORD', "Error saving record in QuickForm::save().\n<br>"), E_USER_ERROR); } } if (PEAR::isError($res)) { $res->addUserInfo(df_translate('scripts.Dataface.QuickForm.save.ERROR_PUSHING_DATA', "Error pushing data from form onto table in QuickForm::save() ", array('line' => 0, 'file' => "_"))); return $res; } // Let's take an inventory of which fields were changed.. because // we are going to make their values available in the htmlValues() // method which is used by the ajax form to gather updates. foreach ($this->_fields as $changedfield) { if ($this->_record->valueChanged($changedfield['name'])) { $this->_changed_fields[] = $changedfield['name']; } } $io = new Dataface_IO($this->tablename, $this->db); $io->lang = $this->_lang; if ($this->_new) { $keys = null; } else { $keys = $values['__keys__']; } $res = $io->write($this->_record, $keys, null, true, $this->_new); if (PEAR::isError($res)) { if (Dataface_Error::isDuplicateEntry($res)) { /* * If this is a duplicate entry (or just a notice - not fatal), we will propogate the exception up to let the application * decide what to do with it. */ return $res; } if (Dataface_Error::isNotice($res)) { return $res; } $res->addUserInfo(df_translate('scripts.Dataface.QuickForm.save.ERROR_SAVING_RECORD', "Error saving form in QuickForm::save()", array('line' => 0, 'file' => "_"))); throw new Exception($res->toString(), E_USER_ERROR); } if (isset($io->insertIds[$this->tablename]) and $this->_table->getAutoIncrementField()) { $this->_record->setValue($this->_table->getAutoIncrementField(), $io->insertIds[$this->tablename]); $this->_record->setSnapshot(); } 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); } } }