1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ajax\semantic\html\modules; |
4
|
|
|
|
5
|
|
|
use Ajax\semantic\html\base\HtmlSemDoubleElement; |
6
|
|
|
use Ajax\semantic\html\elements\HtmlInput; |
7
|
|
|
use Ajax\JsUtils; |
8
|
|
|
use Ajax\semantic\html\base\constants\Direction; |
9
|
|
|
|
10
|
|
|
class HtmlSearch extends HtmlSemDoubleElement { |
11
|
|
|
private $_elements=array (); |
12
|
|
|
private $_params=array (); |
13
|
|
|
private $_searchFields=array ("title" ); |
14
|
|
|
|
15
|
|
|
public function __construct($identifier, $placeholder=NULL, $icon=NULL) { |
16
|
|
|
parent::__construct("search-" . $identifier, "div", "ui search", array ()); |
17
|
|
|
$this->createField($placeholder, $icon); |
18
|
|
|
$this->createResult(); |
19
|
|
|
$this->_params["type"]="standard"; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
private function createField($placeholder=NULL, $icon=NULL) { |
23
|
|
|
$field=new HtmlInput($this->identifier); |
24
|
|
|
if (isset($placeholder) === true) |
25
|
|
|
$field->setPlaceholder($placeholder); |
26
|
|
|
if (isset($icon) === true) |
27
|
|
|
$field->addIcon($icon, Direction::RIGHT); |
28
|
|
|
$field->getField()->setClass("prompt"); |
29
|
|
|
$this->content["field"]=$field; |
30
|
|
|
return $field; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
private function createResult() { |
34
|
|
|
$this->content["result"]=new HtmlSemDoubleElement("results-" . $this->identifier, "div", "results"); |
35
|
|
|
return $this->content["result"]; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function addResult($object) { |
39
|
|
|
$this->_elements[]=$object; |
40
|
|
|
return $this; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function addResults($objects) { |
44
|
|
|
$this->_elements=\array_merge($this->_elements, $objects); |
45
|
|
|
return $this; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function setUrl($url) { |
49
|
|
|
$this->_params["apiSettings"]="%{url: %quote%" . $url . "%quote%}%"; |
50
|
|
|
return $this; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function setType($type) { |
54
|
|
|
$this->_params["type"]=$type; |
55
|
|
|
return $this; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function getType() { |
59
|
|
|
return $this->_params["type"]; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
private function resultsToJson() { |
63
|
|
|
$result=\json_encode($this->_elements); |
64
|
|
|
return $result; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function run(JsUtils $js) { |
68
|
|
|
$searchFields=\json_encode($this->_searchFields); |
69
|
|
|
$searchFields=str_ireplace("\"", "%quote%", $searchFields); |
70
|
|
|
$this->_params["searchFields"]="%" . $searchFields . "%"; |
71
|
|
|
if ($this->getType() === "standard") |
72
|
|
|
$this->_params["source"]="%content%"; |
73
|
|
|
$this->addEvent("beforeExecute", "var content=" . $this->resultsToJson() . ";"); |
74
|
|
View Code Duplication |
if (isset($this->_bsComponent) === false) { |
|
|
|
|
75
|
|
|
$this->_bsComponent=$js->semantic()->search("#" . $this->identifier, $this->_params); |
76
|
|
|
} |
77
|
|
|
$this->addEventsOnRun($js); |
78
|
|
|
return $this->_bsComponent; |
79
|
|
|
} |
80
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.