Go to the documentation of this file.00001 <?php
00002 class form_autocomplete extends form_checkbox {
00003
00004 public function setValue($value, $refill=false) {
00005 if (is_array($value) && $this->cfg->uniqValue) {
00006 parent::setValue(array_shift($value));
00007 } else {
00008 if (is_array($value))
00009 $value = $this->addNew($value);
00010 parent::setValue($value);
00011 }
00012 }
00013
00020 protected function addNew(array $values) {
00021 $new = isset($values['new']) && is_array($values['new']) ? array_filter($values['new']) : null;
00022 unset($values['new']);
00023 if ($this->cfg->allowAdd && count($new) && is_array($new)) {
00024 $dbList = $this->cfg->dbList;
00025 if (is_array($dbList) && $dbList['table']) {
00026 $tbl = db::get('table', $dbList['table']);
00027 $list = utils::htmlDeOut($this->cfg->list);
00028
00029 $i18n = isset($dbList['i18nFields']) && $dbList['i18nFields'];
00030 if (!$i18n)
00031 $field = substr($dbList['fields'], strlen($dbList['ident'])+1);
00032
00033 foreach($new as $v) {
00034 $exists = array_search($v, $list);
00035 if ($exists === false) {
00036 $row = $tbl->getRow();
00037 if ($i18n) {
00038 $i18nRow = $row->getI18nRow();
00039 $i18nRow->set($dbList['i18nFields'], $v);
00040 } else {
00041 $row->set($field, $v);
00042 }
00043 $id = $row->save();
00044 $values[] = $id;
00045 $list[$id] = $v;
00046 } else
00047 $values[] = $exists;
00048 }
00049 $this->cfg->list = utils::htmlOut($list);
00050 if (is_array($this->cfg->list))
00051 $this->addRule('in', array_keys($this->cfg->list));
00052 }
00053 }
00054 return $values;
00055 }
00056
00057 public function to($type) {
00058 $ret = parent::to($type);
00059
00060 if ($type == 'html' && $this->cfg->mode == 'edit') {
00061 $ret = '<span id="'.$this->id.'Container">'.$ret.'</span>';
00062 $resp = response::getInstance();
00063 $resp->addJs('jqueryui');
00064 $resp->addJs('formAutocomplete');
00065 $prm = $this->cfg->jsPrm;
00066 $prm['name'] = $this->id.'[]';
00067 $prm['nameNew'] = $this->id.'[new]';
00068 $resp->blockJquery('$("#'.$this->id.'Container").formAutocomplete('.json_encode($prm).');');
00069 }
00070
00071 return $ret;
00072 }
00073 }