コード例 #1
0
 /**
  * insert or update record
  */
 public function save()
 {
     $rs = new Recordset(get_called_class());
     if ($this->exists) {
         $rs->update(array($this->primaryKey, (int) $this->{$this->primaryKey}), $this);
     } else {
         $rs->insert($this);
     }
 }
コード例 #2
0
 /**
  * action
  */
 public static function action()
 {
     $ret = array();
     // referer, session, admin
     if (self::$request->initSession() && self::$request->isReferer() && ($login = Login::isAdmin())) {
         if ($login['email'] == 'demo') {
             $ret['alert'] = Language::get('Unable to complete the transaction');
         } else {
             if (self::$request->post('action')->toString() === 'delete') {
                 $id = self::$request->post('action')->toInt();
                 $rs = Recordset::create(get_called_class());
                 $index = $rs->find($id);
                 if ($index) {
                     $index->delete();
                 }
                 // คืนค่า
                 $ret['delete_id'] = self::$request->post('src')->toString() . '_' . $id;
                 $ret['alert'] = Language::get('Deleted successfully');
             }
         }
     } else {
         $ret['alert'] = Language::get('Unable to complete the transaction');
     }
     // คืนค่าเป็น JSON
     echo json_encode($ret);
 }
コード例 #3
0
 /**
  * แสดงผล
  *
  * @return string
  */
 public function render()
 {
     // อ่านข้อมูลสมาชิก
     $rs = Recordset::create('Index\\User\\Model');
     $user = $rs->where((int) $_SESSION['login']['id'])->first('id');
     $template = Template::create('member', 'member', 'password');
     $contents = array('/{ACCEPT}/' => Mime::getEccept(self::$cfg->user_icon_typies), '/{USER_ICON_TYPIES}/' => sprintf(Language::get('Upload a picture of %s resize automatically'), empty(self::$cfg->user_icon_typies) ? 'jpg' : implode(', ', self::$cfg->user_icon_typies)));
     // ข้อมูลฟอร์ม
     foreach ($user as $key => $value) {
         if ($key == 'sex') {
             $source = Language::get('SEXES');
             $datas = array();
             foreach ($source as $k => $v) {
                 $sel = $k == $value ? ' selected' : '';
                 $datas[] = '<option value="' . $k . '"' . $sel . '>' . $v . '</option>';
             }
             $contents['/{' . strtoupper($key) . '}/'] = implode('', $datas);
         } elseif ($key === 'subscrib') {
             $contents['/{' . strtoupper($key) . '}/'] = $value == 1 ? 'checked' : '';
         } elseif ($key === 'icon') {
             if (is_file(ROOT_PATH . self::$cfg->usericon_folder . $value)) {
                 $icon = WEB_URL . self::$cfg->usericon_folder . $value;
             } else {
                 $icon = WEB_URL . 'skin/img/noicon.jpg';
             }
             $contents['/{ICON}/'] = $icon;
         } else {
             $contents['/{' . strtoupper($key) . '}/'] = $value;
         }
     }
     $template->add($contents);
     return $template->render();
 }
コード例 #4
0
 /**
  * แสดงผล
  *
  * @return string
  */
 public function render()
 {
     // อ่านข้อมูลสมาชิก
     $rs = Recordset::create('Index\\User\\Model');
     $user = $rs->where((int) $_SESSION['login']['id'])->first('id', 'provinceID', 'country', 'fname', 'lname', 'address1', 'address2', 'province', 'zipcode');
     $template = Template::create('member', 'member', 'address');
     $contents = array();
     // ข้อมูลฟอร์ม
     foreach ($user as $key => $value) {
         if ($key === 'provinceID' || $key === 'country') {
             // select
             if ($key == 'provinceID') {
                 $source = Province::all();
             } elseif ($key == 'country') {
                 $source = Country::all();
             }
             $datas = array();
             foreach ($source as $k => $v) {
                 $sel = $k == $value ? ' selected' : '';
                 $datas[] = '<option value="' . $k . '"' . $sel . '>' . $v . '</option>';
             }
             $contents['/{' . strtoupper($key) . '}/'] = implode('', $datas);
         } else {
             $contents['/{' . strtoupper($key) . '}/'] = $value;
         }
     }
     $template->add($contents);
     return $template->render();
 }
