public function index(Request $request) { $request->initSession(); // Antispam Image Antispam::createImage($request->get('id')->toString()); }
/** * ฟังก์ชั่นสร้าง Form Element * * @param string $tag * @param array $param property ของ Input * id, name, type property ต่างๆของinput * label : ข้อความแสดงใน label ของ input * labelClass : class ของ label * comment : ถ้ากำหนดจะแสดงคำอธิบายของ input * ถ้าไม่กำหนดทั้ง label และ labelClass จะเป็นการสร้าง input อย่างเดียว * @param string $options ตัวเลือก options ของ select * array('name1' => 'value1', 'name2' => 'value2', ....) */ public function render() { $prop = array(); $event = array(); foreach ($this->attributes as $k => $v) { switch ($k) { case 'itemClass': case 'itemId': case 'labelClass': case 'label': case 'comment': case 'unit': case 'value': case 'dataPreview': case 'previewSrc': case 'accept': case 'options': case 'optgroup': case 'multiple': case 'validator': case 'antispamid': case 'text': case 'validator': ${$k} = $v; break; case 'result': $prop[$k] = 'data-' . $k . '="' . $v . '"'; break; case 'title': $prop['title'] = 'title="' . strip_tags($v) . '"'; break; default: if (is_int($k)) { $prop[$v] = $v; } elseif ($v === true) { $prop[$k] = $k; } elseif ($v === false) { continue; } elseif (preg_match('/^on([a-z]+)/', $k, $match)) { $event[$match[1]] = $v; } else { $prop[$k] = $k . '="' . $v . '"'; ${$k} = $v; } break; } } if (isset($id) && empty($name)) { $name = $id; $prop['name'] = 'name="' . $name . '"'; } if (isset(Html::$form)) { if (isset($id) && Html::$form->gform) { if (isset($validator)) { $js = array(); $js[] = '"' . $id . '"'; $js[] = '"' . $validator[0] . '"'; $js[] = $validator[1]; if (isset($validator[2])) { $js[] = '"' . $validator[2] . '"'; $js[] = empty($validator[3]) || $validator[3] === null ? 'null' : '"' . $validator[3] . '"'; $js[] = '"' . Html::$form->attributes['id'] . '"'; } $this->javascript[] = 'new GValidator(' . implode(', ', $js) . ');'; unset($validator); } foreach ($event as $on => $func) { $this->javascript[] = '$G("' . $id . '").addEvent("' . $on . '", ' . $func . ');'; } } elseif (!Html::$form->gform) { foreach ($event as $on => $func) { $prop['on' . $on] = 'on' . $on . '="' . $func . '()"'; } } } if ($this->tag == 'select') { unset($prop['type']); if (isset($multiple)) { $value = isset($value) ? $value : array(); } else { $value = isset($value) ? $value : null; } if (isset($options)) { $datas = array(); foreach ($options as $k => $v) { if (is_array($value)) { $sel = in_array($k, $value) ? ' selected' : ''; } else { $sel = $value == $k ? ' selected' : ''; } $datas[] = '<option value="' . $k . '"' . $sel . '>' . $v . '</option>'; } $value = implode('', $datas); } elseif (isset($optgroup)) { $datas = array(); foreach ($optgroup as $group_label => $options) { $datas[] = '<optgroup label="' . $group_label . '">'; foreach ($options as $k => $v) { if (is_array($value)) { $sel = in_array($k, $value) ? ' selected' : ''; } else { $sel = $value == $k ? ' selected' : ''; } $datas[] = '<option value="' . $k . '"' . $sel . '>' . $v . '</option>'; } $datas[] = '</optgroup>'; } $value = implode('', $datas); } } elseif (isset($value)) { if ($this->tag === 'textarea') { $value = str_replace(array('{', '}', '&'), array('{', '}', '&'), htmlspecialchars($value)); } else { $prop['value'] = 'value="' . str_replace('&', '&', htmlspecialchars($value)) . '"'; } } if (empty($prop['title']) && !empty($comment)) { $prop['title'] = 'title="' . strip_tags($comment) . '"'; } if (isset($dataPreview)) { $prop['data-preview'] = 'data-preview="' . $dataPreview . '"'; } if (isset($accept) && is_array($accept)) { $prop['accept'] = 'accept="' . Mime::getEccept($accept) . '"'; } if (isset($multiple)) { $prop['multiple'] = 'multiple'; } $prop = implode(' ', $prop); if ($this->tag == 'input') { $element = '<' . $this->tag . ' ' . $prop . '>'; } elseif (isset($value)) { $element = '<' . $this->tag . ' ' . $prop . '>' . $value . '</' . $this->tag . '>'; } else { $element = '<' . $this->tag . ' ' . $prop . '></' . $this->tag . '>'; } if (!empty($antispamid)) { $element = Antispam::createImage($antispamid, true) . $element; } if (empty($itemClass)) { $input = empty($comment) ? '' : '<div class="item"' . (empty($itemId) ? '' : ' id="' . $itemId . '"') . '>'; if (empty($labelClass) && empty($label)) { $input .= $element; } elseif (isset($type) && ($type === 'checkbox' || $type === 'radio')) { $input .= '<label' . (empty($labelClass) ? '' : ' class="' . $labelClass . '"') . '>' . $element . ' ' . $label . '</label>'; } else { $input .= '<label' . (empty($labelClass) ? '' : ' class="' . $labelClass . '"') . '>' . (empty($label) ? '' : $label . ' ') . $element . '</label>'; } if (!empty($comment)) { $input .= '<div class="comment"' . (empty($id) ? '' : ' id="result_' . $id . '"') . '>' . $comment . '</div></div>'; } } else { $itemClass .= isset($unit) ? ' wlabel' : ''; $input = '<div class="' . $itemClass . '"' . (empty($itemId) ? '' : ' id="' . $itemId . '"') . '>'; if (isset($type) && $type === 'checkbox') { $input .= '<label' . (empty($labelClass) ? '' : ' class="' . $labelClass . '"') . '>' . $element . ' ' . $label . '</label>'; } else { if (isset($dataPreview)) { $input .= '<div class=usericon><span><img src="' . $previewSrc . '" alt="Image preview" id=' . $dataPreview . '></span></div>'; } if (isset($label) && isset($id)) { $input .= '<label for="' . $id . '">' . $label . '</label>'; } $input .= '<span' . (empty($labelClass) ? '' : ' class="' . $labelClass . '"') . '>' . $element . '</span>'; if (isset($unit)) { $input .= '<span class=label>' . $unit . '</span>'; } } if (!empty($comment)) { $input .= '<div class="comment"' . (empty($id) ? '' : ' id="result_' . $id . '"') . '>' . $comment . '</div>'; } $input .= '</div>'; } return $input; }