Completed
Push — master ( 69e9f4...3042e6 )
by Jean-Christophe
04:01
created

HtmlSearch   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 102
Duplicated Lines 2.94 %

Coupling/Cohesion

Components 2
Dependencies 4

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 20
c 4
b 0
f 0
lcom 2
cbo 4
dl 3
loc 102
rs 10

16 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A createField() 0 10 3
A createResult() 0 4 1
A addResult() 0 5 1
A addResults() 0 5 1
A setUrl() 0 4 1
A setType() 0 4 1
A getType() 0 3 1
A resultsToJson() 0 4 1
A setLocal() 0 3 1
A run() 3 15 3
A setFluid() 0 3 1
A onSelect() 0 3 1
A _opOnSelect() 0 3 1
A getOnSelect() 0 4 1
A postOnSelect() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
	private $_local=false;
15
16
	public function __construct($identifier, $placeholder=NULL, $icon=NULL) {
17
		parent::__construct("search-" . $identifier, "div", "ui search", array ());
18
		$this->createField($placeholder, $icon);
19
		$this->createResult();
20
		$this->_params["type"]="standard";
21
	}
22
23
	private function createField($placeholder=NULL, $icon=NULL) {
24
		$field=new HtmlInput($this->identifier);
25
		if (isset($placeholder) === true)
26
			$field->setPlaceholder($placeholder);
27
		if (isset($icon) === true)
28
			$field->addIcon($icon, Direction::RIGHT);
29
		$field->getField()->setClass("prompt");
30
		$this->content["field"]=$field;
31
		return $field;
32
	}
33
34
	private function createResult() {
35
		$this->content["result"]=new HtmlSemDoubleElement("results-" . $this->identifier, "div", "results");
36
		return $this->content["result"];
37
	}
38
39
	public function addResult($object) {
40
		$this->_local=true;
41
		$this->_elements[]=$object;
42
		return $this;
43
	}
44
45
	public function addResults($objects) {
46
		$this->_local=true;
47
		$this->_elements=\array_merge($this->_elements, $objects);
48
		return $this;
49
	}
50
51
	public function setUrl($url) {
52
		$this->_params["apiSettings"]="%{url: %quote%" . $url . "%quote%}%";
53
		return $this;
54
	}
55
56
	public function setType($type) {
57
		$this->_params["type"]=$type;
58
		return $this;
59
	}
60
61
	public function getType() {
62
		return $this->_params["type"];
63
	}
64
65
	private function resultsToJson() {
66
		$result=\json_encode($this->_elements);
67
		return $result;
68
	}
69
70
	public function setLocal() {
71
		$this->_local=true;
72
	}
73
74
	public function run(JsUtils $js) {
75
		$this->_params["onSelect"]='%function(result,response){$(%quote%#' . $this->identifier . '%quote%).trigger(%quote%onSelect%quote%, {%quote%result%quote%: result, %quote%response%quote%:response} );}%';
76
		$searchFields=\json_encode($this->_searchFields);
77
		$searchFields=str_ireplace("\"", "%quote%", $searchFields);
78
		$this->_params["searchFields"]="%" . $searchFields . "%";
79
		if ($this->_local === true) {
80
			$this->_params["source"]="%content%";
81
			$this->addEvent("beforeExecute", "var content=" . $this->resultsToJson() . ";");
82
		}
83 View Code Duplication
		if (isset($this->_bsComponent) === false) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
84
			$this->_bsComponent=$js->semantic()->search("#" . $this->identifier, $this->_params);
85
		}
86
		$this->addEventsOnRun($js);
87
		return $this->_bsComponent;
88
	}
89
90
	public function setFluid() {
91
		return $this->addToProperty("class", "fluid");
92
	}
93
94
	public function onSelect($jsCode) {
95
		$this->addEvent("onSelect", $jsCode);
96
	}
97
98
	private function _opOnSelect($operation, $url, $responseElement="", $parameters=array()) {
99
		return $this->_ajaxOn($operation, "onSelect", $url, $responseElement, $parameters);
100
	}
101
102
	public function getOnSelect($url, $responseElement="", $parameters=array()) {
103
		$parameters["params"]="data.result";
104
		return $this->_opOnSelect("get", $url, $responseElement, $parameters);
105
	}
106
107
	public function postOnSelect($url, $responseElement="", $parameters=array()) {
108
		$parameters["params"]="data.result";
109
		return $this->_opOnSelect("post", $url, $responseElement, $parameters);
110
	}
111
}