function handle($params) { $app = Dataface_Application::getInstance(); $query = $app->getQuery(); try { if (!@$_POST['-table']) { throw new Exception("No table was specified"); } $vals = array(); foreach ($query as $k => $v) { if ($k and $k[0] != '-') { $vals[$k] = $v; } } $record = new Dataface_Record($_POST['-table'], array()); $record->setValues($vals); if (!$record->checkPermission('ajax_save')) { throw new Exception("Permission Denied", 502); } $res = $record->save(null, true); if (PEAR::isError($res)) { error_log($res->getMessage(), $res->getCode()); throw new Exception("Failed to save record due to a server error. See log for details."); } $this->out(array('code' => 200, 'message' => 'Successfully inserted record.', 'recordId' => $record->getId())); } catch (Exception $ex) { $this->out(array('code' => $ex->getCode(), 'message' => $ex->getMessage())); } }
/** * @brief Wrapper around getContextMask() to get the permissions * for a record through the context of a portal. * @returns array($perm:string => $val:boolean) * @since 2.0 */ function getPortalFieldPermissions(Dataface_Record $record, $params = array()) { return $this->getContextMask($record->getId(), @$params['field']); }
static function touchRecord(Dataface_Record $record = null) { if (!isset($record)) { return; } $id = $record->getId(); $hash = md5($id); $sql = "replace into dataface__record_mtimes \n\t\t\t\t(recordhash, recordid, mtime) values \n\t\t\t\t('" . addslashes($hash) . "','" . addslashes($id) . "','" . time() . "')"; try { $res = df_q($sql); } catch (Exception $ex) { self::createRecordMtimes(); } }
function handle($params) { $app = Dataface_Application::getInstance(); if (!$_POST) { throw new Exception("Commit requires post"); } $data = @$_POST['--data']; if (!$data) { throw new Exception("No data provided"); } $data = json_decode($data, true); $updates = array(); $inserts = array(); $deletes = array(); if (isset($data['inserts'])) { $inserts =& $data['inserts']; } if (isset($data['updates'])) { $updates =& $data['updates']; } if (isset($data['deletes'])) { $deletes =& $data['deletes']; } $numFailures = 0; $numSuccesses = 0; $deleteResponses = array(); // Let's do the deletes first foreach ($deletes as $deleteInfo) { $response = array(); $deleteResponses[] =& $response; $record = df_get_record_by_id($deleteInfo['id']); if (!$record) { $response['message'] = 'Record ' . $deleteInfo['id'] . ' could not be found.'; $response['code'] = 404; $numFailures++; } else { $res = $response->delete(true); if (PEAR::isError($res)) { $response['message'] = $res->getMessage(); $response['code'] = $res->getCode(); $numFailures++; } else { $response['message'] = 'Deleted record ' . $deleteInfo['id'] . '.'; $response['code'] = 200; $response['recordId'] = $deleteInfo['id']; $numSuccesses++; } } } $insertResponses = array(); foreach ($inserts as $insertInfo) { $response = array(); $insertResponses[] =& $response; $record = new Dataface_Record($insertInfo['table'], array()); $record->setValues($insertInfo['data']); $res = $record->save(null, true); if (PEAR::isError($res)) { $response['message'] = $res->getMessage(); $response['code'] = $res->getCode(); $numFailures++; } else { $response['message'] = 'Inserted record'; $response['code'] = $res->getCode(); $response['recordId'] = $record->getId(); $response['version'] = $record->getVersion(); $numSuccesses++; } } $updateResponses = array(); foreach ($updates as $updateInfo) { $response = array(); $insertResponses[] =& $response; $record = df_get_record_by_id($updateInfo['id']); if (!$record) { $response['message'] = 'Record ' . $updateInfo['id'] . ' could not be found.'; $response['code'] = 404; $numFailures++; } else { $record->setValues($updateInfo['data']); $res = $record->save(null, true); if (PEAR::isError($res)) { $response['message'] = $res->getMessage(); $response['code'] = $res->getCode(); $numFailures++; } else { $response['message'] = 'Updated record'; $response['code'] = 200; $response['recordId'] = $record->getId(); $response['version'] = $record->getVersion(); $numSuccesses++; } } } header('Content-type: text/json; charset="' . $app->_conf['oe'] . '"'); $out = array('code' => ($numFailures == 0 and $numSuccesses > 0) ? 200 : $numSuccesses > 0 ? 201 : 202, 'message' => $numSuccesses . ' successes. ' . $numFailures . ' failures.', 'numSuccesses' => $numSuccesses, 'numFailures' => $numFailures, 'responses' => array('updates' => $updateResponses, 'inserts' => $insertResponses, 'deletes' => $deleteResponses)); echo json_encode($out); }
public function buildRecord(xatacard_layout_Schema $schema, Dataface_Record $rec) { //Now we populate the record $out = new xatacard_layout_Record(); $out->setSchema($schema); $out->setDataSource($this); $recordCache = array(); $recordCache[$rec->getId()] = $rec; $recordData = array('records' => array(), 'fields' => array()); $recordIdToIndex = array(); foreach ($schema->getFields() as $key => $value) { if (strpos($key, '/') !== false) { // this is a first class property of the record so we can just get it. $index = count($recordData['records']); $recordData['records'][$index] = array('id' => $rec->getId(), 'version' => $rec->getVersion()); $recordIdToIndex[$rec->getId()] = $index; $recordData['fields'][$key] = $recordIdToIndex[$rec->getId()]; $out->setValue($key, $rec->getValue($key)); } else { // this is a related record's property that we need to get from the related // records. $path = explode('/', $key); $crec = $rec; // As we're going to be going through related records // we need to mark the current record we are dealing /// with $fieldName = array_pop($path); // A flag to indicate if we should break the outer loop // after the inner loop finishes $shouldSkip = false; foreach ($path as $part) { $index = 0; if (preg_match('/^(.*)\\[(\\d+\\)]$/', $part, $matches)) { $index = intval($matches[2]); $part = $matches[1]; } $related = $crec->getRelatedRecord($part, $index); if (PEAR::isError($related)) { throw new Exception(sprintf("MySQL datasource failed to load record because there was an error retrieving a column from a related record. The field '%s' could not be retrieved because the following error: %s", $key, $related->getMessage())); } if (!$related) { // No related record could be found to satisfy this path. // This doesn't constitute an error. It just means that the // value should be blank. $out->setValue($key, null); $shouldSkip = true; break; } unset($crec); if (@$this->recordCache[$related->getId()]) { $crec = $this->recordCache[$related->getId()]; } else { $crec = $related->toRecord(); $this->recordCache[$related->getId()] = $crec; $index = count($recordData['records']); $recordData['records'][$index] = array('id' => $crec->getId(), 'version' => $rec->getVersion()); $recordIdToIndex[$crec->getId()] = $index; } unset($related); } if ($shouldSkip) { // Something occurred in the inner loop to // complete this step so we should skip the rest // of this stuff. continue; } if (!$crec) { // This should never happen throw new Exception(sprintf("MySQL datasource failed to load record because there was an error retrieving a related record required for one of the fields. The field '%s' could not be retrieved.", $key)); } $recordData['fields'][$key] = $recordIdToIndex[$crec->getId()]; $out->setValue($key, $crec->val($fieldName)); } } $cacheKeys = array_keys($recordCache); sort($cacheKeys); $versionstring = array(); foreach ($cacheKeys as $k) { $versionstring[] = $k . ':' . $recordCache[$k]->getVersion(); } $versionstring = implode(' ', $versionstring); $versionhash = md5($versionstring); $res = $this->query(sprintf("select `id` from `%s` where \n\t\t\t\tschema_id=%d and \n\t\t\t\tbase_record_id_hash='%s' and \n\t\t\t\tversion_hash='%s' and\n\t\t\t\t`lang`='%s'", str_replace('`', '', self::$RECORDS_TABLE), intval($schema->getId()), addslashes(md5($rec->getId())), addslashes($versionhash), addslashes($rec->lang))); if (xf_db_num_rows($res) >= 0) { $row = xf_db_fetch_row($res); $out->setId($row[0]); } else { $res = $this->query(sprintf("insert into `%s` (schema_id, base_record_id_hash, base_record_id, version_hash, `lang`, `record_data`)\n\t\t\t\tvalues (\n\t\t\t\t\t%d, '%s', '%s', '%s', '%s'\n\t\t\t\t)", intval($schema->getId()), addslashes(md5($rec->getId())), addslashes($rec->getid()), addslashes($versionhash), addslashes($rec->lang), addslashes(json_encode($recordData)))); $out->setId(xf_db_insert_id(df_db())); } $out->clearSnapshot(); return $out; }