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