/**
  * แสดงผล
  *
  * @param object $index ข้อมูลโมดูล
  */
 public function render($index)
 {
     // รายการ
     $listitem = Grid::create('search', 'search', 'searchitem');
     foreach ($index->items as $item) {
         if ($item->index == 0 && $item->owner == 'document') {
             // document
             $uri1 = \Document\Index\Controller::url($item->module, $item->alias, $item->id);
             $uri2 = \Document\Index\Controller::url($item->module, $item->alias, $item->id, false);
         } elseif ($item->index == 0 && $item->owner == 'board') {
             // board
             $uri1 = \Board\Index\Controller::url($item->module, 0, $item->id);
             $uri2 = $uri1;
         } else {
             // other
             if (self::$cfg->module_url == 1) {
                 $uri1 = Gcms::createUrl($item->module, $item->alias);
                 $uri2 = Gcms::createUrl($item->module, $item->alias, 0, 0, '', false);
             } else {
                 $uri1 = Gcms::createUrl($item->module, '', 0, $item->id, '');
                 $uri2 = $uri1;
             }
         }
         $listitem->add(array('/{URL}/' => $uri1, '/{TOPIC}/' => $item->topic, '/{LINK}/' => $uri2, '/{DETAIL}/' => $item->description));
     }
     // template search/search.html
     $template = Template::create('search', 'search', 'search');
     // canonical
     $index->canonical = Gcms::createUrl($index->module);
     // current URL
     $uri = \Kotchasan\Http\Uri::createFromUri($index->canonical);
     if ($index->total > 0) {
         $list = Gcms::highlightSearch($listitem->render(), $index->q);
     } else {
         $list = $index->q == '' ? '' : '<div>' . Language::get('No results were found for') . ' <strong>' . $index->q . '</strong></div>';
         $list .= '<div><strong>' . Language::get('Search tips') . ' :</strong>' . Language::get('<ul><li>make sure that the spelling correct</li><li>try changing or new phrases. synonyms</li><li>try to identify a non-specific too</li><li>specific keywords to search the most concise</li></ul>') . '</div>';
     }
     // add template
     $template->add(array('/{LIST}/' => $list, '/{SPLITPAGE}/' => $uri->pagination($index->totalpage, $index->page), '/{SEARCH}/' => $index->q, '/{MODULE}/' => 'search', '/{RESULT}/' => $index->total == 0 ? '' : sprintf(Language::get('Search results <strong>%d - %d</strong> of about <strong>%d</strong> for <strong>%s</strong> (%s sec)'), $index->start + 1, $index->end, $index->total, $index->q, number_format(microtime(true) - REQUEST_TIME, 4))));
     $search = Language::get('Search');
     $index->detail = $template->render();
     $index->topic = ($index->q == '' ? '' : $index->q . ' - ') . $search;
     $index->description = $index->topic;
     $index->keywords = $index->topic;
     $index->menu = 'search';
     // breadcrumb ของหน้า
     Gcms::$view->addBreadcrumb($index->canonical, $search, $search);
     return $index;
 }
Beispiel #2
0
 /**
  * กำหนดค่า Uri
  *
  * @param Uri $uri
  * @param boolean $preserveHost
  * @return \static
  */
 public function withUri(UriInterface $uri, $preserveHost = false)
 {
     $clone = clone $this;
     $clone->uri = $uri;
     if (!$preserveHost) {
         if ($uri->getHost() !== '') {
             $clone->headers['Host'] = $uri->getHost();
         }
     } else {
         if ($this->uri->getHost() !== '' && (!$this->hasHeader('Host') || $this->getHeader('Host') === null)) {
             $clone->headers['Host'] = $uri->getHost();
         }
     }
     return $clone;
 }
