Exemplo n.º 1
0
 /**
  * Generated from @assert (array('one', 'One', 'two'), 'one') [==] array('one', 'One').
  *
  * @covers Kotchasan\ArrayTool::filter
  */
 public function testFilter()
 {
     $this->assertEquals(array('one', 'One'), \Kotchasan\ArrayTool::filter(array('one', 'One', 'two'), 'one'));
 }
Exemplo n.º 2
0
 /**
  * สร้างตาราง และเริ่มต้นทำงานตาราง
  * คืนค่าเป็นโค้ด HTML ของ DataTable
  *
  * @return string
  */
 public function render()
 {
     if (!empty($this->actions) && $this->checkCol == -1) {
         $this->checkCol = 1;
     }
     $url_query = array();
     $hidden_fields = array();
     foreach (self::$request->getQueryParams() as $key => $value) {
         $value = rawurlencode($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)) {
             $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->request('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);
             }
         }
         $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">' . 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->request('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
         if ($this->checkCol > -1) {
             $row = array();
             $row[] = '<td colspan="' . $this->checkCol . '"></td>';
             $row[] = '<td class="check-column"><a class="checkall icon-uncheck"></a></td>';
             $row[] = '<td colspan="' . ($colCount - $this->checkCol - 1) . '"></td>';
             $content[] = '<tfoot><tr>' . implode('', $row) . '</tr></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">' . self::$request->getUri()->pagination($totalpage, $page) . '</div>';
         }
     }
     $content[] = '</div>';
     $script = array('page' => $page, 'search' => $search, 'sort' => $this->sort, 'action' => $this->action, 'actionCallback' => $this->actionCallback, 'actionConfirm' => $this->actionConfirm, '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);
 }