thexiv

SwatchMVC

Jan 17th, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 13.75 KB | None | 0 0
  1. <?php
  2. //namespace Swatch\Container;
  3.  
  4. //include('required.php');
  5.  
  6.     class PageControllers {
  7.  
  8.         public $token;
  9.         public $mvc = array();
  10.        
  11.         /*
  12.         *
  13.         * public function __construct
  14.         * @parameters string, string
  15.         *
  16.         */
  17.         function __construct(string $tok, string $view = 'index') {
  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.                 touch("$this->token/index.php");
  26.                 touch("$this->token/config.json");
  27.             }
  28.             $this->mvc['index'] = new PageModels('index');
  29.             $this->mvc['index']->view = new PageViews($tok, $view);
  30.         }
  31.  
  32.        
  33.         /*
  34.         *
  35.         * public function addModelData
  36.         * @parameters string, array
  37.         *
  38.         */
  39.         public function addModelData(string $view_name, array $data) {
  40.             $this->mvc[$view_name]->addModelData($view_name, $data);
  41.             return 1;
  42.         }
  43.  
  44.         /*
  45.         *
  46.         * public function save
  47.         * @parameters none
  48.         *
  49.         */
  50.         public function save() {
  51.             $fp = fopen("$this->token/config.json", "w");
  52.             fwrite($fp, json_encode($this));
  53.             fclose($fp);
  54.         }
  55.        
  56.        
  57.         /*
  58.         *
  59.         * public function paginateModels
  60.         * @parameters string, int, int
  61.         *
  62.         */
  63.         public function paginateModels(string $view_name, int $begin = 0, int $end = 0) {
  64.             $x = $this->mvc[$view_name]->paginateModels($this->token, $view_name, $begin, $end);
  65.             return $x;
  66.            
  67.         }
  68.        
  69.         /*
  70.         *
  71.         * private function add_view
  72.         * @parameters string
  73.         *
  74.         */
  75.         private function add_view(string $view_name) {
  76.             if (is_dir("$this->token/view/$view_name")) {
  77.                 if (!file_exists("$this->token/view/$view_name/index.php")) {
  78.                     $fp = fopen("$this->token/view/$view_name/index.php", "w");
  79.                     fclose($fp);
  80.                 }
  81.                
  82.             }
  83.             else {
  84.                 mkdir("$this->token/view/$view_name/");
  85.                 if (!is_dir("$this->token/view/$view_name")) {
  86.                     echo "Permissions Error: Unable to create Directory";
  87.                     return 0;
  88.                 }
  89.                
  90.                 touch("$this->path/view/$view_name/index.php");
  91.                 touch("$this->token/config.json");
  92.             }
  93.             $this->mvc[$view_name] = new PageModels($view_name);
  94.             $this->mvc[$view_name]->view = new PageViews($this->token, $view_name);
  95.             return 1;
  96.         }
  97.        
  98.         /*
  99.         *
  100.         * public function newView
  101.         * @parameters string
  102.         *
  103.         */
  104.         public function newView(string $view_name) {
  105.             $this->add_view($view_name);
  106.         }
  107.        
  108.        
  109.         /*
  110.         *
  111.         * public function loadJSON
  112.         * @parameters none
  113.         *
  114.         */
  115.         public function loadJSON() {
  116.             if (file_exists("$this->token/config.json") && filesize("$this->token/config.json") > 0)
  117.                 $fp = fopen("$this->token/config.json", "r");
  118.             else
  119.                 return 0;
  120.             $json_context = fread($fp, filesize("$this->token/config.json"));
  121.             $json = json_decode($json_context);
  122.             $obj = new PageControllers($json->token);
  123.             foreach ($json->mvc as $key=>$value) {
  124.             //index,Best
  125.                 foreach($json->mvc->$key as $ky=>$vl) {
  126.                     if ($ky == "view") {
  127.                         foreach($vl as $k=>$v)
  128.                             if ($k == "copy")
  129.                                 $obj->newView($v);
  130.                     }
  131.                 }
  132.                 foreach($json->mvc->$key as $ky=>$vl) {
  133.                     if ($ky == "valid") {
  134.                         foreach($vl as $k=>$v)
  135.                             $obj->mvc[$key]->addModelValid($k,$v);
  136.                     }
  137.                 }
  138.                 foreach($json->mvc->$key as $ky=>$vl) {
  139.                 //Second level auto 'index'
  140.                     if ($ky == "data") {
  141.                         $marray = [];
  142.                         foreach($vl as $k=>$v) {
  143.                             foreach($v as $k1=>$v1) {
  144.                                 $marray = array_merge($marray, array($k1=>$v1));
  145.                             }
  146.                         }
  147.                         foreach($vl as $k=>$v) {
  148.                             $obj->mvc[$key]->addModelData($k, $marray);
  149.                         }
  150.                     }
  151.                 }
  152.                 foreach($json->mvc->$key as $ky=>$vl) {
  153.                     if ($ky == "view") {
  154.                         foreach($vl as $k=>$v) {
  155.                             if ($k == "partials") {
  156.                                 foreach($v as $r)
  157.                                     $obj->mvc[$key]->view->addPartial($r);
  158.                             }
  159.                         }
  160.                     }
  161.                 }
  162.             }
  163.             return $obj;
  164.  
  165.         }
  166.        
  167.         /*
  168.         *
  169.         * public function addPartial
  170.         * @parameters string
  171.         *
  172.         */
  173.         public function addPartial(string $filename) {
  174.             return $this->view->addPartial($filename);
  175.         }
  176.     }
  177.  
  178.     class PageViews {
  179.  
  180.         public $path;
  181.         public $partials = array();
  182.         public $token;
  183.         /*
  184.         *
  185.         * public function __construct
  186.         * @parameters string, string
  187.         *
  188.         */
  189.         function __construct(string $token, string $view_name) {
  190.             $this->path = "$token/view/$view_name";
  191.             $this->copy = $view_name;
  192.             $this->partials = [];
  193.             $this->token = $token;
  194.         }
  195.        
  196.         /*
  197.         *
  198.         * public function addPartial
  199.         * @parameters string
  200.         *
  201.         */
  202.         public function addPartial(string $filename) {
  203.             $bool = 0;
  204.             if (!is_dir("$this->path/partials/"))
  205.                 mkdir("$this->path/partials");
  206.             if (!is_dir("$this->path/partials/")) {
  207.                 echo "No permissions";
  208.                 return 0;
  209.             }
  210.             if (!file_exists("$this->path/partials/$filename")) {
  211.                 echo "Invalid Filename";
  212.             }
  213.             touch("$this->path/partials/$filename");
  214.             foreach ($this->partials as $v) {
  215.                 if ($v == $filename)
  216.                     $bool = 1;
  217.             }
  218.             if ($bool == 1)
  219.                 return 0;
  220.             else
  221.                 $this->partials[] = "$filename";
  222.             return 1;
  223.         }
  224.        
  225.         /*
  226.         *
  227.         * public function changeTitle
  228.         * @parameters string, string
  229.         *
  230.         */
  231.         public function changeTitle(string $view_name, string $title) {
  232.             $bool = 0;
  233.            
  234.             if (!$this->partials) {
  235.                 echo 'No such View';
  236.                 return 0;
  237.             }
  238.  
  239.             $this->partials[$view_name]->title = $title;
  240.  
  241.             return 1;
  242.         }
  243.        
  244.         /*
  245.         *
  246.         * public function writePage
  247.         * @parameters string
  248.         *
  249.         */
  250.         public function writePage(string $view_name) {
  251.             $fp = fopen("$this->token/view/$view_name/index.php", "w");
  252.             $buf = "<?php\r";
  253.             foreach ($this->partials as $v2) {
  254.                 $buf .= "require_once('partials/" . $v2 . "');\n";
  255.             }
  256.             fwrite($fp, $buf);
  257.             fclose($fp);
  258.             return 1;
  259.         }
  260.  
  261.         /*
  262.         *
  263.         * public function removePartial
  264.         * @parameters string, string
  265.         *
  266.         */
  267.         public function removePartial(string $view_name, string $partial) {
  268.             $bool = 0;
  269.             foreach ($this->partials[$view_name] as $v) {
  270.                 if ($v != $partial)
  271.                     $k = array_merge($k, array($v));
  272.                 else
  273.                     $bool = 1;
  274.             }
  275.             if ($bool == 1)
  276.                 return 1;
  277.             return 0;
  278.         }
  279.     }
  280.  
  281.     class PageModels {
  282.    
  283.         public $model = array();
  284.         public $valid = array();
  285.         public $data = array();
  286.         public $copy;
  287.  
  288.        
  289.         /*
  290.         *
  291.         * public function __construct
  292.         * @parameters string
  293.         *
  294.         */
  295.         function ___construct(string $view_name) {
  296.             $this->valid = [];
  297.             $this->model = [];
  298.             $this->copy = $view_name;
  299.         }
  300.  
  301.         /*
  302.         *
  303.         * public function addModelField
  304.         * @parameters string
  305.         *
  306.         */
  307.         public function addModelField(string $fieldname) {
  308.             if ($fieldname == null)
  309.                 return 0;
  310.             $this->model[$fieldname] = null;
  311.             return 1;
  312.         }
  313.        
  314.         /*
  315.         *
  316.         * public function addModelData
  317.         * @parameters string, array
  318.         *
  319.         */
  320.         public function addModelData(string $view_name, array $data) {
  321.             $wrong_ans = [];
  322.             $this->checkValid($this->valid, $data, $wrong_ans);
  323.              
  324.             if (sizeof($this->model) == 0) {
  325.                 $this->model = $data;
  326.                 return 1;
  327.             }
  328.             $cnt = 0;
  329.             foreach ($this->model as $k=>$v) {
  330.                 if ($cnt == 0)
  331.                     $cnt = sizeof($this->valid);
  332.                 if (sizeof($data) != $cnt) {
  333.                     echo "Size of entry has " . sizeof($data) . " columns and should be $cnt";
  334.                     return 0;
  335.                 }
  336.             }
  337.             foreach ($data as $k=>$v) {
  338.                 if ($wrong_ans[$k] == null)
  339.                     $this->data[$view_name][$k] = null;
  340.                 else
  341.                     $this->data[$view_name][$k] = $v;
  342.             }
  343.             return 1;
  344.         }
  345.        
  346.         /*
  347.         *
  348.         * public function paginateModels
  349.         * @parameters string, string, int, int
  350.         *
  351.         */
  352.         public function paginateModels(string $token, string $view_name, int $begin = 0, int $end = 0) {
  353.             $bool = 0;
  354.             $int_cnt = 0;
  355.             $buf = "echo '<table>";
  356.                     $buf .= "<tr>";
  357.                     foreach ($this->model as $kn=>$vn) {
  358.                         if ($begin == $int_cnt || $end == 0 || $int_cnt < $end) {
  359.                             $buf .= "<th>$kn</th>";
  360.                         }
  361.                         $int_cnt++;
  362.                     }
  363.                     $int_cnt = 0;
  364.                     $bool = 1;
  365.                     $buf .= "</tr>";
  366.                     $int_dat = 0;
  367.                     foreach ($this->data as $v1=>$va) {
  368.                         $buf .= "<tr>";
  369.                         foreach ($va as $k2=>$v2) {
  370.                            
  371.                             if ($begin == $int_cnt || $end == 0 || $int_cnt < $end) {
  372.                                 $buf .= "<td>$v2</td>";
  373.                             }
  374.                             $int_cnt++;
  375.                         }
  376.                         $int_cnt = 0;
  377.                         $int_dat++;
  378.                         $buf .= "</tr>";
  379.                     }
  380.                 $buf .= "</table>';";
  381.                 //$bool
  382.                 $fp = null;
  383.                 if ($view_name == 'index')
  384.                     $fp = fopen("$token/index.php", "a");
  385.                 else
  386.                     $fp = fopen("$token/view/$view_name/index.php", "a");
  387.                 fwrite($fp, $buf);
  388.                 fclose($fp);
  389.                 return $buf;
  390.         }
  391.        
  392.         /*
  393.         *
  394.         * public function addModelValid
  395.         * @parameters string, string
  396.         *
  397.         */
  398.         public function addModelValid(string $property, string $regex) {
  399.             $this->addModelField($property);
  400.             $this->valid[$property] = $regex;
  401.             return 1;
  402.         }
  403.  
  404.         /*
  405.         *
  406.         * public function checkValid
  407.         * @parameters array, array, array &
  408.         *
  409.         */
  410.         public function checkValid(array $valid, array $data, array &$wrong_ans = array()) {
  411.             foreach ($data as $k => $v) {
  412.                 if ($v != null && !preg_match($valid[$k], $v)) {
  413.                     $wrong_ans[$k] = null;
  414.                 }
  415.                 else
  416.                     $wrong_ans[$k] = $v;
  417.             }
  418.             return 1;
  419.         }
  420.     }
  421.     $y = array("Address" => "BenSt", "Duration" => "fixed");
  422.     $z = array("Address" => "25th", "Duration" => "limited");
  423.  
  424.     $x = new PageControllers("adp");
  425.     $x->newView("BestPHPEverNow");
  426.     $x->mvc['index']->addModelField("Address");
  427.     $x->mvc['index']->addModelValid("Address",'/.*/');
  428.     $x->mvc['index']->addModelValid("Duration",'/.*/');
  429.     $x->mvc['index']->addModelData('index', $y);
  430.    
  431.     $x->mvc['BestPHPEverNow']->view->addPartial("index.php");
  432.     $x->mvc['BestPHPEverNow']->addModelValid("Address",'/1.*/');
  433.     $x->mvc['BestPHPEverNow']->addModelValid("Duration",'/.*/');
  434.     $x->mvc['BestPHPEverNow']->addModelData('index', $y);
  435.     $x->mvc['BestPHPEverNow']->addModelData('friends', $z);
  436.     $x->mvc['BestPHPEverNow']->view->writePage("BestPHPEverNow");
  437.     $x->save();
  438.     $x->paginateModels('BestPHPEverNow',0,2);
  439.     echo json_encode($x);
  440.     $x = $x->loadJSON();
  441.  
  442.     echo "<br><br><br>";
  443.     echo json_encode($x);
Add Comment
Please, Sign In to add comment