Example #1
0
 /**
  * ฟังก์ชั่นส่งเมล์จากแม่แบบจดหมาย
  *
  * @param int $id ID ของจดหมายที่ต้องการส่ง
  * @param string $module ชื่อโมดูลของจดหมายที่ต้องการส่ง
  * @param array $datas ข้อมูลที่จะถูกแทนที่ลงในจดหมาย ในรูป 'ตัวแปร'=>'ข้อความ'
  * @param string $to ที่อยู่อีเมล์ผู้รับ  คั่นแต่ละรายชื่อด้วย ,
  * @return string สำเร็จคืนค่าว่าง ไม่สำเร็จ คืนค่าข้อความผิดพลาด
  */
 public static function send($id, $module, $datas, $to)
 {
     $model = new static();
     $sql = "SELECT `from_email`,`copy_to`,`subject`,`detail` FROM `" . $model->tableWithPrefix('emailtemplate') . "`";
     $sql .= " WHERE `module`=:module AND `email_id`=:email_id AND `language` IN (:language,'th')";
     $sql .= " LIMIT 1";
     $where = array(':module' => $module, ':email_id' => (int) $id, ':language' => \Language::name());
     $email = $model->db->customQuery($sql, true, $where, $model->cache);
     if (empty($email)) {
         return \Language::get('email template not found');
     } else {
         $email = $email[0];
         // ผู้ส่ง
         $from = empty($email['from_email']) ? self::$cfg->noreply_email : $email['from_email'];
         // ข้อความในอีเมล์
         $replace = array('/%WEBTITLE%/' => strip_tags(self::$cfg->web_title), '/%WEBURL%/' => WEB_URL, '/%ADMINEMAIL%/' => $from, '/%TIME%/' => \Date::format());
         $replace = \Arraytool::replace($replace, $datas);
         \Arraytool::extract($replace, $keys, $values);
         $msg = preg_replace($keys, $values, $email['detail']);
         $subject = preg_replace($keys, $values, $email['subject']);
         $to = explode(',', $to);
         if (!empty($email['copy_to'])) {
             $to[] = $email['copy_to'];
         }
         // ส่งอีเมล์
         return self::custom(implode(',', $to), $from, $subject, $msg);
     }
 }
Example #2
0
 /**
  * แทนที่ข้อความด้วยข้อมูลจากแอเรย์ รองรับข้อมูลรูปแบบแอเรย์ย่อยๆ
  *
  * @param string $text
  * @param array $array array($key1 => $value1, $key2 => $value2, array($key3 => $value3, $key4 => $value4))
  * @return string
  */
 public static function replaceAll($text, $array)
 {
     if (!empty($array)) {
         $keys = array();
         $values = array();
         \Arraytool::extract($array, $keys, $values);
         $text = str_replace($keys, $values, $text);
     }
     return $text;
 }
Example #3
0
 /**
  * สร้างตาราง และเริ่มต้นทำงานตาราง
  * คืนค่าเป็นโค้ด HTML ของ Datatable
  *
  * @return string
  */
 public function render()
 {
     $url_query = array();
     $hidden_fields = array();
     foreach ($_GET as $key => $value) {
         $value = rawurlencode($value);
         $url_query[$key] = $key . '=' . $value;
         // แอเรย์เก็บรายการ input ที่ไม่ต้องสร้าง
         if ($key !== 'search' && $key !== 'count' && $key !== 'page') {
             $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']]);
         // ไม่ Query รายการที่เป็นค่าว่าง
         if (isset($items['value']) && $items['value'] != '' && !empty($items['options'])) {
             if (in_array($items['value'], array_keys($items['options']))) {
                 $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 = \Input::text($_REQUEST, 'search');
     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 (isset($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, \Input::get($_REQUEST, 'page', 1));
         // ตรวจสอบหน้าที่เลือกสูงสุด
         $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 (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);
     // เรียงลำดับ
     $this->sort = \Input::get($_REQUEST, 'sort', $this->sort);
     if (!empty($this->sort)) {
         if (in_array($this->sort, array_keys($this->columns))) {
             $this->sortType = \Input::get($_REQUEST, 'sort_type', $this->sortType);
             $this->sortType = $this->sortType == 'desc' ? 'desc' : 'asc';
             if (isset($this->model)) {
                 $sort = isset($this->headers[$this->sort]['sort']) ? $this->headers[$this->sort]['sort'] : $this->sort;
                 $query->order($sort . ' ' . $this->sortType);
             } elseif (isset($this->datas)) {
                 \Arraytool::sort($this->datas, $this->sort, $this->sortType == 'asc');
             }
         }
     }
     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;
     }
     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) . '>';
         $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++;
                 } elseif ($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
         $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)) {
             if (preg_match('/^((.*)\\s+)?(icon-[a-z0-9\\-_]+)(\\s+(.*))?$/', $this->addNew['class'], $match)) {
                 $match[2] = trim($match[2] . ' ' . (isset($match[5]) ? $match[5] : ''));
                 $table_nav[] = '<a class="' . $match[2] . '" href="' . $this->addNew['href'] . '"><span class="' . $match[3] . '">' . $this->addNew['text'] . '</span></a>';
             } else {
                 $table_nav[] = '<a class="' . $this->addNew['class'] . '" href="' . $this->addNew['href'] . '">' . $this->addNew['text'] . '</a>';
             }
         }
         if (!empty($table_nav)) {
             $content[] = '<div class="table_nav action">' . implode('', $table_nav) . '</div>';
         }
         // แบ่งหน้า
         if (!empty($this->perPage)) {
             $url_query['page'] = 'page=:page';
             $url = '?' . implode('&amp;', $url_query);
             $content[] = '<div class="splitpage">' . \Url::pagination($totalpage, $page, $url) . '</div>';
         }
     }
     $content[] = '</div>';
     // javascript ของ ตาราง
     $javascript = array();
     $script = array('page' => $page, 'search' => $search, 'sort' => $this->sort, 'sort_type' => $this->sortType, 'action' => $this->action, 'actionCallback' => $this->actionCallback, 'actionConfirm' => $this->actionConfirm, 'pmButton' => $this->pmButton, 'dragColumn' => $this->dragColumn);
     $javascript[] = 'var table = new GTable("' . $this->id . '", ' . json_encode($script) . ');';
     $content[] = "<script>\n" . implode("\n", $javascript) . "\n</script>";
     return implode("\n", $content);
 }
