nyroFwk  0.2
mulValue.class.php
Go to the documentation of this file.
1 <?php
10 abstract class form_mulValue extends form_abstract {
11 
15  protected function afterInit() {
16  parent::afterInit();
17 
18  if (!$this->cfg->uniqValue)
19  $this->cfg->name.= '[]';
20 
21  $dbList = $this->cfg->dbList;
22  if (is_array($dbList) && $dbList['table']) {
23  $list = $this->cfg->list;
24  if (!$list)
25  $list = array();
26 
27  $group = $this->cfg->group;
28  if (!$group)
29  $group = array();
30 
31  $db = db::getInstance();
32 
33  $values = $db->select(array_merge($dbList, array('result'=>PDO::FETCH_ASSOC)));
34 
35  $tmp = null;
36  foreach($values as $v) {
37  $key = array_shift($v);
38  if ($dbList['nbFieldGr'] > 0) {
39  $arr = utils::cutArray($v, $dbList['nbFieldGr']);
40  $tmp2 = implode($dbList['sepGr'], $arr[0]);
41  if ($tmp != $tmp2) {
42  $group[$key] = $tmp2;
43  $tmp = $tmp2;
44  }
45  $v = $arr[1];
46  }
47  if (empty($v))
48  $v = array($key);
49  $list[$key] = implode($dbList['sep'], $v);
50  }
51 
52  $this->cfg->group = utils::htmlOut($group);
53  $this->cfg->list = utils::htmlOut($list);
54  } else if ($this->cfg->needOut)
55  $this->cfg->list = utils::htmlOut($this->cfg->list, true);
56 
57  if (is_array($this->cfg->list))
58  $this->addRule('in', array_keys($this->cfg->list));
59  }
60 
67  public function getValue($outside = true) {
68  $val = parent::getValue();
69  if (!is_null($this->cfg->valueNone) && $val == $this->cfg->valueNone)
70  return null;
71  if ($this->cfg->needOut) {
72  if ($outside)
73  $val = utils::htmlDeOut($val, true);
74  else
75  $val = utils::htmlOut($val, true);
76  }
77  return $val;
78  }
79 
80  public function setValue($value, $refill = false) {
81  if (is_array($value) && $this->cfg->uniqValue) {
82  parent::setValue(array_shift($value));
83  } else {
84  parent::setValue($value);
85  }
86  }
87 
88  public function to($type) {
89  if ($this->cfg->mode == 'view') {
90  if ($this->cfg->uniqValue)
91  return $this->cfg->getInArray('list', $this->getValue(false));
92  else {
93  $tmp = array();
94  if (is_array($this->cfg->list)) {
95  foreach($this->cfg->list as $k=>$v) {
96  if ($this->isInValue($k)) {
97  $tmp[] = $this->updateLine($type, $k, $v);
98  }
99  }
100  }
101  /*
102  foreach($this->getValue(false) as $v)
103  $tmp[] = $this->cfg->getInArray('list', $v);
104  */
105  return implode(', ', $tmp);
106  }
107  }
108 
109  $prm = $this->cfg->get($type);
110  $inline = $this->cfg->inline ? 'Inline' : null;
111  $ret = null;
112 
113  $tmp = $prm;
114  unset($tmp['plus'], $tmp['global'], $tmp['globalInline'], $tmp['value'], $tmp['selected'], $tmp['disabled'], $tmp['group'], $tmp['name'], $tmp['des']);
115  $prm['plus'].= ' ';
116  foreach($tmp as $k=>$v)
117  $prm['plus'].= $k.'="'.$v.'" ';
118 
119  $tmpGr = null;
120  $tmpVal = null;
121  $descriptions = $this->cfg->descriptions;
122  if ($this->cfg->outDescriptions)
123  $descriptions = utils::htmlOut($descriptions);
124  if (is_array($this->cfg->list)) {
125  foreach($this->cfg->list as $k=>$v) {
126  if (is_array($this->cfg->group) && array_key_exists($k, $this->cfg->group) && $tmpGr != $k) {
127  $tmpGr = $k;
128  $ret.= str_replace(
129  array('[label]', '[group]'),
130  array($this->cfg->group[$k], $tmpVal),
131  $prm['group']);
132  $tmpVal = null;
133  }
134  $plus = $this->isInValue($k) ? $prm['selected'] : null;
135  $plus.= $this->isDisabled($k) ? $prm['disabled'] : null;
136 
137  $tmpVal.= $this->updateLine($type, $k, str_replace(
138  array('[plus]', '[value]', '[label]', '[des]'),
139  array($plus, $k, $v, isset($descriptions[$k]) ? $descriptions[$k] : null),
140  isset($prm['value'.$inline]) ? $prm['value'.$inline] : $prm['value']));
141  }
142  }
143 
144  if (!empty($tmpGr)) {
145  $ret.= str_replace(
146  array('[label]', '[group]'),
147  array($this->cfg->group[$k], $tmpVal),
148  $prm['group']);
149  } else
150  $ret.= $tmpVal;
151 
152  $this->id = (!$this->cfg->uniqValue)? substr($this->cfg->name, 0, -2) : $this->cfg->name;
153  return str_replace(
154  array('[values]', '[plus]', '[name]', '[id]'),
155  array($ret, $prm['plus'], $this->cfg->name, $this->id),
156  $prm['global'.$inline]);
157  }
158 
167  protected function updateLine($type, $val, $line) {
168  return $line;
169  }
170 
171  public function toHtml() {
172  return $this->to('html');
173  }
174 
175  public function toXul() {
176  return $this->to('xul');
177  }
178 
185  public function isInValue($val) {
186  if (is_array($this->cfg->value))
187  return (in_array($val, $this->cfg->value));
188  else
189  return ($val == $this->cfg->value);
190  }
191 
198  public function isDisabled($val) {
199  return in_array($val, $this->cfg->disabled);
200  }
201 
202 }
static htmlOut($val, $key=false)
static htmlDeOut($val, $key=false)
addRule($type, $prm=null)
updateLine($type, $val, $line)
static cutArray(array $arr, $nb, $presKey=true)
Definition: utils.class.php:20
setValue($value, $refill=false)
getValue($outside=true)
static getInstance($cfg=null)
Definition: db.class.php:58
Generated on Sun Oct 15 2017 22:25:20 for nyroFwk by doxygen 1.8.13