Ubiquity  2.0.0
php rapid development framework
ModelsConfigTrait.php
Go to the documentation of this file.
1 <?php
2 
4 
5 use Ajax\JsUtils;
15 
23 
24  abstract public function _getAdminData();
25 
26  abstract public function _getAdminViewer();
27 
32  abstract public function _getAdminFiles();
33  private $activeStep=5;
34  private $engineering="forward";
35  private $steps=[ "forward" => [ [ "toggle on","Engineering","Forward" ],[ "settings","Conf","Database configuration" ],[ "database","Connexion","Database connexion" ],[ "sticky note","Models","Models generation" ],[ "lightning","Cache","Models cache generation" ] ],"reverse" => [ [ "toggle off","Engineering","Reverse" ],[ "sticky note","Models","Models configuration/implementation" ],[ "lightning","Cache","Models cache generation" ],[ "database plus","Database","Database creation" ] ] ];
36 
37  public function _getModelsStepper() {
38  $this->_checkStep();
39  $stepper=$this->jquery->semantic()->htmlStep("stepper");
40  $stepper->setStartStep(1);
41  $steps=$this->steps[$this->engineering];
42  $count=\sizeof($steps);
43  $completed=($this->_isModelsCompleted()) ? "completed" : "";
44  for($index=0; $index < $count; $index++) {
45  $step=$steps[$index];
46  $step=$stepper->addStep($step);
47  if ($index === 0) {
48  $step->addClass("_noStep")->getOnClick($this->_getAdminFiles()->getAdminBaseRoute() . "/_changeEngineering/" . $this->engineering . "/" . $completed, "#stepper", [ "jqueryDone" => "replaceWith","hasLoader" => false ]);
49  } else {
50  $step->setProperty("data-ajax", $index);
51  }
52  }
53  $stepper->setActiveStep($this->activeStep);
54  $_SESSION["step"]=$this->activeStep;
55  $stepper->asLinks();
56  $this->jquery->getOnClick(".step:not(._noStep)", $this->_getAdminFiles()->getAdminBaseRoute() . "/_loadModelStep/" . $this->engineering . "/", "#models-main", [ "attr" => "data-ajax" ]);
57  return $stepper;
58  }
59 
60  public function _isModelsCompleted() {
61  return \sizeof($this->steps[$this->engineering]) === $this->activeStep;
62  }
63 
64  public function _changeEngineering($oldEngineering, $completed=null) {
65  $this->engineering="forward";
66  if ($oldEngineering === "forward") {
67  $this->engineering="reverse";
68  }
69  $this->activeStep=\sizeof($this->getModelSteps());
70  echo $this->_getModelsStepper();
71  if ($completed !== "completed")
72  $this->jquery->get($this->_getAdminFiles()->getAdminBaseRoute() . "/_loadModelStep/" . $this->engineering . "/" . $this->activeStep, "#models-main");
73  echo $this->jquery->compile($this->view);
74  }
75 
76  protected function getModelSteps() {
77  return $this->steps[$this->engineering];
78  }
79 
80  protected function getActiveModelStep() {
81  if (isset($this->getModelSteps()[$this->activeStep]))
82  return $this->getModelSteps()[$this->activeStep];
83  return end($this->steps);
84  }
85 
86  protected function getNextModelStep() {
87  $steps=$this->getModelSteps();
88  $nextIndex=$this->activeStep + 1;
89  if ($nextIndex < \sizeof($steps))
90  return $steps[$nextIndex];
91  return null;
92  }
93 
94  public function _loadModelStep($engineering=null, $newStep=null) {
95  if (isset($engineering))
96  $this->engineering=$engineering;
97  if (isset($newStep)) {
98  $this->_checkStep($newStep);
99  if ($newStep !== @$_SESSION["step"]) {
100  if (isset($_SESSION["step"])) {
101  $oldStep=$_SESSION["step"];
102  $this->jquery->execAtLast('$("#item-' . $oldStep . '.step").removeClass("active");');
103  }
104  }
105  $this->jquery->execAtLast('$("#item-' . $newStep . '.step").addClass("active");');
106  $this->activeStep=$newStep;
107  $_SESSION["step"]=$newStep;
108  }
109 
110  $this->displayAllMessages();
111 
112  echo $this->jquery->compile($this->view);
113  }
114 
115  public function _importFromYuml() {
116  $yumlContent="[User|«pk» id:int(11);name:varchar(11)],[Groupe|«pk» id:int(11);name:varchar(11)],[User]0..*-0..*[Groupe]";
117  $bt=$this->jquery->semantic()->htmlButton("bt-gen", "Generate models", "green fluid");
118  $bt->postOnClick($this->_getAdminFiles()->getAdminBaseRoute() . "/_generateFromYuml", "{code:$('#yuml-code').val()}", "#stepper", [ "attr" => "","jqueryDone" => "replaceWith" ]);
119  $menu=$this->_yumlMenu("/_updateYumlDiagram", "{refresh:'true',code:$('#yuml-code').val()}", "#diag-class");
120  $this->jquery->exec('$("#modelsMessages-success").hide()', true);
121  $menu->compile($this->jquery, $this->view);
122  $form=$this->jquery->semantic()->htmlForm("frm-yuml-code");
123  $textarea=$form->addTextarea("yuml-code", "Yuml code", \str_replace(",", ",\n", $yumlContent . ""));
124  $textarea->getField()->setProperty("rows", 20);
125  $diagram=$this->_getYumlImage("plain", $yumlContent);
126  $this->jquery->execAtLast('$("#yuml-tab .item").tab();');
127  $this->jquery->compile($this->view);
128  $this->loadView($this->_getAdminFiles()->getViewYumlReverse(), [ "diagram" => $diagram ]);
129  }
130 
131  public function _generateFromYuml() {
132  if (URequest::isPost()) {
133  $config=Startup::getConfig();
134  $yumlGen=new YumlModelsCreator();
135  $yumlGen->initYuml($_POST["code"]);
136  \ob_start();
137  $yumlGen->create($config);
138  \ob_get_clean();
139  Startup::forward($this->_getAdminFiles()->getAdminBaseRoute() . "/_changeEngineering/completed");
140  }
141  }
142 
143  public function _updateYumlDiagram() {
144  if (URequest::isPost()) {
145  $type=$_POST["type"];
146  $size=$_POST["size"];
147  $yumlContent=$_POST["code"];
148  echo $this->_getYumlImage($type . $size, $yumlContent);
149  echo $this->jquery->compile();
150  }
151  }
152 
153  private function _yumlMenu($url="/_updateDiagram", $params="{}", $responseElement="#diag-class", $type="plain", $size=";scale:100") {
154  $params=JsUtils::_implodeParams([ "$('#frmProperties').serialize()",$params ]);
155  $menu=new HtmlMenu("menu-diagram");
156  $ddScruffy=new HtmlDropdown("ddScruffy", $type, [ "nofunky" => "Boring","plain" => "Plain","scruffy" => "Scruffy" ], true);
157  $ddScruffy->setValue("plain")->asSelect("type");
158  $this->jquery->postOn("change", "#type", $this->_getAdminFiles()->getAdminBaseRoute() . $url, $params, $responseElement, [ "ajaxTransition" => "random","attr" => "" ]);
159  $menu->addItem($ddScruffy);
160  $ddSize=new HtmlDropdown("ddSize", $size, [ ";scale:180" => "Huge",";scale:120" => "Big",";scale:100" => "Normal",";scale:80" => "Small",";scale:60" => "Tiny" ], true);
161  $ddSize->asSelect("size");
162  $this->jquery->postOn("change", "#size", $this->_getAdminFiles()->getAdminBaseRoute() . $url, $params, $responseElement, [ "ajaxTransition" => "random","attr" => "" ]);
163  $menu->wrap("<form id='frmProperties' name='frmProperties'>", "</form>");
164  $menu->addItem($ddSize);
165  return $menu;
166  }
167 
168  protected function displayModelsMessages($type, $messagesToDisplay) {
169  $step=$this->getActiveModelStep();
170  return $this->displayMessages($type, $messagesToDisplay, $step[2], $step[0]);
171  }
172 }
static isPost()
Returns true if the request is sent by the POST method.
Definition: URequest.php:91
_loadModelStep($engineering=null, $newStep=null)
_yumlMenu($url="/_updateDiagram", $params="{}", $responseElement="#diag-class", $type="plain", $size=";scale:100")
displayMessages($type, $messagesToDisplay, $header="", $icon="")
Definition: CheckTrait.php:295
_changeEngineering($oldEngineering, $completed=null)