00001 <?php
00010 abstract class form_mulValue extends form_abstract {
00011
00015 protected function afterInit() {
00016 parent::afterInit();
00017
00018 if (!$this->cfg->uniqValue)
00019 $this->cfg->name.= '[]';
00020
00021 $dbList = $this->cfg->dbList;
00022 if (is_array($dbList) && $dbList['table']) {
00023 $list = $this->cfg->list;
00024 if (!$list)
00025 $list = array();
00026
00027 $group = $this->cfg->group;
00028 if (!$group)
00029 $group = array();
00030
00031 $db = db::getInstance();
00032
00033 $values = $db->select(array_merge($dbList, array('result'=>MYSQL_NUM)));
00034
00035 $tmp = null;
00036 foreach($values as $v) {
00037 $key = array_shift($v);
00038 if ($dbList['nbFieldGr'] > 0) {
00039 $arr = utils::cutArray($v, $dbList['nbFieldGr']);
00040 $tmp2 = implode($dbList['sepGr'], $arr[0]);
00041 if ($tmp != $tmp2) {
00042 $group[$key] = $tmp2;
00043 $tmp = $tmp2;
00044 }
00045 $v = $arr[1];
00046 }
00047 if (empty($v))
00048 $v = array($key);
00049 $list[$key] = implode($dbList['sep'], $v);
00050 }
00051 $this->cfg->group = utils::htmlOut($group);
00052 $this->cfg->list = utils::htmlOut($list);
00053 } else if ($this->cfg->needOut)
00054 $this->cfg->list = utils::htmlOut($this->cfg->list, true);
00055
00056 if (is_array($this->cfg->list))
00057 $this->addRule('in', array_keys($this->cfg->list));
00058 }
00059
00066 public function getValue($outside = true) {
00067 $val = parent::getValue();
00068 if (!is_null($this->cfg->valueNone) && $val == $this->cfg->valueNone)
00069 return null;
00070 if ($this->cfg->needOut) {
00071 if ($outside)
00072 $val = utils::htmlDeOut($val, true);
00073 else
00074 $val = utils::htmlOut($val, true);
00075 }
00076 return $val;
00077 }
00078
00079 public function setValue($value, $refill = false) {
00080 if (is_array($value) && $this->cfg->uniqValue) {
00081 parent::setValue(array_shift($value));
00082 } else {
00083 parent::setValue($value);
00084 }
00085 }
00086
00087 public function to($type) {
00088 if ($this->cfg->mode == 'view') {
00089 if ($this->cfg->uniqValue)
00090 return $this->cfg->getInArray('list', $this->getValue(false));
00091 else {
00092 $tmp = array();
00093 if (is_array($this->cfg->list)) {
00094 foreach($this->cfg->list as $k=>$v) {
00095 if ($this->isInValue($k)) {
00096 $tmp[] = $this->updateLine($type, $k, $v);
00097 }
00098 }
00099 }
00100
00101
00102
00103
00104 return implode(', ', $tmp);
00105 }
00106 }
00107
00108 $prm = $this->cfg->get($type);
00109 $inline = $this->cfg->inline? 'Inline' : null;
00110 $ret = null;
00111
00112 $tmp = $prm;
00113 unset($tmp['plus'], $tmp['global'], $tmp['globalInline'], $tmp['value'], $tmp['selected'], $tmp['group']);
00114 $prm['plus'].= ' ';
00115 foreach($tmp as $k=>$v)
00116 $prm['plus'].= $k.'="'.$v.'" ';
00117
00118 $tmpGr = null;
00119 $tmpVal = null;
00120 if (is_array($this->cfg->list)) {
00121 foreach($this->cfg->list as $k=>$v) {
00122 if (is_array($this->cfg->group) && array_key_exists($k, $this->cfg->group) && $tmpGr != $k) {
00123 $tmpGr = $k;
00124 $ret.= str_replace(
00125 array('[label]', '[group]'),
00126 array($this->cfg->group[$k], $tmpVal),
00127 $prm['group']);
00128 $tmpVal = null;
00129 }
00130 $selected = $this->isInValue($k)? $prm['selected'] : null;
00131
00132 $tmpVal.= $this->updateLine($type, $k, str_replace(
00133 array('[plus]', '[value]', '[label]'),
00134 array($selected, $k, $v),
00135 $prm['value']));
00136 }
00137 }
00138
00139 if (!empty($tmpGr)) {
00140 $ret.= str_replace(
00141 array('[label]', '[group]'),
00142 array($this->cfg->group[$k], $tmpVal),
00143 $prm['group']);
00144 } else
00145 $ret.= $tmpVal;
00146
00147 $this->id = (!$this->cfg->uniqValue)? substr($this->cfg->name, 0, -2) : $this->cfg->name;
00148 return str_replace(
00149 array('[values]', '[plus]', '[name]', '[id]'),
00150 array($ret, $prm['plus'], $this->cfg->name, $this->id),
00151 $prm['global'.$inline]);
00152 }
00153
00162 protected function updateLine($type, $val, $line) {
00163 return $line;
00164 }
00165
00166 public function toHtml() {
00167 return $this->to('html');
00168 }
00169
00170 public function toXul() {
00171 return $this->to('xul');
00172 }
00173
00180 public function isInValue($val) {
00181 if (is_array($this->cfg->value))
00182 return (in_array($val, $this->cfg->value));
00183 else
00184 return ($val == $this->cfg->value);
00185 }
00186
00187 }