/** * ฟังก์ชั่นส่งเมล์จากแม่แบบจดหมาย * * @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); } }
/** * ฟังก์ชั่นแก้ไขข้อมูล * * @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; } }
/** * 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'))); }
/** * โหลดไฟล์ภาษาทั้งหมดที่ติดตั้ง * คืนค่าข้อมูลภาษาทั้งหมด * * @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; }
/** * ฟังก์ชั่นประมวลผลคำสั่ง 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; }
/** * ฟังก์ชั่นสร้างคำสั่ง 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; }
/** * ฟังก์ชั่นสร้างคำสั่ง 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); }
/** * 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; }