nyroFwk  0.2
autocomplete.class.php
Go to the documentation of this file.
1 <?php
3 
4  public function setValue($value, $refill=false) {
5  if (is_array($value) && $this->cfg->uniqValue) {
6  parent::setValue(array_shift($value));
7  } else {
8  if (is_array($value))
9  $value = $this->addNew($value);
10  parent::setValue($value);
11  }
12  }
13 
20  protected function addNew(array $values) {
21  $new = isset($values['new']) && is_array($values['new']) ? array_filter($values['new']) : null;
22  unset($values['new']);
23  if ($this->cfg->allowAdd && count($new) && is_array($new)) {
24  $dbList = $this->cfg->dbList;
25  if (is_array($dbList) && $dbList['table']) {
26  $tbl = db::get('table', $dbList['table']);
27  $list = utils::htmlDeOut($this->cfg->list);
28 
29  $i18n = isset($dbList['i18nFields']) && $dbList['i18nFields'];
30  if (!$i18n)
31  $field = substr($dbList['fields'], strlen($dbList['ident'])+1);
32 
33  foreach($new as $v) {
34  $exists = array_search($v, $list);
35  if ($exists === false) {
36  $row = $tbl->getRow();
37  if ($i18n) {
38  $i18nRow = $row->getI18nRow();
39  $i18nRow->set($dbList['i18nFields'], $v);
40  } else {
41  $row->set($field, $v);
42  }
43  $id = $row->save();
44  $values[] = $id;
45  $list[$id] = $v;
46  } else
47  $values[] = $exists;
48  }
49  $this->cfg->list = utils::htmlOut($list);
50  if (is_array($this->cfg->list))
51  $this->addRule('in', array_keys($this->cfg->list));
52  }
53  }
54  return $values;
55  }
56 
57  public function to($type) {
58  $ret = parent::to($type);
59 
60  if ($type == 'html' && $this->cfg->mode == 'edit') {
61  $ret = '<span id="'.$this->id.'Container">'.$ret.'</span>';
62  $resp = response::getInstance();
63  $resp->addJs('jqueryui');
64  $resp->addJs('formAutocomplete');
65  $prm = $this->cfg->jsPrm;
66  $prm['name'] = $this->id.'[]';
67  $prm['nameNew'] = $this->id.'[new]';
68  $resp->blockJquery('$("#'.$this->id.'Container").formAutocomplete('.json_encode($prm).');');
69  }
70 
71  return $ret;
72  }
73 }
static htmlOut($val, $key=false)
static htmlDeOut($val, $key=false)
addRule($type, $prm=null)
addNew(array $values)
static getInstance()
setValue($value, $refill=false)
static get($type, $table, array $prm=array())
Definition: db.class.php:87
Generated on Sun Oct 15 2017 22:25:20 for nyroFwk by doxygen 1.8.13