PHP Classes

File: modules/new/Controller.php

Recommend this page to a friend!
  Classes of Adrian M   upMVC   modules/new/Controller.php   Download  
File: modules/new/Controller.php
Role: Class source
Content type: text/plain
Description: Class source
Class: upMVC
Pure PHP web development without other frameworks
Author: By
Last change: General code updates.

General code updates.
Date: 2 months ago
Size: 6,477 bytes
 

Contents

Class file image Download
<?php
//declare(strict_types=1)
/**
 * Generated by PHPistols
    The PHP code generator https://www.phpistols.com

    Created on Tue Oct 31 2023
 
    Copyright (c) 2023 BitsHost
    All rights reserved.

 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:

 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.

 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 * Here you may host your app for free:
     https://bitshost.biz/
 */

namespace New;

//use New\Model
//use New\View
use PDO;
use
Exception;


class
Controller

{
   
//var $errorInfo

  
public function display($reqRoute, $reqMet){
   
$this->phPistols();
    echo
$reqMet . " " . $reqRoute . " ";
   }

  

    private function
phPistols()
    {
       
ini_set('display_errors', 'On');
       
error_reporting(E_ALL ^ E_WARNING ^ E_NOTICE);

       
define('ITEMS_PER_PAGE', 4);
       
define('THIS_DIR', rtrim(dirname(__FILE__), '/\\'));


       
$HTMLUsers = new View();
       
$dataUsers = new Model();



       
$HTMLUsers->header();

       
$page = $_GET['page'];
        if (
$page == 0) {
           
$page = 1;
        }

       
//add your own filters

       
$dataUsers->id = intval($_POST['id']);

       
$dataUsers->name = filter_var($_POST['name'], FILTER_SANITIZE_SPECIAL_CHARS);
       
$dataUsers->email = filter_var($_POST['email'], FILTER_SANITIZE_SPECIAL_CHARS);


       
$fieldsNames = array('id', 'name', 'email');

       
$orderBy = '';
       
/*
if(in_array($_GET['orderBy'], $fieldsNames)){
    $orderBy = $_GET['orderBy']
}
*/ // Get index of the field
       
$orderBy = array_search($_GET['orderBy'], $fieldsNames) + 1;

       
$order = 'ASC';
        if (
$_GET['order']) {
           
$order = strtoupper($_GET['order']);
        }

       
$task = $_GET['task'];
        if (
$task == 'edit' && $_POST) {
           
$dataUsers->id = intval($_POST['id']);
        } elseif (
$_GET['id'] != null) {
           
$dataUsers->id = intval($_GET['id']);
        }

       
$HTMLUsers->submenu();

        switch (
$task) {


               
//-----------------------------------------------------------------------------------------
                // Edit or add
                //-----------------------------------------------------------------------------------------
           
case 'edit':
               
// get calendar date for this training
                // Get training infos
               
if ($_POST) {
                    try {
                       
$dataUsers->save();
                       
$message['type'] = 'success';
                       
$message['text'] = 'The entry has been saved';
                    } catch (
Exception $e) {
                       
$message['type'] = 'danger';
                        if (
$e->$this->errorInfo[0] == 23000) {
                           
$message['text'] = 'Duplicate element';
                        } else {
                           
$message['text'] = 'Error saving data';
                        }
                    }
                }

                try {
                    if (
$dataUsers->id) {
                       
$dataUsers->get();
                    }
                } catch (
Exception $e) {
                   
$message['type'] = 'danger';
                   
$message['text'] = 'Error retrieving data';
                }

               
$HTMLUsers->form($dataUsers, $message);

                break;



               
//-----------------------------------------------------------------------------------------
                // Delete
                //-----------------------------------------------------------------------------------------
           
case 'del':
                if (
$dataUsers->id) {
                    try {
                       
$dataUsers->del();
                       
$message['type'] = 'success';
                       
$message['text'] = 'Done';
                    } catch (
Exception $e) {
                       
$message['type'] = 'danger';
                       
$message['text'] = 'Error deleting data';
                    }
                } else {
                   
$message['type'] = 'danger';
                   
$message['text'] = 'Missing parameter';
                }
               
// Continue to list

                //-----------------------------------------------------------------------------------------
                // List
                //-----------------------------------------------------------------------------------------
           
case 'edit-list':
               
// Display items list for edition

               
try {
                   
$nbItems = $dataUsers->getList(($page * ITEMS_PER_PAGE) - ITEMS_PER_PAGE, ITEMS_PER_PAGE, $orderBy, $order);
                } catch (
Exception $e) {
                   
$message['type'] = 'danger';
                   
$message['text'] = 'Error retrieving data';
                }

               
$revertOrder = 'ASC';
                if (
$order == 'ASC') {
                   
$revertOrder = 'DESC';
                }
               
$HTMLUsers->editList($dataUsers->list, '?option=users&task=edit-list&', $fieldsNames[$orderBy - 1], $revertOrder, $message);

               
$nbPages = ceil($nbItems / ITEMS_PER_PAGE);
               
$HTMLUsers->pagination($nbPages, $page, "?option=users&task=edit-list&");
                break;


               
//-----------------------------------------------------------------------------------------
                // Display details
                //-----------------------------------------------------------------------------------------
           
case 'view':
               
// Display selected trainings
               
try {
                   
$dataUsers->get();
                } catch (
Exception $e) {
                   
$message['type'] = 'danger';
                   
$message['text'] = 'Error retrieving data';
                }
               
$HTMLUsers->displayDetails($dataUsers, $message);
                break;

               
//-----------------------------------------------------------------------------------------
                // Display List
                //-----------------------------------------------------------------------------------------
           
default:
               
// Display items list
               
try {
                   
$nbItems = $dataUsers->getList(($page * ITEMS_PER_PAGE) - ITEMS_PER_PAGE, ITEMS_PER_PAGE, $orderBy, $order);
                } catch (
Exception $e) {
                   
$message['type'] = 'danger';
                   
$message['text'] = 'Error retrieving data';
                }

               
$revertOrder = 'ASC';
                if (
$order == 'ASC') {
                   
$revertOrder = 'DESC';
                }
               
$HTMLUsers->displayList($dataUsers->list, '?option=users&', $fieldsNames[$orderBy - 1], $revertOrder, $message);

               
$nbPages = ceil($nbItems / ITEMS_PER_PAGE);
               
$HTMLUsers->pagination($nbPages, $page, "?option=users&");
                break;
        }

       
$HTMLUsers->footer();
    }
}