/** * Handle the exception. Renders the error view using the class' functions. * @param \Exception $exception */ public function handle(\Exception $exception) { \Application::log()->error($exception->getMessage(), $exception->getTrace()); $this->sendHeader($exception); $errorRenderer = new Renderer\ErrorRenderer('Error'); echo $errorRenderer->render($this->getErrorView($exception), ['exception' => $exception]); }
public static function getMulti($params, $mode = SQLDatabaseModel::MODE_ASSOC) { //Load all models $fields = array(); $field_infos = array(); $models = array(); $fieldDescriptions = array(); $headers = array(); $fixedConditions = array(); foreach ($params["fields"] as $field) { $fieldreferences = explode(", ", $field); if (count($fieldreferences) == 1) { $fields[] = (string) $field; $field_infos[] = Model::resolvePath((string) $field); } else { $fields[] = $fieldreferences; foreach ($fieldreferences as $ref) { $infos[] = Model::resolvePath((string) $ref); } $field_infos[] = $infos; } } foreach ($fields as $i => $field) { if (is_array($field)) { foreach ($field_infos[$i] as $info) { if (array_search($info["model"], array_keys($models)) === false) { $models[$info["model"]] = Model::load($info["model"]); } } } else { if (array_search($field_infos[$i]["model"], array_keys($models)) === false) { $models[$field_infos[$i]["model"]] = Model::load($field_infos[$i]["model"]); if ($models[$field_infos[$i]["model"]]->fixedConditions != "") { $fixedConditions[] = $models[$field_infos[$i]["model"]]->fixedConditions; } } } } // Replace any conditions with filters if (isset($params['filter'])) { $params['conditions'] = $params['filter']; } else { $params['conditions'] = ''; } if (count($fixedConditions) > 0) { $params["conditions"] = ($params["conditions"] == "" ? "" : "(" . $params["conditions"] . ") AND ") . "(" . implode(" AND ", $fixedConditions) . ")"; } //Buld the query $query = "SELECT "; $fieldList = array(); $rawFields = array(); $functions = $params["global_functions"]; foreach ($fields as $i => $field) { $field_info = $field_infos[$i]; if (is_array($field)) { $concatFieldList = array(); foreach ($field_info as $info) { $fieldData = $models[$info["model"]]->getFields(array($info["field"])); $concatFieldList[] = $models[$info["model"]]->datastore->formatField($fieldData[0], $models[$info["model"]]->getDatabase() . "." . $info["field"], true, null, true); $rawFields[] = $models[$info["model"]]->getDatabase() . "." . $info["field"]; } $fieldList[] = $models[$info["model"]]->datastore->applySqlFunctions($models[$info["model"]]->datastore->concatenate($concatFieldList), $functions); } else { $fieldData = $models[$field_infos[$i]["model"]]->getFields(array($field_info["field"])); $fieldDescriptions[] = $fieldData[0]; $headers[] = $fieldData[0]["label"]; $fieldList[] = $models[$field_info["model"]]->datastore->formatField($fieldData[0], $models[$field_info["model"]]->getDatabase() . "." . $field_info["field"], true, $functions, true); $rawFields[] = $models[$field_info["model"]]->getDatabase() . "." . $field_info["field"]; } } if ($params['moreInfoOnly'] === true) { return array("data" => $data, "fieldInfos" => $fieldDescriptions, "headers" => $headers, "rawFields" => $rawFields); } $tableList = array(); foreach ($models as $model) { $tableList[] = $model->getDatabase(); } $joinConditions = array(); $hasDontJoin = is_array($params['dont_join']); foreach ($models as $model) { foreach ($models as $other_model) { // skip if the models are the same if ($model->name == $other_model->name) { continue; } if ($model->hasField($other_model->getKeyField())) { if ($hasDontJoin) { $modelPair = "{$model->package},{$other_model->package}"; $fieldPair = "{$model->package}.{$other_model->getKeyField()},{$other_model->package}.{$other_model->getKeyField()}"; Application::log('GET_MULTI')->debug("Trying to skip a join", ['model_pair' => $modelPair, 'field_pair' => $fieldPair, 'dont_join' => $params['dont_join']]); if (array_search($modelPair, $params['dont_join']) !== false) { continue; } if (array_search($fieldPair, $params['dont_join']) !== false) { continue; } } $joinConditions[] = "{$model->getDatabase()}.{$other_model->getKeyField()}={$other_model->getDatabase()}.{$other_model->getKeyField()}"; } } } if ($params["distinct"] === true) { $query .= " DISTINCT "; } // Apply any special modifiers to the fields if ($params["count"] === true || $params["enumerate"] === true) { $query .= ' COUNT(*) as "count" FROM ' . implode(', ', array_unique($tableList)); } else { if ($params["sum"] === true) { $query .= "SUM(" . implode("), SUM(", $rawFields) . ") FROM " . implode(", ", array_unique($tableList)); } else { if ($params["max"] === true) { $query .= "MAX(" . implode("), MAX(", $rawFields) . ") FROM " . implode(", ", array_unique($tableList)); } else { $query .= ($params["resolve"] === false ? implode(",", $rawFields) : implode(",", $fieldList)) . " FROM " . implode(",", array_unique($tableList)); } } } if (count($joinConditions) > 0) { $query .= " WHERE (" . implode(" AND ", $joinConditions) . ") "; $query .= strlen($params["conditions"]) > 0 ? " AND (" . $params["conditions"] . ")" : ""; } else { $query .= strlen($params["conditions"]) > 0 ? " WHERE " . $params["conditions"] : ""; } if (is_array($params["sort_field"]) && $params["count"] !== true) { if (count($params["sort_field"]) > 0) { $query .= " ORDER BY "; $orderClauses = array(); foreach ($params["sort_field"] as $sortField) { if ($sortField["field"] == "") { continue; } $orderClauses[] = "{$sortField["field"]} {$sortField["type"]}"; } $query .= implode(", ", $orderClauses); } } else { if ($params["sort_field"] != "" && $params["count"] !== true) { $query .= " ORDER BY {$params["sort_field"]} {$params["sort_type"]}"; } } if ($params["group_field"] != "") { $query .= " GROUP BY {$params["group_field"]}"; foreach ($rawFields as $field) { if ($field != $params["group_field"]) { $query .= "," . $field; } } } if (isset($params["limit"])) { $query .= " LIMIT ?"; $params['bind'][] = $params['limit']; } if (isset($params['offset'])) { $query .= " OFFSET ?"; $params['bind'][] = $params['offset']; } Application::log()->info($params['cache_key']); $data = $other_model->datastore->query($query, $mode, $params['bind'], $params['cache_key']); if (isset($params['cache_key'])) { Cache::add($params['cache_key'], $query); } if ($params["moreInfo"] === true) { if (isset($params['cache_key'])) { Cache::add("{$params['cache_key']}_meta", ['fieldInfos' => $fieldDescriptions, 'headers' => $headers, 'rawFields' => $rawFields]); } return array("data" => $data, "fieldInfos" => $fieldDescriptions, "headers" => $headers, "rawFields" => $rawFields); } else { return $data; } }
<?php chdir(__DIR__); require_once '../../vendor/autoload.php'; new Framework\Bootstrap('..'); echo "It works\n"; \Application::log()->info('test'); $tables = \Database\Database::getTables(); foreach ($tables as $table) { print_r($table); print_r($table->getColumns()); }
public static function query($query, $instance = null, $mode = null) { if ($instance === null) { $instance = Db::$lastInstance; } $instance = Db::getCachedInstance($instance); try { Application::log('DB')->debug("Query [{$query}]"); $result = $instance->query($query); } catch (PDOException $e) { throw new Exception("Error executing query [{$query}]. PDO says: {$e->getMessage()}"); } if ($mode == Db::MODE_ARRAY) { return $result->fetchAll(PDO::FETCH_NUM); } else { return $result->fetchAll(PDO::FETCH_ASSOC); } }
/** * Execute the request and return the value. * @return array */ public function request() { \Application::log()->debug('RESTRequest: ' . $this->getContentType()); \Application::log()->debug('RESTRequest: ' . $this->url); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $this->url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $this->requestType); $this->headers[] = 'Content-Type: ' . $this->getContentType(); if (!empty($this->basicAuth)) { curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); curl_setopt($ch, CURLOPT_USERPWD, $this->basicAuth); } if (!empty($this->post)) { $dataString = json_encode($this->post); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $dataString); $this->headers[] = 'Content-Length: ' . strlen($dataString); } curl_setopt($ch, CURLOPT_HTTPHEADER, $this->headers); foreach ($this->headers as $header) { \Application::log()->debug("RESTRequest header: {$header}"); } $data = curl_exec($ch); curl_close($ch); \Application::log()->debug("RESTRequest result: {$data}"); return $this->parse($data); }