Go to the documentation of this file.00001 <?php
00010 class form_checkbox_fields extends form_checkbox {
00011
00017 protected $form;
00018
00024 protected $replaceName;
00025
00031 protected $valuesForValid = array();
00032
00033 protected function afterInit() {
00034 parent::afterInit();
00035
00036 $this->form = factory::get('form_db', array_merge($this->cfg->formOpts, array(
00037 'table'=>$this->cfg->table,
00038 )));
00039 $this->replaceName = str_replace('[]', '', $this->name).'_fields['.$this->cfg->replaceKey.'][[name]]';
00040 foreach($this->cfg->fields as $f) {
00041 $f['name'] = str_replace('[name]', $f['name'], $this->replaceName);
00042 $this->form->addFromField($f);
00043 }
00044
00045 $this->prepareValuesForValid();
00046 $this->valid->getCfg()->setRef('value', $this->valuesForValid);
00047 }
00048
00052 protected function prepareValuesForValid() {
00053 $this->valuesForValid = array();
00054 $value = $this->getValue();
00055 if (is_array($value)) {
00056 foreach($value as $v) {
00057 $this->valuesForValid[] = $v[db::getCfg('relatedValue')];
00058 }
00059 }
00060 }
00061
00062 public function setValue($value, $refill=false) {
00063 if ($refill) {
00064 $vals = http_vars::getInstance()->post($this->name.'_fields');
00065 $tmpVal = $value;
00066 $value = array();
00067 if (is_array($tmpVal)) {
00068 foreach($tmpVal as $v) {
00069 $curVal = array(
00070 db::getCfg('relatedValue')=>$v
00071 );
00072 foreach($this->cfg->fields as $f) {
00073 $curVal[$f['name']] = isset($vals[$v]) && isset($vals[$v][$f['name']]) ? $vals[$v][$f['name']] : null;
00074 }
00075 $value[] = $curVal;
00076 }
00077 }
00078 }
00079 parent::setValue($value);
00080 $this->prepareValuesForValid();
00081 }
00082
00083 public function to($type) {
00084 if ($type == 'html' && $this->cfg->mode == 'edit') {
00085 response::getInstance()->addJs('checkboxFields');
00086 }
00087 return parent::to($type);
00088 }
00089
00090 protected function updateLine($type, $val, $line) {
00091 if ($this->cfg->mode == 'view') {
00092 $tmp = array();
00093 if (is_array($this->cfg->value))
00094 foreach($this->cfg->value as $v) {
00095 if ($v[db::getCfg('relatedValue')] == $val) {
00096 foreach($v as $kk=>$vv) {
00097 if ($kk != db::getCfg('relatedValue') && $vv) {
00098 $tmp[] = $this->form->get(str_replace('[name]', $kk, $this->replaceName))->label.$this->cfg->sepLabelViewSubValues.$vv;
00099 }
00100 }
00101 }
00102 }
00103 if (count($tmp))
00104 $line.= ' ('.implode($this->cfg->sepViewSubValues, $tmp).')';
00105 return $line;
00106 }
00107
00108 $form = clone $this->form;
00109
00110 if (is_array($this->cfg->value))
00111 foreach($this->cfg->value as $v) {
00112 if ($v[db::getCfg('relatedValue')] == $val) {
00113 foreach($v as $k=>$vv) {
00114 if ($k != db::getCfg('relatedValue')) {
00115 $form->setValue(str_replace('[name]', $k, $this->replaceName), $vv);
00116 }
00117 }
00118 }
00119 }
00120 foreach($this->cfg->fields as $f) {
00121 $name = str_replace('[name]', $f['name'], $this->replaceName);
00122 $form->get($name)->getCfg()->name = str_replace($this->cfg->replaceKey, $val, $name);
00123 $form->get($name)->renew();
00124 }
00125
00126 return str_replace('[fields]', $form->__toString(), $line);
00127 }
00128
00129 public function isInValue($val) {
00130 if (is_array($this->cfg->value))
00131 foreach($this->cfg->value as $v) {
00132 if ($v[db::getCfg('relatedValue')] == $val)
00133 return true;
00134 }
00135 return false;
00136 }
00137 }