Beispiel #3
0
 /**
  * สร้างตาราง และเริ่มต้นทำงานตาราง
  * คืนค่าเป็นโค้ด HTML ของ DataTable
  *
  * @return string
  */
 public function render()
 {
     if (!empty($this->actions) && $this->checkCol == -1) {
         $this->checkCol = 1;
     }
     $url_query = array();
     $hidden_fields = array();
     parse_str($this->uri->getQuery(), $query_string);
     foreach ($query_string as $key => $value) {
         $url_query[$key] = $key . '=' . $value;
         // แอเรย์เก็บรายการ input ที่ไม่ต้องสร้าง
         if ($key !== 'search' && $key !== 'count' && $key !== 'page' && $key !== 'action') {
             $hidden_fields[$key] = '<input type="hidden" name="' . $key . '" value="' . $value . '">';
         }
     }
     if (isset($this->model)) {
         // รายการ Query หลัก (AND)
         $qs = array();
         foreach ($this->defaultFilters as $array) {
             $qs[] = $array;
         }
     }
     // create HTML
     $content = array('<div class="datatable" id="' . $this->id . '">');
     // form
     $form = array();
     if (isset($this->perPage)) {
         $entries = Language::get('entries');
         $form[] = $this->addFilter(array('name' => 'count', 'text' => Language::get('Show'), 'value' => $this->perPage, 'options' => array(10 => '10 ' . $entries, 20 => '20 ' . $entries, 30 => '30 ' . $entries, 40 => '40 ' . $entries, 50 => '50 ' . $entries, 100 => '100 ' . $entries)));
     }
     // รายการ Query กำหนดโดย User (AND)
     foreach ($this->filters as $key => $items) {
         $form[] = $this->addFilter($items);
         unset($hidden_fields[$items['name']]);
         if (!isset($items['default'])) {
             $items['default'] = '';
         }
         // ไม่ Query รายการ default
         if (!empty($items['options']) && isset($items['value']) && $items['value'] !== $items['default'] && in_array($items['value'], array_keys($items['options']), true)) {
             if (isset($items['onFilter'])) {
                 $q = call_user_func($items['onFilter'], $key, $items['value']);
                 if ($q) {
                     $qs[] = $q;
                 }
             } else {
                 $qs[] = array($key, $items['value']);
             }
         }
     }
     // ปุ่ม Go
     if (!empty($form)) {
         $form[] = '<fieldset>';
         $form[] = '<input type=submit class="button go" value="' . Language::get('Go') . '">';
         $form[] = implode('', $hidden_fields);
         $form[] = '</fieldset>';
     }
     // search
     $search = self::$request->globals(array('POST', 'GET'), 'search')->text();
     if (!empty($this->searchColumns)) {
         if (!empty($search)) {
             if (isset($this->model)) {
                 $sh = array();
                 foreach ($this->searchColumns as $key) {
                     $sh[] = array($key, 'LIKE', "%{$search}%");
                 }
                 $qs[] = $this->rs->group($sh, 'OR');
             } elseif (isset($this->datas)) {
                 // filter ข้อมูลจาก array
                 $this->datas = ArrayTool::filter($this->datas, $search);
             }
             $this->uri = $this->uri->withParams(array('search' => $search));
         }
         $form[] = '&nbsp;<fieldset class=search>';
         $form[] = '<label accesskey=f class="icon-search"><input type=text name=search value="' . $search . '" placeholder="' . Language::get('Search') . '"></label>';
         $form[] = '<input type=submit value="&nbsp;">';
         $form[] = '</fieldset>';
     }
     if (!empty($form)) {
         $content[] = '<form class="table_nav" method="get" action="' . $this->uri . '">' . implode('', $form) . '</form>';
     }
     if (isset($this->model)) {
         // Model
         $query = $this->rs->where($qs)->toArray();
         if ($this->cache) {
             $this->rs->cacheOn();
         }
         // จำนวนข้อมูลทั้งหมด
         $count = $this->rs->count();
     } elseif (!empty($this->datas)) {
         // จำนวนข้อมูลใน array
         $count = sizeof($this->datas);
     } else {
         $count = 0;
     }
     // การแบ่งหน้า
     if (empty($this->perPage)) {
         $start = 0;
         $totalpage = 1;
         $page = 1;
         $s = 1;
         $e = $count;
         $this->perPage = 0;
     } else {
         // หน้าที่เลือก
         $page = max(1, self::$request->globals(array('POST', 'GET'), 'page', 1)->toInt());
         // ตรวจสอบหน้าที่เลือกสูงสุด
         $totalpage = round($count / $this->perPage);
         $totalpage += $totalpage * $this->perPage < $count ? 1 : 0;
         $page = max(1, $page > $totalpage ? $totalpage : $page);
         $start = $this->perPage * ($page - 1);
         // คำนวณรายการที่แสดง
         $s = $start < 0 ? 0 : $start + 1;
         $e = min($count, $s + $this->perPage - 1);
     }
     // table caption
     if ($this->showCaption) {
         if (empty($search)) {
             $caption = Language::get('All :count entries, displayed :start to :end, page :page of :total pages');
         } else {
             $caption = Language::get('Search <strong>:search</strong> found :count entries, displayed :start to :end, page :page of :total pages');
         }
         $caption = str_replace(array(':search', ':count', ':start', ':end', ':page', ':total'), array($search, number_format($count), number_format($s), number_format($e), number_format($page), number_format($totalpage)), $caption);
     }
     // เรียงลำดับ
     if (!empty($this->sort)) {
         $sorts = array();
         foreach (explode(',', $this->sort) as $sort) {
             if (preg_match('/^([a-z0-9_\\-]+)([\\s]+(desc|asc))?$/i', trim($sort), $match)) {
                 if (isset($this->headers[$match[1]]['sort'])) {
                     $sort = $this->headers[$match[1]]['sort'];
                 } elseif (isset($this->columns[$match[1]])) {
                     $sort = $match[1];
                 } elseif (isset($this->rs) && $this->rs->fieldExists($match[1])) {
                     $sort = $match[1];
                 } else {
                     $sort = null;
                 }
                 if ($sort) {
                     $sortType = isset($match[3]) && strtolower($match[3]) == 'desc' ? 'desc' : 'asc';
                     $this->sorts[$sort] = $sortType;
                     $sorts[] = $sort . ' ' . $sortType;
                 }
             }
         }
         $this->sort = implode(',', $sorts);
         if (isset($this->model)) {
             if (!empty($sorts)) {
                 $query->order($sorts);
             }
         } elseif (!empty($this->sorts)) {
             reset($this->sorts);
             $sort = key($this->sorts);
             $this->datas = ArrayTool::sort($this->datas, $sort, $this->sorts[$sort]);
         }
     }
     if (isset($this->model)) {
         // query ข้อมูล
         $this->datas = $this->rs->take($start, $this->perPage)->execute($this->fields);
         // รายการสุดท้าย
         $end = $this->perPage + 1;
         // รายการแรก
         $start = -1;
     } elseif (isset($this->datas)) {
         // รายการสุดท้าย
         $end = $start + $this->perPage - 1;
         // รายการแรก
         $start = $start - 2;
     } else {
         $end = 0;
     }
     if (!empty($this->headers)) {
         // property ของ ตาราง
         $prop = array();
         $c = array();
         if (isset($this->class)) {
             $c[] = $this->class;
         }
         if ($this->border) {
             $c[] = 'border';
         }
         if ($this->responsive) {
             $c[] = 'responsive-v';
         }
         if ($this->fullWidth) {
             $c[] = 'fullwidth';
         }
         if (sizeof($c) > 0) {
             $prop[] = ' class="' . implode(' ', $c) . '"';
         }
         // table
         $content[] = '<div class="tablebody"><table' . implode('', $prop) . '>';
         if ($this->showCaption) {
             $content[] = '<caption>' . $caption . '</caption>';
         }
         $row = array();
         $i = 0;
         $colCount = 0;
         $colspan = 0;
         foreach ($this->headers as $key => $attributes) {
             if ($colspan === 0) {
                 if ($i == $this->checkCol) {
                     $row[] = '<th class="check-column"><a class="checkall icon-uncheck"></a></th>';
                     $colCount++;
                 }
                 if ($i == $this->dragColumn) {
                     $row[] = '<th></th>';
                     $colCount++;
                 }
                 if (isset($attributes['colspan'])) {
                     $colspan = $attributes['colspan'] - 1;
                 }
                 $row[] = $this->th($i, $key, $attributes);
                 $colCount++;
                 $i++;
             } else {
                 $colspan--;
             }
         }
         if (!empty($this->buttons)) {
             $row[] = $this->th($i, '', array('text' => ''));
             $colCount++;
             $i++;
         }
         if ($this->pmButton) {
             $row[] = $this->th($i, '', array('text' => ''));
             $colCount++;
         }
         // thead
         $content[] = '<thead><tr>' . implode('', $row) . '</tr></thead>';
         // tbody
         if (!empty($this->datas)) {
             $content[] = '<tbody>' . $this->tbody($start, $end) . '</tbody>';
         }
         // tfoot
         $tfoot = null;
         if (isset($this->onCreateFooter)) {
             $tfoot = call_user_func($this->onCreateFooter);
         } elseif ($this->checkCol > -1) {
             $tfoot = '<tr>';
             $tfoot .= '<td colspan="' . $this->checkCol . '"></td>';
             $tfoot .= '<td class="check-column"><a class="checkall icon-uncheck"></a></td>';
             $tfoot .= '<td colspan="' . ($colCount - $this->checkCol - 1) . '"></td>';
             $tfoot .= '</tr>';
         }
         if ($tfoot) {
             $content[] = '<tfoot>' . $tfoot . '</tfoot>';
         }
         $content[] = '</table></div>';
         $table_nav = array();
         foreach ($this->actions as $item) {
             $table_nav[] = $this->addAction($item);
         }
         if (!empty($this->addNew)) {
             $prop = array();
             foreach ($this->addNew as $k => $v) {
                 $prop[$k] = $k . '="' . $v . '"';
             }
             if (preg_match('/^((.*)\\s+)?(icon-[a-z0-9\\-_]+)(\\s+(.*))?$/', $this->addNew['class'], $match)) {
                 $prop['class'] = 'class="' . trim($match[2] . ' ' . (isset($match[5]) ? $match[5] : '')) . '"';
                 $table_nav[] = '<a ' . implode(' ', $prop) . '><span class="' . $match[3] . '">' . (isset($this->addNew['text']) ? $this->addNew['text'] : '') . '</span></a>';
             } else {
                 $table_nav[] = '<a ' . implode(' ', $prop) . '>' . (isset($this->addNew['text']) ? $this->addNew['text'] : '') . '</a>';
             }
         }
         if (!empty($table_nav)) {
             $content[] = '<div class="table_nav action">' . implode('', $table_nav) . '</div>';
         }
         // แบ่งหน้า
         if (!empty($this->perPage)) {
             $content[] = '<div class="splitpage">' . $this->uri->pagination($totalpage, $page) . '</div>';
         }
     }
     $content[] = '</div>';
     $script = array('page' => $page, 'search' => $search, 'sort' => $this->sort, 'action' => $this->action, 'actionCallback' => $this->actionCallback, 'actionConfirm' => $this->actionConfirm, 'onBeforeDelete' => $this->onBeforeDelete, 'onInitRow' => $this->onInitRow, 'onAddRow' => $this->onAddRow, 'pmButton' => $this->pmButton, 'dragColumn' => $this->dragColumn);
     $this->javascript[] = 'var table = new GTable("' . $this->id . '", ' . json_encode($script) . ');';
     $content[] = "<script>\n" . implode("\n", $this->javascript) . "\n</script>";
     return implode("\n", $content);
 }