1: <?php
2:
3: class InputSearchable {
4:
5: public function __construct(ExtendedFormHelper $parent, $fieldName, $options) {
6: $this->parent = $parent;
7: $this->fieldName = $fieldName;
8: $this->options = $options;
9: $this->hiddenInputId = $this->parent->createNewDomId();
10: $this->visibleInputId = $this->parent->createNewDomId();
11: }
12:
13: public function output() {
14: $b = $this->parent->hidden($this->fieldName, array('id' => $this->hiddenInputId));
15: $b .= $this->_visibleInput();
16: return $b;
17: }
18:
19: private function _visibleInput() {
20: $visibleOptions = $this->options;
21: unset($visibleOptions['search']);
22: $visibleOptions['id'] = $this->visibleInputId;
23: $visibleOptions['initCallback'] = 'ExtendedFormHelper.InputSearchable.initCallback';
24: $visibleOptions['initOptions'] = json_encode(array(
25: 'searchOptions' => $this->_searchOptions(),
26: 'initialId' => $this->_hiddenInputValue(),
27: 'initialLabel' => $this->_visibleInputValue(),
28: ));
29: return $this->parent->text(
30: $this->fieldName . '_search', $visibleOptions
31: );
32: }
33:
34: private function _hiddenInputValue() {
35: $this->parent->setEntity($this->fieldName);
36: if (($fieldValue = $this->parent->value())) {
37: return $fieldValue['value'];
38: } else {
39: return '';
40: }
41: }
42:
43: private function _visibleInputValue() {
44: if (($hiddenValue = $this->_hiddenInputValue())) {
45: $model = $this->parent->CakeLayers->getModel($this->_searchOptions('modelName'));
46: $instance = $model->findByPrimaryKey($hiddenValue);
47: return $instance[$model->alias][$this->_searchOptions('displayField')];
48: }
49:
50: return '';
51: }
52:
53: private function _searchOptions($subOption = false) {
54: if ($subOption) {
55: if (!empty($this->options['search'][$subOption])) {
56: return $this->options['search'][$subOption];
57: } else {
58: return false;
59: }
60: } else {
61: return $this->options['search'];
62: }
63: }
64:
65: }
66:
67: ?>
68: