| 1: | <?php |
| 2: | |
| 3: | namespace ZCL\BT; |
| 4: | |
| 5: | use \Zippy\Interfaces\AjaxRender; |
| 6: | use \Zippy\WebApplication; |
| 7: | |
| 8: | |
| 9: | |
| 10: | |
| 11: | |
| 12: | class Tags extends \Zippy\Html\Form\HtmlFormDataElement |
| 13: | { |
| 14: | |
| 15: | private $_sug = array(); |
| 16: | private $_options = array(); |
| 17: | |
| 18: | |
| 19: | |
| 20: | |
| 21: | |
| 22: | |
| 23: | |
| 24: | public function __construct($id, $options = null) { |
| 25: | parent::__construct($id); |
| 26: | $this->setValue(array()); |
| 27: | if (is_array($options)) { |
| 28: | $this->_options = $options; |
| 29: | } |
| 30: | } |
| 31: | |
| 32: | public function RenderImpl() { |
| 33: | $url = $this->owner->getURLNode() . "::" . $this->id . "&ajax=true"; |
| 34: | |
| 35: | $data = json_encode($this->value); |
| 36: | $sug = json_encode($this->_sug); |
| 37: | $val = implode(';', $this->value); |
| 38: | |
| 39: | $js = " |
| 40: | var tags = $('#{$this->id}').tags({ |
| 41: | tagData:{$data}, |
| 42: | suggestions:{$sug} , "; |
| 43: | foreach ($this->_options as $key => $value) { |
| 44: | $js .= "{$key}:'{$value}',"; |
| 45: | } |
| 46: | $js .= " afterAddingTag:function(tag){ |
| 47: | var t = tags.getTags(); |
| 48: | $('#{$this->id}_tags').val(t.join(\";\")); |
| 49: | }, |
| 50: | afterDeletingTag:function(tag){ |
| 51: | var t = tags.getTags(); |
| 52: | $('#{$this->id}_tags').val(t.join(\";\")); |
| 53: | |
| 54: | } |
| 55: | |
| 56: | |
| 57: | }); |
| 58: | $('#{$this->id}').after('<input type=\"hidden\" id=\"{$this->id}_tags\" name=\"{$this->id}_tags\" value=\"{$val}\" />'); |
| 59: | |
| 60: | "; |
| 61: | |
| 62: | WebApplication::$app->getResponse()->addJavaScript($js, true); |
| 63: | } |
| 64: | |
| 65: | |
| 66: | |
| 67: | |
| 68: | |
| 69: | |
| 70: | public function setSuggestions($sug) { |
| 71: | $this->_sug = $sug; |
| 72: | } |
| 73: | |
| 74: | public function setTags($tags) { |
| 75: | $this->setValue($tags); |
| 76: | } |
| 77: | |
| 78: | public function getTags() { |
| 79: | return $this->getValue(); |
| 80: | } |
| 81: | |
| 82: | public function getRequestData() { |
| 83: | |
| 84: | $tags = $_REQUEST[$this->id . '_tags']; |
| 85: | $this->setValue(explode(';', $tags)); |
| 86: | } |
| 87: | |
| 88: | public function clean() { |
| 89: | $this->setTags(array()); |
| 90: | $this->setSuggestions(array()); |
| 91: | } |
| 92: | |
| 93: | } |
| 94: | |