Skip to content

Commit 138cdf0

Browse files
committed
First business widget : FormLogin
1 parent abc2469 commit 138cdf0

File tree

12 files changed

+267
-14
lines changed

12 files changed

+267
-14
lines changed

Ajax/common/Widget.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,14 @@ protected function _init($instanceViewer,$contentKey,$content,$edition){
7272
$this->_edition=$edition;
7373
}
7474

75+
/**
76+
* @param int|string $fieldName
77+
* @return int|string
78+
*/
79+
protected function _getIndex($fieldName){
80+
return $fieldName;
81+
}
82+
7583
protected function _getFieldIdentifier($prefix,$name=""){
7684
return $this->identifier."-{$prefix}-".$this->_instanceViewer->getIdentifier();
7785
}
@@ -114,6 +122,19 @@ public function setAttached($value=true){
114122
return $this->getHtmlComponent()->setAttached($value);
115123
}
116124

125+
/**
126+
* Associates a $callback function after the compilation of the field at $index position
127+
* The $callback function can take the following arguments : $field=>the compiled field, $instance : the active instance of the object, $index: the field position
128+
* @param int $index postion of the compiled field
129+
* @param callable $callback function called after the field compilation
130+
* @return Widget
131+
*/
132+
public function afterCompile($index,$callback){
133+
$index=$this->_getIndex($index);
134+
$this->_instanceViewer->afterCompile($index, $callback);
135+
return $this;
136+
}
137+
117138
public function setColor($color){
118139
return $this->getHtmlComponent()->setColor($color);
119140
}
@@ -124,6 +145,11 @@ public function setCaptions($captions){
124145
return $this;
125146
}
126147

148+
public function setCaption($index,$caption){
149+
$this->_instanceViewer->setCaption($this->_getIndex($index), $caption);
150+
return $this;
151+
}
152+
127153
public function setFields($fields){
128154
$this->_instanceViewer->setVisibleProperties($fields);
129155
return $this;
@@ -145,16 +171,19 @@ public function addErrorMessage(){
145171
}
146172

147173
public function insertField($index,$field){
174+
$index=$this->_getIndex($index);
148175
$this->_instanceViewer->insertField($index, $field);
149176
return $this;
150177
}
151178

152179
public function insertInField($index,$field){
180+
$index=$this->_getIndex($index);
153181
$this->_instanceViewer->insertInField($index, $field);
154182
return $this;
155183
}
156184

157185
public function setValueFunction($index,$callback){
186+
$index=$this->_getIndex($index);
158187
$this->_instanceViewer->setValueFunction($index, $callback);
159188
return $this;
160189
}
@@ -380,4 +409,20 @@ public function setValidationParams(array $_validationParams) {
380409
$this->getForm()->setValidationParams($_validationParams);
381410
return $this;
382411
}
412+
413+
public function moveFieldTo($from,$to){
414+
return $this->_instanceViewer->moveFieldTo($from, $to);
415+
}
416+
417+
public function swapFields($index1,$index2){
418+
$index1=$this->_getIndex($index1);
419+
$index2=$this->_getIndex($index2);
420+
return $this->_instanceViewer->swapFields($index1, $index2);
421+
}
422+
423+
public function removeField($index){
424+
$index=$this->_getIndex($index);
425+
$this->_instanceViewer->removeField($index);
426+
return $this;
427+
}
383428
}

Ajax/semantic/traits/SemanticWidgetsTrait.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use Ajax\semantic\widgets\datatable\DataTable;
55
use Ajax\semantic\widgets\dataelement\DataElement;
66
use Ajax\semantic\widgets\dataform\DataForm;
7+
use Ajax\semantic\widgets\business\user\FormLogin;
78