Example #4
0
 /**
  * ฟังก์ชั่นแก้ไขข้อมูล
  *
  * @param string $table ชื่อตาราง
  * @param mixed $condition query WHERE
  * @param array $recArr ข้อมูลที่ต้องการบันทึก รูปแบบ array('key1'=>'value1', 'key2'=>'value2', ...)
  * @return boolean สำเร็จ คืนค่า true, ผิดพลาด คืนค่า false
  */
 public function update($table, $condition, $recArr)
 {
     $sets = array();
     $values = array();
     foreach ($recArr as $key => $value) {
         $sets[] = '`' . $key . '` = :' . $key;
         $values[':' . $key] = $value;
     }
     $condition = $this->buildWhere($condition);
     if (is_array($condition)) {
         $values = \Arraytool::replace($values, $condition[1]);
         $condition = $condition[0];
     }
     $sql = 'UPDATE `' . $table . '` SET ' . implode(', ', $sets) . ' WHERE ' . $condition;
     try {
         $query = $this->connection->prepare($sql);
         $query->execute($values);
         self::$query_count++;
         return true;
     } catch (PDOException $e) {
         $this->sendError($sql, $e->getMessage());
         return false;
     }
 }
Example #5
0
 /**
  * Generated from @assert (array(1 => 1, 2 => 2, 3 => 'three'), array(1 => 'one', 2 => 'two')) [==] array(1 => 'one', 2 => 'two', 3 => 'three').
  *
  * @covers Arraytool::replace
  */
 public function testReplace()
 {
     $this->assertEquals(array(1 => 'one', 2 => 'two', 3 => 'three'), \Arraytool::replace(array(1 => 1, 2 => 2, 3 => 'three'), array(1 => 'one', 2 => 'two')));
 }
Example #6
0
 /**
  * โหลดไฟล์ภาษาทั้งหมดที่ติดตั้ง
  * คืนค่าข้อมูลภาษาทั้งหมด
  *
  * @return array
  */
 public static function installed($type)
 {
     $language_folder = self::languageFolder();
     $datas = array();
     foreach (self::installedLanguage() as $lng) {
         if ($type == 'php') {
             if (is_file($language_folder . $lng . '.php')) {
                 // php
                 $datas[$lng] = (include $language_folder . $lng . '.php');
             }
         } elseif (is_file($language_folder . $lng . '.js')) {
             // js
             $list = file($language_folder . $lng . '.js');
             foreach ($list as $item) {
                 if (preg_match('/var\\s+(.*)\\s+=\\s+[\'"](.*)[\'"];/', $item, $values)) {
                     $datas[$lng][$values[1]] = $values[2];
                 }
             }
         }
     }
     // จัดกลุ่มภาษาตาม key
     $languages = array();
     foreach ($datas as $language => $values) {
         foreach ($values as $key => $value) {
             $languages[$key][$language] = $value;
             if (is_array($value)) {
                 $languages[$key]['array'] = true;
             }
         }
     }
     // จัดกลุามภาษาตาม id
     $datas = array();
     $i = 0;
     foreach ($languages as $key => $row) {
         $datas[$i] = \Arraytool::replace(array('id' => $i, 'key' => $key), $row);
         $i++;
     }
     return $datas;
 }
Example #7
0
 /**
  * ฟังก์ชั่นประมวลผลคำสั่ง SQL จาก query builder
  *
  * @return boolean|array
  */
 public function execQuery($sqls, $values = array(), $cache = null)
 {
     $sql = $this->makeQuery($sqls);
     if (isset($sqls['values'])) {
         $values = \Arraytool::replace($sqls['values'], $values);
     }
     if ($sqls['function'] == 'customQuery') {
         $result = $this->customQuery($sql, true, $values, $cache);
     } else {
         $result = $this->query($sql, $values);
     }
     return $result;
 }
