/** * Dispatch the request. */ public function dispatch() { if ($this->active) { // Initialize the core model $coreModel = new CoreModel($this->request); // Validate the request $requestValid = $coreModel->validateRequest(); $output = ''; if ($requestValid) { // Retrieve the correct module controller $controllerObj = $this->getRequestController(); // In case the controller could not be initialized, throw an exception if (!$controllerObj) { ErrorHandler::error(E_ERROR, 'The requested endpoint could not be initialized'); } // In case the module is inactive or the requested method does not exist, throw an exception if (!$controllerObj->active || !method_exists($controllerObj, $this->action)) { ErrorHandler::error(E_ERROR, "The requested action '%s' is not available", $this->action); } // Start an output buffer to catch request content ob_start(); // Execute the before action when present $beforeMethodName = 'before' . ucfirst($this->action); if (method_exists($controllerObj, $beforeMethodName)) { $controllerObj->{$beforeMethodName}(); } // Execute the requested action $controllerObj->{$this->action}(); // Execute the after action when present $afterMethodName = 'after' . ucfirst($this->action); if (method_exists($controllerObj, $afterMethodName)) { $controllerObj->{$afterMethodName}(); } // Retrieve the output buffer result $result = ob_get_clean(); // In case the request is AJAX, output the request result directly if ($this->request->ajax) { // Retrieve the header include content $header = $this->getHeaderIncludeHTML(); $output = $header . $result; } else { // Retrieve the output ob_start(); require_once $this->modulePath . DIR_VIEW . 'index.php'; $output = ob_get_clean(); } } } else { $output = $this->getMaintenanceView(); } // Set the output character set header('Content-type: text/html; charset=utf-8'); // header('Cache-Control: max-age=3600'); // Send the output exit($output); }
protected static function getDBH() { if (is_null(self::$dbh)) { $db = DB::getInstance(); self::$dbh = $db->getDBH(); } return self::$dbh; }
/** * Sets a model to the controller * * @param CoreModel $model The model */ public function setModel(CoreModel $model) { $this->{$model->getModelName()} = $model; }
public function __construct() { parent::__construct(); }
/** * binds the model to the Form. * * @param coreModel $model the model to represent */ public function initFromModel(CoreModel $model) { $this->model = $model; $modelName = explode('Model', $model->getModelName()); $this->setAction('/?controller=' . $modelName[0] . '&action=save'); $this->setName($modelName[0]); $this->setHiddenFields($this->model->keys); $fields = $this->model->getFields(); $this->model->configure(); $configuredFields = $this->model->getTypes(); $validators = $this->model->getValidators(); $formFields = array_keys($this->getFields()); if (is_array($fields) && count($fields)) { foreach ($fields as $fieldName) { if (!in_array($fieldName, $this->hiddenFields)) { $type = isset($configuredFields[$fieldName]) ? $configuredFields[$fieldName]['type'] : 'text'; if (!in_array($fieldName, $formFields)) { $element = FormElementFactory::getElement($type); $element->setType($type); $element->setAttributes($configuredFields[$fieldName]['attributes']); $element->setName($fieldName); $element->setLabel($fieldName); $element->setValue($model->{$fieldName}); $this->setValidators($element, $validators); $this->setField($element); } } } } }
define('DATABASE_USER', _USER_); define('DATABASE_PASS', _PWD_); define('DATABASE_HOST', _HOST_); require_once 'class.DBPDO.php'; use DBPDO; class CoreModel { private $data; function __construct() { $this->DB = new DBPDO(); } public function get() { $sql = "SELECT * FROM assistance "; $items = $this->DB->fetchAll($sql, null); $this->data = $items; return $this; } public function toJSON() { $results = array(); foreach ($this->data as $key => $page) { array_push($results, (object) $page); } header('Content-Type: application/json', true); echo json_encode($results); } } $coreModel = new CoreModel(); $coreModel->get()->toJSON();
/** * Set the static method with current connection * @param Connection $connection */ public static function _setConnection(Connection $connection) { self::$connection = $connection; }
public function __construct() { parent::__construct(); $this->database(); }