89
trait SemanticWidgetsTrait {
910

@@ -36,4 +37,8 @@ public function dataElement($identifier, $instance){
3637
public function dataForm($identifier, $instance){
3738
return $this->addHtmlComponent(new DataForm($identifier,$instance));
3839
}
40+
41+
public function formLogin($identifier){
42+
return $this->addHtmlComponent(new FormLogin($identifier));
43+
}
3944
}

Ajax/semantic/widgets/base/FieldAsTrait.php

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Ajax\semantic\html\collections\HtmlMessage;
1717
use Ajax\semantic\html\elements\HtmlButton;
1818
use Ajax\service\JArray;
19+
use Ajax\semantic\html\elements\html5\HtmlLink;
1920

2021
/**
2122
* trait used in Widget
@@ -55,6 +56,7 @@ private function _getLabelField($caption,$icon=NULL){
5556
return $label;
5657
}
5758

59+
5860
protected function _addRules($element,&$attributes){
5961
if(isset($attributes["rules"])){
6062
$rules=$attributes["rules"];
@@ -74,8 +76,8 @@ protected function _prepareFormFields(&$field,$name,&$attributes){
7476
return $field;
7577
}
7678

77-
protected function _fieldAs($elementCallback,$index,$attributes=NULL,$prefix=null){
78-
$this->setValueFunction($index,function($value) use ($index,&$attributes,$elementCallback,$prefix){
79+
protected function _fieldAs($elementCallback,&$index,$attributes=NULL,$prefix=null){
80+
$this->setValueFunction($index,function($value,$instance,$index) use (&$attributes,$elementCallback,$prefix){
7981
$caption=$this->_getFieldCaption($index);
8082
$name=$this->_getFieldName($index);
8183
$id=$this->_getFieldIdentifier($prefix,$name);
@@ -199,23 +201,32 @@ public function fieldAsDropDown($index,$elements=[],$multiple=false,$attributes=
199201

200202
public function fieldAsMessage($index,$attributes=NULL){
201203
return $this->_fieldAs(function($id,$name,$value,$caption){
202-
$mess= new HtmlMessage("message-".$id,$value);
203-
$mess->addHeader($caption);
204+
$mess= new HtmlMessage("message-".$id,$caption);
205+
$mess->addHeader($value);
204206
return $mess;
205207
}, $index,$attributes,"message");
206208
}
207209

210+
public function fieldAsLink($index,$attributes=NULL){
211+
return $this->_fieldAs(function($id,$name,$value,$caption){
212+
$lnk= new HtmlLink("message-".$id,"#",$caption);
213+
return $lnk;
214+
}, $index,$attributes,"link");
215+
}
216+
208217
/**Change fields type
209-
* @param array $types an array or associative array $type=>$attribute
218+
* @param array $types an array or associative array $type=>$attributes
210219
*/
211220
public function fieldsAs(array $types){
212221
$i=0;
213222
if(JArray::isAssociative($types)){
214223
foreach ($types as $type=>$attributes){
215224
if(\is_int($type))
216225
$this->fieldAs($i++,$attributes,[]);
217-
else
218-
$this->fieldAs($i++,$type,$attributes);
226+
else{
227+
$type=preg_replace('/\d/', '', $type );
228+
$this->fieldAs($i++,$type,[$attributes]);
229+
}
219230
}
220231
}else{
221232
foreach ($types as $type){

Ajax/semantic/widgets/base/InstanceViewer.php

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22
namespace Ajax\semantic\widgets\base;
33
use Ajax\service\JString;
4+
use Ajax\service\JArray;
45

56
class InstanceViewer {
67
protected $widgetIdentifier;
@@ -28,6 +29,27 @@ public function __construct($identifier,$instance=NULL,$captions=NULL){
2829
$this->defaultValueFunction=function($name,$value){return $value;};
2930
}
3031

32+
public function moveFieldTo($from,$to){
33+
if(JArray::moveElementTo($this->visibleProperties, $from, $to)){
34+
return JArray::moveElementTo($this->values, $from, $to);
35+
}
36+
return false;
37+
}
38+
39+
public function swapFields($index1,$index2){
40+
if(JArray::swapElements($this->visibleProperties, $index1, $index2)){
41+
return JArray::swapElements($this->values, $index1, $index2);
42+
}
43+
return false;
44+
}
45+
46+
public function removeField($index){
47+
\array_splice($this->visibleProperties,$index,1);
48+
\array_splice($this->values,$index,1);
49+
\array_splice($this->captions,$index,1);
50+
return $this;
51+
}
52+
3153
public function getValues(){
3254
$values=[];
3355
$index=0;
@@ -240,7 +262,7 @@ public function getCaption($index){
240262

241263
public function getCaptions(){
242264
if(isset($this->captions)){
243-
$captions= $this->captions;
265+
$captions= \array_values($this->captions);
244266
for($i=\sizeof($captions);$i<$this->count();$i++){
245267
$captions[]="";
246268
}
@@ -303,6 +325,4 @@ public function setDefaultValueFunction($defaultValueFunction) {
303325
$this->defaultValueFunction=$defaultValueFunction;
304326
return $this;
305327
}
306-
307-
308328
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace Ajax\semantic\widgets\business;
4+
5+
use Ajax\semantic\widgets\dataform\DataForm;
6+
use Ajax\JsUtils;
7+
8+
/**
9+
* @author jc
10+
*/
11+
abstract class BusinessForm extends DataForm {
12+
protected $_fieldsOrder;
13+
protected $_fieldsDefinition;
14+
/**
15+
*
16+
* {@inheritdoc}
17+
*
18+
* @see \Ajax\semantic\widgets\dataform\DataForm::__construct()
19+
*/
20+
public function __construct($identifier,$modelInstance=null,$fields=[],$captions=[],$separators=[]) {
21+
parent::__construct($identifier,$modelInstance);
22+
$this->setFields($fields);
23+
$this->setSeparators($separators);
24+
$this->fieldsAs($this->_fieldsDefinition);
25+
$this->setCaptions($captions);
26+
}
27+
28+
protected function _getIndex($fieldName){
29+
$index=$fieldName;
30+
if(\is_string($fieldName)){
31+
$index=\array_search($fieldName, $this->_fieldsOrder);
32+
}
33+
return $index;
34+
}
35+
protected function _fieldAs($elementCallback,&$index,$attributes=NULL,$prefix=null){
36+
$index=$this->_getIndex($index);
37+
return parent::_fieldAs($elementCallback, $index,$attributes,$prefix);
38+
}
39+
40+
41+
public function removeField($fieldName){
42+
parent::removeField($fieldName);
43+
\array_splice($this->_fieldsOrder,$this->_getIndex($fieldName),1);
44+
return $this;
45+
}
46+
47+
public function compile(JsUtils $js=NULL,&$view=NULL){
48+
return parent::compile($js,$view);
49+
}
50+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Ajax\semantic\widgets\business\user;
4+
5+
use Ajax\semantic\widgets\business\BusinessForm;
6+
7+
/**
8+
* @author jc
9+
*/
10+
class FormLogin extends BusinessForm {
11+
12+
/**
13+
* @param string $identifier
14+
* @param object $modelInstance
15+
*/
16+
public function __construct($identifier,$modelInstance=null) {
17+
if(!isset($modelInstance))
18+
$modelInstance=new UserModel();
19+
$this->_fieldsOrder=["message","login","password","remember","forget","submit"];
20+
$this->_fieldsDefinition=["message"=>["icon"=>"sign in"],"input0"=>["rules"=>"empty"],"input1"=>["inputType"=>"password","rules"=>"empty"],"checkbox","link","submit"=>"green fluid"];
21+
parent::__construct($identifier,$modelInstance,["Connection","login","password","remember","forget","submit"],
22+
["Please enter login and password to connect","Login","Password","Remember me.","Forgot your password?","Connection"],
23+
[0,2,4,5]);
24+
}
25+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Ajax\semantic\widgets\business\user;
4+
5+
class UserModel {
6+
protected $login="abc";
7+
protected $password="0000";
8+
9+
public function getLogin() {
10+
return $this->login;
11+
}
12+
13+
public function setLogin($login) {
14+
$this->login=$login;
15+
return $this;
16+
}
17+
18+
public function getPassword() {
19+
return $this->password;
20+
}
21+
22+
public function setPassword($password) {
23+
$this->password=$password;
24+
return $this;
25+
}
26+
27+
}

Ajax/semantic/widgets/dataelement/DataElement.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace Ajax\semantic\widgets\dataelement;
44

55
use Ajax\common\Widget;
6-
use Ajax\semantic\widgets\base\InstanceViewer;
76
use Ajax\semantic\widgets\datatable\PositionInTable;
87
use Ajax\JsUtils;
98
use Ajax\service\JArray;
@@ -20,7 +19,7 @@ class DataElement extends Widget {
2019

2120
public function __construct($identifier, $modelInstance=NULL) {
2221
parent::__construct($identifier, null,$modelInstance);
23-
$this->_init(new InstanceViewer($identifier), "table", new HtmlTable($identifier, 0,2), false);
22+
$this->_init(new DeInstanceViewer($identifier), "table", new HtmlTable($identifier, 0,2), false);
2423
$this->content["table"]->setDefinition();
2524
}
2625

@@ -45,8 +44,8 @@ public function compile(JsUtils $js=NULL,&$view=NULL){
4544
* @param HtmlTable $table
4645
*/
4746
protected function _generateContent($table){
48-
$captions=$this->_instanceViewer->getCaptions();
4947
$values= $this->_instanceViewer->getValues();
48+
$captions=$this->_instanceViewer->getCaptions();
5049
$count=$this->_instanceViewer->count();
5150
for($i=0;$i<$count;$i++){
5251
$table->addRow([$captions[$i],$values[$i]]);
@@ -61,6 +60,10 @@ protected function _getFieldCaption($index){
6160
return null;
6261
}
6362

63+
protected function _getFieldIdentifier($prefix,$name=""){
64+
return $this->identifier."-{$prefix}-".$name;
65+
}
66+
6467
/**
6568
* {@inheritDoc}
6669
* @see \Ajax\common\Widget::getHtmlComponent()
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Ajax\semantic\widgets\dataelement;
4+
5+
use Ajax\semantic\widgets\base\InstanceViewer;
6+
use Ajax\semantic\html\collections\form\HtmlFormField;
7+
use Ajax\semantic\html\base\HtmlSemDoubleElement;
8+
9+
class DeInstanceViewer extends InstanceViewer {
10+
11+
public function __construct($identifier, $instance=NULL, $captions=NULL) {
12+
parent::__construct($identifier, $instance, $captions);
13+
}
14+
15+
public function getValue($index){
16+
$result=parent::getValue($index);
17+
if($result instanceof HtmlFormField){
18+
$lbl=new HtmlSemDoubleElement("lbl-".$this->widgetIdentifier."-".$index,"label","",$this->getCaption($index));
19+
$lbl->setProperty("for", $result->getDataField()->getIdentifier());
20+
$this->captions[$index]=$lbl;
21+
}
22+
return $result;
23+
}
24+
}

0 commit comments

Comments
 (0)