コード例 #5
0
ファイル: index.php プロジェクト: goragod/kotchasan
 /**
  * Recordset Performance (select and update)
  * ทดสอบการเรียกข้อมูลและอัปเดทข้อมูลด้วย Recordset
  */
 public function recordset()
 {
     $rs = \Kotchasan\Orm\Recordset::create('Index\\World\\Model');
     $rs->updateAll(array('name' => ''));
     for ($i = 0; $i < 2; $i++) {
         $rnd = mt_rand(1, 10000);
         $result = $rs->find($rnd);
         $result->name = 'Hello World!';
         $result->save();
     }
     $result = $rs->find($result->id);
     echo $result->name;
 }
コード例 #6
0
ファイル: index.php プロジェクト: goragod/kotchasan
 /**
  * แสดงผล
  *
  * @param Request $request
  */
 public function index(Request $request)
 {
     // อ่านรายชื่อฟิลด์ของตาราง
     $rs = Recordset::create('Index\\World\\Model');
     $result = $rs->find(100);
     $fields = $rs->getFields();
     echo implode(', ', array_keys($fields)) . '<br>';
     // ลบข้อมูลทั้งตาราง
     $rs->emptyTable();
     // insert new record
     for ($i = 0; $i < 10000; $i++) {
         $query = World::create();
         $query->updated_at = Date::mktimeToSqlDateTime();
         $query->save();
     }
     // อัปเดททุก record
     $rs->updateAll(array('created_at' => Date::mktimeToSqlDateTime()));
     // อ่านจำนวนข้อมูลทั้งหมดในตาราง
     echo 'All ' . $rs->count() . ' records.<br>';
     // สุ่ม record มาแก้ไข
     for ($i = 0; $i < 5; $i++) {
         $rnd = rand(1, 10000);
         $world = $rs->find($rnd);
         $world->name = 'Hello World!';
         $world->save();
     }
     // query รายการที่มีการแก้ไข
     $rs->where(array('name', '!=', ''));
     // อ่านจำนวนข้อมูลที่พบ
     echo 'Found ' . $rs->count() . ' records.<br>';
     // แสดงผลรายการที่พบ
     foreach ($rs->all('id', 'name') as $item) {
         echo $item->id . '=' . $item->name . '<br>';
         // ลบรายการที่กำลังแสดงผล
         $item->delete();
     }
     // อ่านรายชื่อฟิลด์ของ query
     $fields = $rs->getFields();
     echo implode(', ', array_keys($fields)) . '<br>';
     // อ่านจำนวนข้อมูลที่เหลือ
     echo 'Remain ' . Recordset::create('Index\\World\\Model')->count() . ' records.<br>';
 }
コード例 #7
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);
 }
コード例 #8
0
ファイル: Recordset.php プロジェクト: goragod/kotchasan
 /**
  * INNER JOIN table ON ....
  *
  * @param string $field field class ของตารางที่ join
  * @param string $type เช่น LEFT, RIGHT, INNER...
  * @param mixed $on where condition สำหรับการ join
  * @return \static
  */
 private function doJoin($field, $type, $on)
 {
     if (preg_match('/^([a-zA-Z0-9\\\\]+)(\\s+(as|AS))?[\\s]+([A-Z0-9]{1,2})?$/', $field, $match)) {
         $field = $match[1];
     }
     $rs = new Recordset($field);
     $table = $rs->field->getTableWithAlias(isset($match[4]) ? $match[4] : null);
     $ret = $rs->buildJoin($table, $type, $on);
     if (is_array($ret)) {
         $this->sqls['join'][] = $ret[0];
         $this->values = ArrayTool::replace($this->values, $ret[1]);
     } else {
         $this->sqls['join'][] = $ret;
     }
     return $this;
 }