thexiv

MVC for Swatch www.github.com/swatchphp/swatch

Jan 16th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.44 KB | None | 0 0
  1. <?php
  2. //namespace Swatch\Container;
  3.  
  4. //include('required.php');
  5.  
  6.     class PageController {
  7.  
  8.         public $token;
  9.  
  10.        
  11.         /*
  12.         *
  13.         * public function __construct
  14.         * @parameters string
  15.         *
  16.         */
  17.         function __construct(string $tok) {
  18.             $this->mvc = null;
  19.             $this->token = $tok;
  20.             if (!is_dir("$this->token"))
  21.                 mkdir("$this->token");
  22.             if (!is_dir("$this->token/view/"))
  23.                 mkdir("$this->token/view/");
  24.             if (!file_exists("$this->token/config.json")) {
  25.                 $fp = fopen("$this->token/config.json", "w");
  26.                 fclose($fp);
  27.                 $fp = fopen("$this->token/index.php", "w");
  28.                 fclose($fp);
  29.             }
  30.             $this->mvc['index'] = new PageModels();
  31.         }
  32.        
  33.         /*
  34.         *
  35.         * public function save
  36.         * @parameters none
  37.         *
  38.         */
  39.         public function save() {
  40.             $fp = fopen("$this->token/config.json", "w");
  41.             fwrite($fp, json_encode($this->mvc));
  42.             fclose($fp);
  43.         }
  44.        
  45.        
  46.         /*
  47.         *
  48.         * public function paginateModels
  49.         * @parameters int, int
  50.         *
  51.         */
  52.         public function paginateModels(string $view_name, int $begin = 0, int $end = 0) {
  53.             $x = $this->mvc[$view_name]->paginateModels($this->token, $begin, $end);
  54.             return $x;
  55.            
  56.         }
  57.        
  58.         /*
  59.         *
  60.         * public function add_view
  61.         * @parameters string
  62.         *
  63.         */
  64.         private function add_view(string $view_name) {
  65.             if (is_dir("$this->token/view/$view_name")) {
  66.                 if (!file_exists("$this->token/view/$view_name/index.php")) {
  67.                     $fp = fopen("$this->token/view/$view_name/index.php", "w");
  68.                     fclose($fp);
  69.                 }
  70.                
  71.             }
  72.             else {
  73.                 mkdir("$this->token/view/$view_name/");
  74.                 if (!is_dir("$this->token/view/$view_name")) {
  75.                     echo "Permissions Error: Unable to create Directory";
  76.                     return 0;
  77.                 }
  78.                 $fp = fopen("$this->token/view/$view_name/index.php", "w");
  79.                 fclose($fp);
  80.                 $fp = fopen("$this->token/config.json", "w");
  81.                 fclose($fp);
  82.             }
  83.             $this->mvc[$view_name] = [];
  84.             return 1;
  85.         }
  86.        
  87.         /*
  88.         *
  89.         * public function newView
  90.         * @parameters string
  91.         *
  92.         */
  93.         public function newView(string $view_name) {
  94.             $this->add_view($view_name);
  95.         }
  96.        
  97.         /*
  98.         *
  99.         * public function addPartial
  100.         * @parameters string, string
  101.         *
  102.         */
  103.         public function addPartial(string $view_name, string $filename) {
  104.             $bool = 0;
  105.             if (!file_exists($filename)) {
  106.                 echo "Invalid Filename";
  107.                 return 0;
  108.             }
  109.             if (!$this->mvc[$view_name]) {
  110.                 echo 'No such View';
  111.                 return 0;
  112.             }
  113.             foreach ($this->mvc[$view_name]['partials'] as $v) {
  114.                 if ($v == $filename)
  115.                     $bool = 1;
  116.             }
  117.             if ($bool == 0)
  118.                 return 0;
  119.             else
  120.                 $this->mvc[$view_name]->partials[] = "$this->token/view/$view_name/partials/" . $filename;
  121.             return 1;
  122.         }
  123.        
  124.         /*
  125.         *
  126.         * public function changeTitle
  127.         * @parameters string, string
  128.         *
  129.         */
  130.         public function changeTitle(string $view_name, string $title) {
  131.             $bool = 0;
  132.            
  133.             if (!$this->mvc[$view_name]) {
  134.                 echo 'No such View';
  135.                 return 0;
  136.             }
  137.  
  138.             $this->mvc[$view_name]->title = $title;
  139.  
  140.             return 1;
  141.         }
  142.        
  143.         /*
  144.         *
  145.         * public function writePage
  146.         * @parameters string
  147.         *
  148.         */
  149.         public function writePage(string $view_name) {
  150.             $fp = fopen("$this->token/index.php", "w");
  151.             $buf = "<?php\r";
  152.             foreach ($this->mvc[$view_name]['partials'] as $v2) {
  153.                 $buf .= "require_once('$v2');\n";
  154.             }
  155.             fwrite($fp, $buf);
  156.             fclose($fp);
  157.             return 1;
  158.         }
  159.  
  160.        
  161.         /*
  162.         *
  163.         * public function loadJSON
  164.         * @parameters none
  165.         *
  166.         */
  167.         public function loadJSON() {
  168.             if (file_exists("$this->token/config.json") && filesize("$this->token/config.json") > 0)
  169.                 $fp = fopen("$this->token/config.json", "r");
  170.             else
  171.                 return 0;
  172.             $json_context = fread($fp, filesize("$this->token/config.json"));
  173.             return json_decode($json_context);
  174.         }
  175.  
  176.        
  177.         /*
  178.         *
  179.         * public function removePartial
  180.         * @parameters string, string
  181.         *
  182.         */
  183.         public function removePartial(string $view_name, string $partial) {
  184.             $bool = 0;
  185.             foreach ($this->mvc[$view_name]['partials'] as $v) {
  186.                 if ($v != $partial)
  187.                     $k = array_merge($k, array($v));
  188.                 else
  189.                     $bool = 1;
  190.             }
  191.             if ($bool == 1)
  192.                 return 1;
  193.             return 0;
  194.         }
  195.        
  196.     }
  197.  
  198.     class PageModels {
  199.    
  200.         public $model = array();
  201.         public $valid = array();
  202.  
  203.         function ___construct() {
  204.             $this->valid = [];
  205.             $this->model = [];
  206.         }
  207.  
  208.         /*
  209.         *
  210.         * public function addModelField
  211.         * @parameters string, string
  212.         *
  213.         */
  214.         public function addModelField(string $fieldname) {
  215.             $this->model[$fieldname] = null;
  216.             return 1;
  217.         }
  218.        
  219.         /*
  220.         *
  221.         * public function addModelData
  222.         * @parameters string, array
  223.         *
  224.         */
  225.         public function addModelData(array $data) {
  226.             $wrong_ans = [];
  227.             $this->checkValid($this->valid, $data, $wrong_ans);
  228.              
  229.             if (sizeof($this->model) == 0) {
  230.                 $this->model = $data;
  231.                 return 1;
  232.             }
  233.             $cnt = 0;
  234.             foreach ($this->model as $k=>$v) {
  235.                 if ($cnt == 0)
  236.                     $cnt = sizeof($this->valid);
  237.                 if (sizeof($data) != $cnt) {
  238.                     echo "Size of entry has " . sizeof($data) . " columns and should be $cnt";
  239.                     return 0;
  240.                 }
  241.             }
  242.            
  243.             foreach ($data as $k=>$v) {
  244.                 if ($wrong_ans[$k] == null)
  245.                     $this->model[$k] = null;
  246.                 else
  247.                     $this->model[$k] = $v;
  248.             }
  249.             return 1;
  250.         }
  251.        
  252.         /*
  253.         *
  254.         * public function paginateModels
  255.         * @parameters string, string, int, int
  256.         *
  257.         */
  258.         private function paginateModels(string $token, int $begin = 0, int $end = 0) {
  259.             $bool = 0;
  260.             $int_cnt = 0;
  261.             $buf = "<table>";
  262.                     $buf .= "<tr>";
  263.                     foreach ($this->model as $kn=>$vn) {
  264.                         $buf .= "<th>$kn</th>";
  265.  
  266.                     }
  267.                     $bool = 1;
  268.                     $buf .= "</tr>";
  269.                     $buf .= "<tr>";
  270.                     foreach ($this->model as $v1=>$va) {
  271.                         if ($begin <= $int_cnt || $end == 0 || $int_cnt < $end) {
  272.                             $buf .= "<td>$va</td>";
  273.                             $int_cnt++;
  274.                         }
  275.                         else break;
  276.                     }
  277.                     $buf .= "</tr>";
  278.                 $buf .= "</table>";
  279.                 //$bool
  280.                 $fp = fopen("$token/index.php", "a");
  281.                 fwrite($fp, $buf);
  282.                 fclose($fp);
  283.                 return $buf;
  284.         }
  285.        
  286.         /*
  287.         *
  288.         * public function addModelValid
  289.         * @parameters string, string, string
  290.         *
  291.         */
  292.         public function addModelValid(string $property, string $regex) {
  293.             $this->addModelField($property);
  294.             $this->valid[$property] = $regex;
  295.             return 1;
  296.         }
  297.  
  298.         /*
  299.         *
  300.         * public function checkValid
  301.         * @parameters string, array, array &
  302.         *
  303.         */
  304.         public function checkValid(array $valid, array $data, array &$wrong_ans) {
  305.             foreach ($data as $k => $v) {
  306.                 if ($v != null && !preg_match($valid[$k], $v)) {
  307.                     $wrong_ans[$k] = null;
  308.                 }
  309.                 else
  310.                     $wrong_ans[$k] = $v;
  311.             }
  312.             return 1;
  313.         }
  314.     }
  315.     $y = array("Address" => "BenSt", "Duration" => "fixed");
  316.  
  317.     $x = new PageController("adp");
  318.     $x->newView("BestPHPEverNow");
  319.     $x->mvc['index']->addModelField("Address");
  320.     $x->mvc['index']->addModelValid("Address",'/1.*/');
  321.     $x->mvc['index']->addModelValid("Duration",'/.*/');
  322.     $x->mvc['index']->addModelData($y);
  323.     $x->paginateModels('index',0,2);
  324.     $x->save();
  325.    // $x = $x->loadJSON();
  326.     echo json_encode($x);
Add Comment
Please, Sign In to add comment