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 handle($params) { if (!defined('DISABLE_reCAPTCHA')) { define('DISABLE_reCAPTCHA', 1); } import('Dataface/QuickForm.php'); Dataface_QuickForm::$TRACK_SUBMIT = false; $app = Dataface_Application::getInstance(); $query = $app->getQuery(); $errors = null; try { if (!@$_POST['-table']) { throw new Exception("No table specified"); } $table = $_POST['-table']; $rec = new Dataface_Record($table, array()); $tableObj = $rec->_table; $fields = array(); if (!$rec->checkPermission('new')) { throw new Exception("Failed to insert record. Permission denied"); } foreach ($_POST as $k => $v) { if ($k[0] == '-') { continue; } $fields[] = $k; $rec->setValue($k, $v); if (!$rec->checkPermission('new', array('field' => $k))) { throw new Exception(sprintf("Failed to insert record because you do not have permission to insert data into the %s column", $k)); } } $form = df_create_new_record_form($table, $fields); $form->_flagSubmitted = true; $res = $form->validate(); if (!$res) { $errors = $form->_errors; throw new Exception('Validation error', REST_INSERT_VALIDATION_ERROR); } $res = $rec->save(null, true); if (PEAR::isError($res)) { throw new Exception("Failed to insert record due to a server error: " . $res->getMessage(), 500); } $out = array(); $vals = $rec->strvals(); foreach ($vals as $k => $v) { if ($rec->checkPermission('view')) { $out[$k] = $v; } } $this->out(array('code' => 200, 'message' => 'Record successfully inserted', 'record' => $out)); exit; } catch (Exception $ex) { $this->out(array('code' => $ex->getCode(), 'message' => $ex->getMessage(), 'errors' => $errors)); exit; } }
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 test_log_record() { $profile =& df_get_record('Profiles', array('id' => 10)); $ht = new Dataface_HistoryTool(); $ht->logRecord($profile); $history =& df_get_record($ht->logTableName('Profiles'), array('id' => 10)); $profile2 = new Dataface_Record('Profiles', $history->vals()); $this->assertEquals($profile->strvals(), $profile2->strvals()); //print_r($history->strvals()); }