1: <?php
2:
3: namespace Zippy\Html\Link;
4:
5: use Zippy\WebApplication;
6: use Zippy\HtpRequest;
7: use Zippy\Html\HtmlComponent;
8: use Zippy\Interfaces\ClickListener;
9: use Zippy\Interfaces\Requestable;
10: use Zippy\Interfaces\EventReceiver;
11: use Zippy\Event;
12:
13: /**
14: * Компонент отображающий список ссылок
15: *
16: */
17: class LinkList extends HtmlComponent implements ClickListener, Requestable
18: {
19: public $delimiter = " ";
20: public $list = array();
21: protected $event;
22: public $selectedvalue;
23:
24: /**
25: * Конструктор
26: * @param string ID компонента
27: * @param string Разделитель между ссылками
28: */
29: public function __construct($id, $delimiter = ' ') {
30: parent::__construct($id);
31: $this->delimiter = $delimiter;
32: }
33:
34: /**
35: * @see HtmlComponent
36: */
37: public function RenderImpl() {
38: $url = $this->owner->getURLNode() . "::" . $this->id;
39:
40: $out = "";
41: for ($i = 1; $i <= count($this->list); $i++) {
42: $item = $this->list[$i];
43: $attributes = "";
44: if ($item['selected'] == true) {
45: $item['attributes']['data-selected'] = 'selected';
46: } else {
47: $item['attributes']['data-selected'] = '';
48: }
49: if (count($item['attributes']) > 0) {
50: foreach ($item['attributes'] as $key => $value) {
51: if (strlen($value) > 0) {
52: $attributes = $attributes . ' ' . $key . "=\"{$value}\" ";
53: }
54: }
55: }
56: if ($item['disabled'] == true) {
57: $out = $out . "<span>" . $item['caption'] . "</span>";
58: } else {
59:
60:
61: if ($item['type'] == 'click') {
62: $out = $out . "<a href = \"javascript:void(0);\" onclick=\"window.location='{$url}:{$i}';event.returnValue=false; return false;\" {$attributes} >{$item['caption']}</a>";
63: }
64: if ($item['type'] == 'redirect') {
65: if ($item['encode'] == true) {
66: $_BASEURL = WebApplication::$app->getResponse()->getHostUrl();
67: $url = $_BASEURL . "/index.php?r=" . base64_encode(serialize(array($item['page'], $item['params'])));
68: } else {
69: $pagename = str_replace("\\", "/", ltrim($item['page'], "\\"));
70:
71: $url = $_BASEURL . "/index.php?p=" . $pagename;
72: if (count($item['params']) > 0) {
73: $_param = implode("/", $item['params']);
74: $url .= "&arg=" . $_param;
75: }
76: }
77:
78:
79: $out = $out . "<a href = \"{$url}\" {$attributes} >{$item['caption']}</a>";
80: }
81: if ($item['type'] == 'bookmarkable') {
82: $out = $out . "<a href = \"{$item['href']}\" {$attributes} >{$item['caption']}</a>";
83: }
84: }
85: $out .= $this->delimiter;
86: }
87: $out = substr($out, 0, strlen($out) - strlen($this->delimiter));
88:
89: $this->getTag()->html($out);
90: }
91:
92: /**
93: * Добавляет к списку аналог ClickLink
94: * @param mixed Значение привязанное к ссылке
95: * @param string Текст ссылки
96: * @param array список аттрибутов
97: */
98: public function AddClickLink($value, $caption, $attributes = array()) {
99: $this->list[count($this->list) + 1] = array('type' => 'click', 'value' => $value, 'caption' => $caption, 'attributes' => $attributes, 'selected' => false, 'disabled' => false);
100: }
101:
102: /**
103: * Добавляет к списку аналог BookmarkableLink
104: * @param mixed Адрес ссылки
105: * @param string Текст ссылки
106: * @param array список аттрибутов
107: */
108: public function AddBookmarkableLink($href, $caption, $attributes = array()) {
109: $this->list[count($this->list) + 1] = array('type' => 'bookmarkable', 'href' => $href, 'caption' => $caption, 'attributes' => $attributes, 'selected' => false, 'disabled' => false);
110: }
111:
112: /**
113: * Добавляет к списку аналог RedirectLink
114: * @param mixed Страница
115: * @param array список параметров страницы
116: * @param string Текст ссылки
117: * @param array список аттрибутов
118: */
119: public function AddRedirectLink($page, $params, $caption, $attributes = array(), $bookmarkable = true, $encode = false) {
120: $this->list[count($this->list) + 1] = array('type' => 'redirect', 'page' => $page, 'params' => $params, 'caption' => $caption, 'attributes' => $attributes, 'bookmarkable' => $bookmarkable, 'encode' => $encode, 'selected' => false, 'disabled' => false);
121: }
122:
123: /**
124: * @see Requestable
125: */
126: public function RequestHandle() {
127:
128: $p = WebApplication::$app->getRequest()->request_params[$this->id];
129: if (!is_numeric($p[0])) {
130: return;
131: }
132: $item = $this->list[$p[0]];
133: if (!is_array($item)) {
134: return;
135: }
136: if ($item['type'] == 'click') {
137: $this->selectedvalue = $item['value'];
138: $this->setSelected($p[0]);
139: $this->OnEvent();
140: }
141: if ($item['type'] == 'redirect') {
142: WebApplication::getApplication()->getResponse()->Redirect($item['page'], $item['params']);
143: }
144: }
145:
146: /**
147: * @see ClickListener
148: */
149: public function onClick(\Zippy\Interfaces\EventReceiver $receiver, $handler, $ajax = false) {
150: $this->event = new Event($receiver, $handler);
151: $this->event->isajax = $ajax;
152: }
153:
154: /**
155: * Вызывает событие
156: */
157: public function OnEvent() {
158: if ($this->event != null) {
159: $this->event->onEvent($this);
160: }
161: }
162:
163: /**
164: * Очистка массива
165: */
166: public function Clear() {
167: $this->list = array();
168: }
169:
170: /**
171: * Устанавливает пункт выделенным.
172: * @param mixed Номер пункта
173: */
174: public function setSelected($number) {
175:
176: for ($i = 1; $i <= count($this->list); $i++) {
177: $this->list[$i]['selected'] = false;
178: }
179: if (isset($this->list[$number])) {
180: $this->list[$number]['selected'] = true;
181: }
182: }
183:
184: /**
185: * Устанавливает пункт как текст
186: * @param mixed Номер пункта
187: */
188: public function setDisabled($number) {
189: for ($i = 1; $i <= count($this->list); $i++) {
190: $this->list[$i]['disabled'] == false;
191: }
192:
193: if (isset($this->list[$number])) {
194: $this->list[$number]['disabled'] = true;
195: }
196: }
197:
198: public function getSelectedValue() {
199: return $this->selectedvalue;
200: }
201:
202: }
203: