Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //namespace Swatch\Container;
- //include('required.php');
- class PageController {
- public $token;
- /*
- *
- * public function __construct
- * @parameters string
- *
- */
- function __construct(string $tok) {
- $this->mvc = null;
- $this->token = $tok;
- if (!is_dir("$this->token"))
- mkdir("$this->token");
- if (!is_dir("$this->token/view/"))
- mkdir("$this->token/view/");
- if (!file_exists("$this->token/config.json")) {
- $fp = fopen("$this->token/config.json", "w");
- fclose($fp);
- $fp = fopen("$this->token/index.php", "w");
- fclose($fp);
- }
- $this->mvc['index'] = new PageModels();
- }
- /*
- *
- * public function save
- * @parameters none
- *
- */
- public function save() {
- $fp = fopen("$this->token/config.json", "w");
- fwrite($fp, json_encode($this->mvc));
- fclose($fp);
- }
- /*
- *
- * public function paginateModels
- * @parameters int, int
- *
- */
- public function paginateModels(string $view_name, int $begin = 0, int $end = 0) {
- $x = $this->mvc[$view_name]->paginateModels($this->token, $begin, $end);
- return $x;
- }
- /*
- *
- * public function add_view
- * @parameters string
- *
- */
- private function add_view(string $view_name) {
- if (is_dir("$this->token/view/$view_name")) {
- if (!file_exists("$this->token/view/$view_name/index.php")) {
- $fp = fopen("$this->token/view/$view_name/index.php", "w");
- fclose($fp);
- }
- }
- else {
- mkdir("$this->token/view/$view_name/");
- if (!is_dir("$this->token/view/$view_name")) {
- echo "Permissions Error: Unable to create Directory";
- return 0;
- }
- $fp = fopen("$this->token/view/$view_name/index.php", "w");
- fclose($fp);
- $fp = fopen("$this->token/config.json", "w");
- fclose($fp);
- }
- $this->mvc[$view_name] = [];
- return 1;
- }
- /*
- *
- * public function newView
- * @parameters string
- *
- */
- public function newView(string $view_name) {
- $this->add_view($view_name);
- }
- /*
- *
- * public function addPartial
- * @parameters string, string
- *
- */
- public function addPartial(string $view_name, string $filename) {
- $bool = 0;
- if (!file_exists($filename)) {
- echo "Invalid Filename";
- return 0;
- }
- if (!$this->mvc[$view_name]) {
- echo 'No such View';
- return 0;
- }
- foreach ($this->mvc[$view_name]['partials'] as $v) {
- if ($v == $filename)
- $bool = 1;
- }
- if ($bool == 0)
- return 0;
- else
- $this->mvc[$view_name]->partials[] = "$this->token/view/$view_name/partials/" . $filename;
- return 1;
- }
- /*
- *
- * public function changeTitle
- * @parameters string, string
- *
- */
- public function changeTitle(string $view_name, string $title) {
- $bool = 0;
- if (!$this->mvc[$view_name]) {
- echo 'No such View';
- return 0;
- }
- $this->mvc[$view_name]->title = $title;
- return 1;
- }
- /*
- *
- * public function writePage
- * @parameters string
- *
- */
- public function writePage(string $view_name) {
- $fp = fopen("$this->token/index.php", "w");
- $buf = "<?php\r";
- foreach ($this->mvc[$view_name]['partials'] as $v2) {
- $buf .= "require_once('$v2');\n";
- }
- fwrite($fp, $buf);
- fclose($fp);
- return 1;
- }
- /*
- *
- * public function loadJSON
- * @parameters none
- *
- */
- public function loadJSON() {
- if (file_exists("$this->token/config.json") && filesize("$this->token/config.json") > 0)
- $fp = fopen("$this->token/config.json", "r");
- else
- return 0;
- $json_context = fread($fp, filesize("$this->token/config.json"));
- return json_decode($json_context);
- }
- /*
- *
- * public function removePartial
- * @parameters string, string
- *
- */
- public function removePartial(string $view_name, string $partial) {
- $bool = 0;
- foreach ($this->mvc[$view_name]['partials'] as $v) {
- if ($v != $partial)
- $k = array_merge($k, array($v));
- else
- $bool = 1;
- }
- if ($bool == 1)
- return 1;
- return 0;
- }
- }
- class PageModels {
- public $model = array();
- public $valid = array();
- function ___construct() {
- $this->valid = [];
- $this->model = [];
- }
- /*
- *
- * public function addModelField
- * @parameters string, string
- *
- */
- public function addModelField(string $fieldname) {
- $this->model[$fieldname] = null;
- return 1;
- }
- /*
- *
- * public function addModelData
- * @parameters string, array
- *
- */
- public function addModelData(array $data) {
- $wrong_ans = [];
- $this->checkValid($this->valid, $data, $wrong_ans);
- if (sizeof($this->model) == 0) {
- $this->model = $data;
- return 1;
- }
- $cnt = 0;
- foreach ($this->model as $k=>$v) {
- if ($cnt == 0)
- $cnt = sizeof($this->valid);
- if (sizeof($data) != $cnt) {
- echo "Size of entry has " . sizeof($data) . " columns and should be $cnt";
- return 0;
- }
- }
- foreach ($data as $k=>$v) {
- if ($wrong_ans[$k] == null)
- $this->model[$k] = null;
- else
- $this->model[$k] = $v;
- }
- return 1;
- }
- /*
- *
- * public function paginateModels
- * @parameters string, string, int, int
- *
- */
- private function paginateModels(string $token, int $begin = 0, int $end = 0) {
- $bool = 0;
- $int_cnt = 0;
- $buf = "<table>";
- $buf .= "<tr>";
- foreach ($this->model as $kn=>$vn) {
- $buf .= "<th>$kn</th>";
- }
- $bool = 1;
- $buf .= "</tr>";
- $buf .= "<tr>";
- foreach ($this->model as $v1=>$va) {
- if ($begin <= $int_cnt || $end == 0 || $int_cnt < $end) {
- $buf .= "<td>$va</td>";
- $int_cnt++;
- }
- else break;
- }
- $buf .= "</tr>";
- $buf .= "</table>";
- //$bool
- $fp = fopen("$token/index.php", "a");
- fwrite($fp, $buf);
- fclose($fp);
- return $buf;
- }
- /*
- *
- * public function addModelValid
- * @parameters string, string, string
- *
- */
- public function addModelValid(string $property, string $regex) {
- $this->addModelField($property);
- $this->valid[$property] = $regex;
- return 1;
- }
- /*
- *
- * public function checkValid
- * @parameters string, array, array &
- *
- */
- public function checkValid(array $valid, array $data, array &$wrong_ans) {
- foreach ($data as $k => $v) {
- if ($v != null && !preg_match($valid[$k], $v)) {
- $wrong_ans[$k] = null;
- }
- else
- $wrong_ans[$k] = $v;
- }
- return 1;
- }
- }
- $y = array("Address" => "BenSt", "Duration" => "fixed");
- $x = new PageController("adp");
- $x->newView("BestPHPEverNow");
- $x->mvc['index']->addModelField("Address");
- $x->mvc['index']->addModelValid("Address",'/1.*/');
- $x->mvc['index']->addModelValid("Duration",'/.*/');
- $x->mvc['index']->addModelData($y);
- $x->paginateModels('index',0,2);
- $x->save();
- // $x = $x->loadJSON();
- echo json_encode($x);
Add Comment
Please, Sign In to add comment