Example #8
0
 /**
  * ฟังก์ชั่นสร้างคำสั่ง WHERE
  *
  * @param mixed $condition query string หรือ array
  * @assert where(1)->text() [==] " WHERE `id`=1"
  * @assert where(array('id', 1))->text() [==] " WHERE `id`=1"
  * @assert where(array('id', '1'))->text() [==] " WHERE `id`='1'"
  * @assert where(array('date', '2015-1-1 30:30'))->text() [==] " WHERE `date`='2015-1-1 30:30'"
  * @assert where(array('id', '=', 1))->text() [==] " WHERE `id`=1"
  * @assert where('`id`=1 OR (SELECT ....)')->text() [==] " WHERE `id`=1 OR (SELECT ....)"
  * @assert where(array('id', '=', 1))->text() [==] " WHERE `id`=1"
  * @assert where(array('id', 'IN', array(1, 2, '3')))->text() [==] " WHERE `id` IN (:id0, :id1, :id2)"
  * @return \Core\Database\QueryBuilder
  */
 public function where($condition, $oprator = 'AND', $id = 'id')
 {
     $ret = $this->buildWhere($condition, $oprator, $id);
     if (is_array($ret)) {
         $this->sqls['where'] = $ret[0];
         $this->values = \Arraytool::replace($this->values, $ret[1]);
     } else {
         $this->sqls['where'] = $ret;
     }
     return $this;
 }
Example #9
0
 /**
  * ฟังก์ชั่นสร้างคำสั่ง WHERE และ values ไม่ใส่ alias ให้กับชื่อฟิลด์
  *
  * @param mixed $condition
  * @param string $oprator (optional) เช่น AND หรือ OR
  * @param string $id (optional )ชื่อฟิลด์ที่เป็น key
  * @assert (1) [==] array("`id` = :id", array(':id' => 1))
  * @assert ('string') [==] array("string", array())
  * @assert (array('user_id', 1)) [==] array("`user_id` = :user_id", array(':user_id' => 1))
  * @assert (array(array('id', 1), array('id', array(1, 2, '3')))) [==] array("`id` = :id AND `id` IN (:id0,:id1,:id2)", array(':id0' => 1, ':id1' => 2, ':id2' => '3', ':id' => 1))
  * @assert (array('(...)')) [==] array('(...)', array())
  * @return array ($condition, $values)
  */
 protected function buildWhereValues($condition, $oprator = 'AND', $id = 'id')
 {
     if (is_array($condition)) {
         $values = array();
         $qs = array();
         if (is_array($condition[0])) {
             foreach ($condition as $item) {
                 $ret = $this->buildWhereValues($item, $oprator, $id);
                 $qs[] = $ret[0];
                 $values = \Arraytool::replace($values, $ret[1]);
             }
             $condition = implode(' ' . $oprator . ' ', $qs);
         } elseif (strpos($condition[0], '(') !== false) {
             $condition = $condition[0];
         } else {
             if (sizeof($condition) == 2) {
                 $condition = array($condition[0], '=', $condition[1]);
             } else {
                 $condition[1] = strtoupper(trim($condition[1]));
             }
             if (is_array($condition[2])) {
                 $operator = $condition[1] == '=' ? 'IN' : $condition[1];
                 $qs = array();
                 foreach ($condition[2] as $k => $v) {
                     $qs[] = ":{$condition['0']}{$k}";
                     $values[":{$condition['0']}{$k}"] = $v;
                 }
                 $condition = "`{$condition['0']}` {$operator} (" . implode(',', $qs) . ")";
             } else {
                 $values[":{$condition['0']}"] = $condition[2];
                 $condition = "`{$condition['0']}` {$condition['1']} :{$condition['0']}";
             }
         }
     } elseif (is_int($condition)) {
         // primaryKey
         $values = array(":{$id}" => $condition);
         $condition = "`{$id}` = :{$id}";
     } else {
         $values = array();
     }
     return array($condition, $values);
 }
Example #10
0
 /**
  * WHERE ....
  * int ค้นหาจาก primaryKey เช่น id=1 หมายถึง WHERE `id`=1
  * string เช่น QUERY ต่างๆ `email`='xxx.com' หมายถึง WHERE `email`='xxx.com'
  * array เช่น ('id', 1) หมายถึง WHERE `id`=1
  * array เช่น ('email', '!=', 'xxx.com') หมายถึง WHERE `email`!='xxx.com'
  * ถ้าเป็น array สามารถรุบได้หลายค่าโดยแต่ละค่าจะเชื่อมด้วย $oprator
  *
  * @param mixed $where
  * @param string $oprator (options) AND (default), OR
  * @return \Core\Orm\Recordset
  */
 public function where($where = array(), $oprator = 'AND')
 {
     if (is_string($where) && $where != '' || !empty($where)) {
         $where = $this->buildWhere($where, $oprator, $this->table_alias . '.' . $this->model->getAttribute('primaryKey'));
         if (is_array($where)) {
             $this->values = \Arraytool::replace($this->values, $where[1]);
             $where = $where[0];
         }
         $this->sqls['where'] = $where;
     }
     return $this;
 }