nyroFwk  0.2
fields.class.php
Go to the documentation of this file.
1 <?php
11 
17  protected $form;
18 
24  protected $replaceName;
25 
31  protected $valuesForValid = array();
32 
33  protected function afterInit() {
34  parent::afterInit();
35 
36  $this->form = factory::get('form_db', array_merge($this->cfg->formOpts, array(
37  'table'=>$this->cfg->table,
38  )));
39  $this->replaceName = str_replace('[]', '', $this->name).'_fields['.$this->cfg->replaceKey.'][[name]]';
40  foreach($this->cfg->fields as $f) {
41  $f['name'] = str_replace('[name]', $f['name'], $this->replaceName);
42  $this->form->addFromField($f);
43  }
44 
45  $this->prepareValuesForValid();
46  $this->valid->getCfg()->setRef('value', $this->valuesForValid);
47  }
48 
52  protected function prepareValuesForValid() {
53  $this->valuesForValid = array();
54  $value = $this->getValue();
55  if (is_array($value)) {
56  foreach($value as $v) {
57  $this->valuesForValid[] = $v[db::getCfg('relatedValue')];
58  }
59  }
60  }
61 
62  public function setValue($value, $refill=false) {
63  if ($refill) {
64  $vals = http_vars::getInstance()->post($this->name.'_fields');
65  $tmpVal = $value;
66  $value = array();
67  if (is_array($tmpVal)) {
68  foreach($tmpVal as $v) {
69  $curVal = array(
70  db::getCfg('relatedValue')=>$v
71  );
72  foreach($this->cfg->fields as $f) {
73  $curVal[$f['name']] = isset($vals[$v]) && isset($vals[$v][$f['name']]) ? $vals[$v][$f['name']] : null;
74  }
75  $value[] = $curVal;
76  }
77  }
78  }
79  parent::setValue($value);
80  $this->prepareValuesForValid();
81  }
82 
83  public function to($type) {
84  if ($type == 'html' && $this->cfg->mode == 'edit') {
85  response::getInstance()->addJs('checkboxFields');
86  }
87  return parent::to($type);
88  }
89 
90  protected function updateLine($type, $val, $line) {
91  if ($this->cfg->mode == 'view') {
92  $tmp = array();
93  if (is_array($this->cfg->value))
94  foreach($this->cfg->value as $v) {
95  if ($v[db::getCfg('relatedValue')] == $val) {
96  foreach($v as $kk=>$vv) {
97  if ($kk != db::getCfg('relatedValue') && $vv) {
98  $tmp[] = $this->form->get(str_replace('[name]', $kk, $this->replaceName))->label.$this->cfg->sepLabelViewSubValues.$vv;
99  }
100  }
101  }
102  }
103  if (count($tmp))
104  $line.= ' ('.implode($this->cfg->sepViewSubValues, $tmp).')';
105  return $line;
106  }
107 
108  $form = clone $this->form;
109 
110  if (is_array($this->cfg->value))
111  foreach($this->cfg->value as $v) {
112  if ($v[db::getCfg('relatedValue')] == $val) {
113  foreach($v as $k=>$vv) {
114  if ($k != db::getCfg('relatedValue')) {
115  $form->setValue(str_replace('[name]', $k, $this->replaceName), $vv);
116  }
117  }
118  }
119  }
120  foreach($this->cfg->fields as $f) {
121  $name = str_replace('[name]', $f['name'], $this->replaceName);
122  $form->get($name)->getCfg()->name = str_replace($this->cfg->replaceKey, $val, $name);
123  $form->get($name)->renew();
124  }
125 
126  return str_replace('[fields]', $form->__toString(), $line);
127  }
128 
129  public function isInValue($val) {
130  if (is_array($this->cfg->value))
131  foreach($this->cfg->value as $v) {
132  if ($v[db::getCfg('relatedValue')] == $val)
133  return true;
134  }
135  return false;
136  }
137 }
updateLine($type, $val, $line)
static getCfg($key)
Definition: db.class.php:125
static getInstance()
setValue($value, $refill=false)
$f
Definition: list.php:6
static getInstance()
Definition: vars.class.php:29
get($name)
Definition: form.class.php:384
static get($className, array $cfg=array())
Generated on Sun Oct 15 2017 22:25:20 for nyroFwk by doxygen 1.8.13