/** * aql::insert/update on the save array depending on what needs to be done * recursive because of sub objects / queries * @param array $save_array array to save * @param array $ids ids to pass through * @return array updated save array */ private final function saveArray($save_array, $ids = array()) { $is_dev = $this->isDev(); // copy out objects $objects = $save_array['__objects__']; unset($save_array['__objects__']); // helper function $addRecordInfo = function ($table_block, $is_update = false) { $fields = array('update' => array('mod__person_id', 'update__person_id'), 'insert' => array('insert__person_id')); $key = $is_update ? 'update' : 'insert'; $time_field = $key . '_time'; if (!$table_block['fields'][$time_field]) { $table_block['fields'][$time_field] = aql::now(); } if (!defined('PERSON_ID') || !is_numeric(PERSON_ID)) { return $table_block; } $id = PERSON_ID; $fields = $fields[$key]; foreach ($fields as $field) { $table_block['fields'][$field] = $table_block['fields'][$field] ?: $id; } return $table_block; }; foreach ($save_array as $table => $info) { // make sure this is an array $info['fields'] = arrayify($info['fields']); foreach ($ids as $n => $v) { if (is_array($this->_ignore['fields']) && in_array($n, $this->_ignore['fields'])) { // since ids are added to each table in case of foreign keys // abort if they are being ignored in this model continue; } // add the id to the table block iff there are already fields if ($info['fields'] && !$info['fields'][$n]) { $save_array[$table]['fields'][$n] = $v; $info['fields'][$n] = $v; } } if ($info['fields']) { $is_update = $info['id'] && is_numeric($info['id']); $info = $addRecordInfo($info, $is_update); if ($is_update) { aql::update($table, $info['fields'], $info['id'], true); } else { $rs = aql::insert($table, $info['fields'], true); $save_array[$table]['id'] = $info['id'] = $rs[0][$table . '_id']; } } $ids[$table . '_id'] = $info['id']; if (is_array($info['subs'])) { foreach ($info['subs'] as $i => $sub) { $save_array[$table]['subs'][$i] = $this->saveArray($sub, $ids); } } } if (is_array($objects)) { foreach ($objects as $o) { if (!$o['data']) { continue; } $tmp = Model::get($o['object']); $tmp->_data = $o['data']; $tmp->loadIDs($ids); $pt = $tmp->getPrimaryTable(); $pt_id = $pt . '_id'; if (!$tmp->{$pt_id} && $this->{$pt_id}) { $tmp->{$pt_id} = $this->{$pt_id}; } $tmp->save(true); if ($tmp->_errors) { $this->addErrors($tmp->_errors); $this->failTransaction(); } } } $save_array['objects'] = $objects; return $save_array; }
/** * Get or create a token for this person_id for this app * @return array */ public function getOAuthToken() { $person_id = $this->person_id(); if (!$person_id) { static::error('User not specified.'); } if (!\person::exists($person_id)) { static::error('Invalid user.'); } $clause = array('person_id' => $person_id); if (!$this->app_key()) { $app_id = null; } else { $api_app = static::getApiAppByKey($this->app_key()); if (!$api_app) { static::error('Invalid app_key.'); } $app_id = $api_app->getID(); } $clause[static::getApiAppModelName() . '_id'] = $app_id; if (!$clause) { static::error('Unknown Identity.'); } $m = static::getOauthModelName(); $oauth = $m::getOne($clause); if (!$oauth || !$oauth->token) { $oauth = $m::insert($clause); } return array('oauth_token' => $oauth->token, 'issued' => $oauth->getTimeIssued(), 'now' => strtotime(\aql::now()), 'expires' => $oauth->getTimeExpires()); }