phpMv  -UI toolkit 2.4.12
jQuery, jQuery UI, Twitter Bootstrap and Semantic-UI library for php & php MVC Frameworks
DataElement.php
Go to the documentation of this file.
1 <?php
2 
4 
7 use Ajax\JsUtils;
12 
20 class DataElement extends Widget {
21  use BaseTrait;
22  protected $_colWidths;
23 
24  public function __construct($identifier, $modelInstance=NULL) {
25  parent::__construct($identifier, null,$modelInstance);
26  $this->_init(new DeInstanceViewer($identifier), "table", new HtmlTable($identifier, 0,2), false);
27  $this->content["table"]->setDefinition()->addClass("_element");
28  }
29 
30  public function compile(JsUtils $js=NULL,&$view=NULL){
31  if(!$this->_generated){
32  $this->_instanceViewer->setInstance($this->_modelInstance);
33 
34  $table=$this->content["table"];
35  $this->_generateContent($table);
36 
37  if(isset($this->_toolbar)){
38  $this->_setToolbarPosition($table);
39  }
40  if(isset($this->_colWidths)){
41  $this->_applyStyleAttributes($table);
42  }
44  $this->_compileForm();
45  $this->_generated=true;
46  }
47  return parent::compile($js,$view);
48  }
49 
53  protected function _generateContent($table){
54  $values= $this->_instanceViewer->getValues();
55  $captions=$this->_instanceViewer->getCaptions();
56  $fields=$this->_instanceViewer->getVisibleProperties();
57  $count=$this->_instanceViewer->count();
58  $this->setProperty("data-ajax", $this->_instanceViewer->getIdentifier());
59  for($i=0;$i<$count;$i++){
60  $row=$table->addRow([$captions[$i],$values[$i]]);
61  $row->getItem(1)->setProperty("data-field", $fields[$i]);
62  }
63  }
64 
65  public function getFieldValue($index){
66  if(is_string($index)){
67  $fields=$this->_instanceViewer->getVisibleProperties();
68  $index=array_search($index, $fields);
69  }
70  if(is_numeric($index)){
71  $values= $this->_instanceViewer->getValues();
72  if(isset($values[$index])){
73  return $values[$index];
74  }
75  }
76  return null;
77  }
78 
79  protected function _applyStyleAttributes(HtmlTable $table){
80  $table->setColWidths($this->_colWidths);
81  }
82  protected function _getFieldName($index){
83  return $this->_instanceViewer->getFieldName($index);
84  }
85 
86  protected function _getFieldCaption($index){
87  return null;
88  }
89 
90  protected function _getFieldIdentifier($prefix,$name=""){
91  return $this->identifier."-{$prefix}-".$name;
92  }
93 
99  public function getHtmlComponent() {
100  return $this->content["table"];
101  }
102 
107  protected function _setToolbarPosition($table, $captions=NULL) {
109  }
110 
117  public function onNewRow($callback) {
118  $this->content["table"]->onNewRow($callback);
119  return $this;
120  }
121 
122  public function asForm(){
123  return $this->getForm();
124  }
125 
126  public function setColCaptionWidth($width){
127  $this->_colWidths[0]=$width;
128  return $this;
129  }
130 
131  public function setColValueWidth($width) {
132  $this->_colWidths[1]=$width;
133  return $this;
134  }
135 
136  public function setColWidths($widths){
137  $this->_colWidths=$widths;
138  return $this;
139  }
140 
141  public function run(JsUtils $js){
142  if(JString::isNotNull($this->_identifier))
143  $js->execOn("click", "#".$this->_identifier." .ui.toggle", 'var active=$(this).hasClass("active");$(this).children("i").toggleClass("up",active).toggleClass("down",!active);var nextTd=$(this).closest("td").next("td");nextTd.children(":not(.toggle-caption)").toggle(active);nextTd.children(".toggle-caption").toggle(!active);$(this).trigger({type:"toggled",active: active,caption: nextTd.children(".toggle-caption")});');
144  parent::run($js);
145  }
146 }
_setToolbarPosition($table, $captions=NULL)
{}
static isNotNull($s)
Definition: JString.php:22
__construct($identifier, $modelInstance=NULL)
Definition: DataElement.php:24
onNewRow($callback)
The callback function called after the insertion of each row when fromDatabaseObjects is called callb...
_init($instanceViewer, $contentKey, $content, $edition)
Definition: Widget.php:82
compile(JsUtils $js=NULL, &$view=NULL)
Definition: DataElement.php:30
Semantic HTML Table component.
Definition: HtmlTable.php:21
DataElement widget for displaying an instance of model.
Definition: DataElement.php:20
JQuery PHP library.
Definition: JsUtils.php:23
static sortAssociative($array, $sortedKeys=array())
Definition: JArray.php:66