/lib/controllers/common/parent.php
PHP | 645 lines | 304 code | 190 blank | 151 comment | 92 complexity | 4c0e89deca77fb07571f7ae470374935 MD5 | raw file
- <?php
- /**
- * Every *_Controller extends the Parent_Controller will
- * inherit all the properties and methods.
- */
- Class Parent_Controller {
-
- /**
- * Base path of the website.
- * @var string
- */
- protected $_basePath;
- /**
- * Model object
- * @var object
- */
- protected $_model;
- /**
- * Name of the template.
- * @var string
- */
- protected $_template;
- /**
- * Name of the page.
- * @var string
- */
- protected $_page;
- /**
- * View object.
- * @var object.
- */
- protected $_view;
- /**
- * Breadcrumbs navigation.
- * @var array
- */
- protected $_breadcrumbs;
- /**
- * Content to be outputted
- * @var array
- */
- protected $_content;
- /**
- * Sidebar content to be outputted
- * @var array
- */
- protected $_sidebar;
- /**
- * Messages to be outputted.
- * @var array
- */
- protected $_messages;
- /**
- * Whether to return only the data.
- * @var boolean
- */
- protected $_return;
- /**
- * GET variables.
- * @var array
- */
- protected $_getVars;
- /**
- * Scripts to be included.
- * @var array
- */
- protected $_scripts;
- /**
- * List of table headings.
- * @var array
- */
- protected $_tableHeadings;
- /**
- * Initialise the properties.
- * @param array $getVars GET variables
- * @param boolean $return Whether to only return data.
- */
- public function __construct($getVars = null){
- $this->_getVars = $getVars;
- $this->_return = $GLOBALS["return"];
- $this->_content = $this->_sidebar = $this->_messages = $this->_breadcrumbs = $this->_scripts = $this->_tableHeadings = array();
- $this->_page = $GLOBALS["options"]["page"];
- $this->_template = strtolower($this->_page);
- if($this->_return === false){
- // Store the base path.
- $this->_basePath = $GLOBALS["admin"] ? ADMIN_PATH . "/" . $this->_template : SITE_PATH . $this->_template;
- // If it's an admin page, add a parent breadcrumb.
- if($GLOBALS["admin"]) {
- $breadcrumb = array(ADMIN_PATH, "Control Panel");
- array_push($this->_breadcrumbs, $breadcrumb);
- }
- // Adds the page to the breadcrumb.
- $breadcrumb = array($this->_basePath, $this->_page);
- array_push($this->_breadcrumbs, $breadcrumb);
- }
- self::main();
- }
- /**
- * Call the assign() method.
- */
- public function __destruct(){
- self::assign();
- }
- /**
- * Creates the Model/View objects, then calls the action() method.
- * @param array $getVars GET variables.
- * @return [type] [description]
- */
- protected function main(){
- $this->_view = new View_Model($this->_page, !$this->_return);
- // Assigns the title to the view.
- $this->_view->assign("title", $this->_page);
-
- // Creates the Model, passing the View object
- // if the return property is false.
- $class = $this->_page."_Model";
- $this->_model = $this->_return === false ? new $class($this->_view) : new $class;
- $tempEntity = $this->_model->getEntity();
- session_start();
- //unset($_SESSION["cust_id"]);
- if(!isset($_SESSION["cust_id"]) || empty($_SESSION["cust_id"])){
- $this->_model->setEntity("customer");
- $customers = $this->_model->getAll();
- $lastCustomer = $customers[count($customers)-1];
- $customerID = $lastCustomer["cust_id"];
- while(true){
-
- if($this->_model->get($customerID)) $customerID++;
- else break;
- }
- $customerArray = array("", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "");
- if($this->_model->add($customerArray)) $_SESSION["cust_id"] = $this->_model->lastInsertID();
- }
- $this->_model->setEntity($tempEntity);
- if($GLOBALS["admin"]) array_push($this->_sidebar, file_get_contents(SIDEBAR_ROOT . "control-panel.php"));
- else {
- $GLOBALS["model"] = $this->_model;
- ob_start();
- require_once(SIDEBAR_ROOT . "categories.php");
- array_push($this->_sidebar, ob_get_clean());
- }
- self::action();
-
- }
- /**
- * Determines which action to perform.
- */
- protected function action(){
- // If a search query has been performed, remove
- // the query from the vars.
- if($this->_template === "search" && isset($_GET["query"])) array_shift($this->_getVars);
- // If an image has been requested, manually set
- // the action and remove id/thumbnail from vars.
- if($this->_template === "image" && isset($_GET["id"])){
-
- if(isset($_GET["thumbnail"])) array_shift($this->_getVars);
- array_shift($this->_getVars);
- $GLOBALS["options"]["action"] = "get";
- }
- // Check that the supplied argument is an array and isn't empty.
- if(isset($GLOBALS["options"]["action"])){
- // Store the ID.
- $id = isset($this->_getVars["id"]) ? $this->_getVars["id"] : false;
- // Call the appropriate method.
- switch($GLOBALS["options"]["action"]){
- case "add":
- static::add();
- break;
- case "edit":
- static::edit($id);
- break;
- case "get":
- static::get($id);
- break;
- case "getall":
- static::getAll();
- break;
- case "remove":
- static::remove($id);
- break;
- default:
- try {
- static::$GLOBALS["options"]["action"]();
- } catch(Exception $e) {
- // If the action does not exist, return to the page's home.
- echo 'Caught exception: ', $e->getMessage(), "\n";
- header("Location: ../" . static::getPage());
- }
-
- break;
- }
- if($GLOBALS["admin"] && !$GLOBALS["return"]) {
- // Adds the action breadcrumb.
- $breadcrumb = array($this->_basePath . "/" . $GLOBALS["options"]["action"], ucfirst($GLOBALS["options"]["action"]));
- array_push($this->_breadcrumbs, $breadcrumb);
- }
-
- } else static::home();
-
- }
- /**
- * Assigns data to the view.
- */
- protected function assign(){
- if($this->_return === false){
- $this->_view->assign("breadcrumbs", $this->_breadcrumbs);
- $this->_view->assign("content", $this->_content);
- $this->_view->assign("sidebar", $this->_sidebar);
- $this->_view->assign("message", $this->_messages);
- $this->_view->assign("scripts", $this->_scripts);
- } else {
- if(!empty($this->_content)) foreach($this->_content as $content) echo $content;
- if(!empty($this->_messages)){
- foreach($this->_messages as $messages) echo $messages;
-
- }
- }
-
- }
- /**
- * Returns the name of the page.
- * @return string Name of the page.
- */
- public function getPage(){
- return $this->_template;
- }
- /**
- * Outputs all the rows in the entity to a table.
- */
- protected function home(){
- // Attempts get all the rows from the database.
- if($items = $this->_model->getAll()){
- $columns = $this->_model->getColumns();
- $removableColumns = array("product" => array("prod_image_type", "prod_thumbnail"));
- foreach($removableColumns as $remove){
- if(is_array($remove)){
- foreach($remove as $column){
- if($index = array_search($column, $columns)) unset($columns[$index]);
- }
- }
- }
- if($this->_tableHeadings === null) $headings = $columns;
- else {
- if(count($this->_tableHeadings) === count($columns)) $headings = $this->_tableHeadings;
- else array_push($this->_messages, '<li class="error">The table headings are invalid [not the correct amount].</li>');
- }
- array_push($this->_content, "<h2>" . ucfirst($this->_template) . "</h2>");
- array_push($this->_content, "<p>You can change a cell by clicking on it. To remove a row, hover over the ID.</p>");
- $table = "<table id=\"" . $this->_template . "_table\" class=\"table\">\n";
- // Output the table headings.
- $table .= "<tr class=\"headings\">\n";
- $table .= "<th> </th>";
- foreach($headings as $heading) $table .= "<th>" . $heading . "</th>\n";
- $table .= "</tr>\n";
-
- $listIDs = "";
- foreach($items as $item){
- // Store list of IDs in hidden input.
- $listIDs .= $item[$columns[0]] . ",";
- // Output the table data.
- $table .= "<tr>";
- foreach($columns as $column){
-
- if($column == $columns[0]) $table .= '<td><a href="admin/' . $this->_template . '/remove/' . $item[$columns[0]] . '" class="remove" title="Remove the row"> </a></td>';
- $table .= "<td id=\"" . $item[$columns[0]] . "_" . $column . "\"";
- if($column == $columns[0]) $table .= ' class="primary" ';
- $table .= " title=\"\">";
- if($this->_template === "product"){
- if($column === "prod_image") $item[$column] = "<img src='image/" . $item[$columns[0]] . "/1' />";
- }
- $table .= $item[$column]
- . "</td>\n";
- }
- $table .= "</tr>";
- }
- $listIDs = substr($listIDs, 0, -1);
- $table .= "</table>\n";
- $table .= '<input type="hidden" id="' . $this->_template . '_ids" value="' . $listIDs . '" />';
- // Store list of columns in hidden input.
- $listColumns = "";
- foreach($columns as $column) $listColumns .= $column . ",";
- $listColumns = substr($listColumns, 0, -1);
- $table .= '<input type="hidden" id="' . $this->_template . '_columns" value="' . $listColumns . '" />';
- array_push($this->_content, $table);
- array_push($this->_scripts, "edit.js");
- }
- }
- /**
- * Processes the form variables, then inserts data into the entity.
- */
- protected function add($outputScript = true){
- $error = false;
- // Make sure the form has been submitted.
- if($_SERVER['REQUEST_METHOD'] == "POST"){
- // Get the entities columns.
- $columns = $this->_model->getColumns();
- // Shift the array to remove the Primary Key.
- array_shift($columns);
- if($this->_template === "product"){
- if(isset($_FILES["prod_image"])){
- $error = true;
- // Check for any errors.
- $imageError = $_FILES["prod_image"]["error"];
- if($imageError == 4) $no_image = true;
- else $no_image = false;
- // Image details.
- $imageName = $_FILES["prod_image"]["name"];
- $imageType = $_FILES["prod_image"]["type"];
- $imageSize = $_FILES["prod_image"]["size"];
- $imageTemporary = $_FILES["prod_image"]["tmp_name"];
-
- $extensionArray = array("jpg", "jpeg", "gif", "png");
- $extension = explode(".", $imageName);
- $extension = end($extension);
- // If image type is valid.
- if(($imageType == "image/gif") || ($imageType == "image/jpeg") || ($imageType == "image/png") || ($imageType == "image/pjpeg") || $no_image){
- // If image is less than 500KB.
- if($imageSize < 512000 || $no_image){
- // If image extension is in array.
- if(in_array($extension, $extensionArray) || $no_image){
- // If there was an error.
- if($imageError > 0 && !$no_image) array_push($this->_messages, '<li class="error">Return Code: ' . $imageError . '</li>');
- else {
- // If there is no image.
- if(!$no_image){
- // Store image data into variable.
- $imageData = file_get_contents($imageTemporary);
- // Load image.
- $image="";
- switch($imageType){
- case "image/jpeg":
- case "image/pjpeg":
- $image=imagecreatefromjpeg($imageTemporary);
- break;
- case "image/png":
- $image=imagecreatefrompng($imageTemporary);
- break;
- case "image/gif":
- $image=imagecreatefromgif($imageTemporary);
- break;
- }
-
- $thumbnailWidth = 150;
- // Get image size.
- $width = imagesx($image);
- $height = imagesy($image);
- // Calculate thumbnail size.
- $newWidth = $thumbnailWidth;
- $newHeight = floor($height * ($thumbnailWidth / $width));
- // Create a new temporary image.
- $temporaryImage = imagecreatetruecolor($newWidth, $newHeight);
- // Copy and resize old image into new image.
- imagecopyresampled($temporaryImage, $image, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
- $temporaryImagePath = $imageTemporary . time();
- // Save thumbnail into a file.
- switch($imageType){
- case "image/jpeg":
- case "image/pjpeg":
- imagejpeg($temporaryImage, $temporaryImagePath, 9);
- break;
- case "image/png":
- imagepng($temporaryImage, $temporaryImagePath, 9);
- break;
- case "image/gif":
- imagegif($temporaryImage, $temporaryImagePath, 9);
- break;
- }
- // Store thumbnail data into variable.
- $fileHandler = fopen($temporaryImagePath, 'r');
- $imageThumbnailData = fread($fileHandler, filesize($temporaryImagePath));
- fclose($fileHandler);
- $error = false;
- } else {
- $imageData = $imageThumbnailData = file_get_contents(IMG_PATH . "no_image.jpg");
- $imageType = "image/jpeg";
- $error = false;
- }
- }
- } else array_push($this->_messages, '<li class="error">File-extension is invalid. Please upload either: .jpg, .jpeg, .gif, or .png.</li>');
- } else array_push($this->_messages, '<li class="error">File-size is too big. Please upload an image with a maximum size of 500KB.</li>');
- } else array_push($this->_messages, '<li class="error">File-type invalid. Please upload either: image/gif, image/jpeg, image/png, or image/pjpeg.</li>');
- $_POST["prod_image"] = $imageData;
- $_POST["prod_image_type"] = $imageType;
- $_POST["prod_thumbnail"] = $imageThumbnailData;
- }
- }
- if(isset($error) && !$error){
- // Populate the data array.
- $data = array();
- foreach($columns as $column){
- if(strpos($column, "_last_update") !== false) $_POST[$column] = time();
- if(strpos($column, "_date") !== false) $_POST[$column] = time();
- array_push($data, $_POST[$column]);
- }
- // Attempts to add the data to the entity.
- if($this->_model->add($data)){
- array_push($this->_messages, '<li class="success">Added the '.$this->_template.' successfully.</li>');
- if($GLOBALS["admin"]) array_push($this->_messages, redirect("admin/" . $this->_template, "1000"));
- else array_push($this->_messages, redirect($this->_template, "1000"));
- return true;
- }
- }
- } else if($outputScript) array_push($this->_scripts, "ajax.php?entity=" . $this->_template . "&form_action=add&admin=true");
- return false;
- }
- /**
- * Processes the form variables, then changes the row in the entity.
- */
- protected function edit($columns = null){
- // Make sure the form has been submitted.
- if($_SERVER['REQUEST_METHOD']=="POST"){
- // Populate the data array.
- $data = array();
- foreach($this->_model->getColumns() as $column){
- if(strpos($column, "_last_update") !== false) $_POST[$column] = time();
- array_push($data, $_POST[$column]);
- }
- // Attempts to edit the data in the entity.
- if($this->_model->edit($data, $columns)){
- array_push($this->_messages, '<li class="success">Edited the '.$this->_template.' successfully.</li>');
- if($GLOBALS["admin"]) array_push($this->_messages, redirect("admin/" . $this->_template, "1000"));
- else array_push($this->_messages, redirect($this->_template, "1000"));
- return true;
- }
- } else array_push($this->_scripts, "ajax.php?entity=" . $this->_template . "&form_action=edit");
- return false;
- }
- /**
- * Returns a row from the entity.
- * @param string $value This can either be the ID of the row, or a string to search for.
- * @return array The row.
- */
- protected function get($value = null){
- if($value == null) $value = $_GET["id"];
- // Attempts get a row from the table.
- if($row = $this->_model->get($value)) return $row;
- else return false;
- }
- /**
- * Returns all the froms from an entity.
- * @param array $where Conditions to search for.
- * @return array All the rows.
- */
- protected function getAll($where = null){
- // Attempts get all the rows from the table.
- if($items = $this->_model->getAll($where)) return $items;
- else return false;
- }
- /**
- * Processes the form variables, then removes the row from the entity.
- */
- protected function remove($id = null){
- // Make sure the form has been submitted.
- if($_SERVER['REQUEST_METHOD']=="POST" || $id != null){
- // Get the entities columns.
- $columns = $this->_model->getColumns();
- // ID of row to be removed.
- if(isset($_POST[$columns[0]])) $id = $_POST[$columns[0]];
- // Attempts to remove the row from the entity.
- if($this->_model->remove($id)){
- array_push($this->_messages, '<li class="success">Removed the '.$this->_template.' successfully.</li>');
- if($GLOBALS["admin"]) array_push($this->_messages, redirect("admin/" . $this->_template, "1000"));
- else array_push($this->_messages, redirect($this->_template, "1000"));
- return true;
- }
- } else array_push($this->_scripts, "ajax.php?entity=" . $this->_template . "&form_action=remove");
- return false;
- }
- }