Overview

Namespaces

  • Kotchasan
    • Cache
    • Database
    • Http
    • Log
    • Orm
  • None
  • PHP
  • Psr
    • Cache
    • Http
      • Message
    • Log

Classes

  • ArrayIterator
  • DateInterval
  • DOMNode
  • Kotchasan
  • Kotchasan\Accordion
  • Kotchasan\ApiController
  • Kotchasan\ArrayTool
  • Kotchasan\Cache\ApcCache
  • Kotchasan\Cache\Cache
  • Kotchasan\Cache\CacheItem
  • Kotchasan\Cache\FileCache
  • Kotchasan\CKEditor
  • Kotchasan\Collection
  • Kotchasan\Config
  • Kotchasan\Controller
  • Kotchasan\Country
  • Kotchasan\Csv
  • Kotchasan\Curl
  • Kotchasan\Currency
  • Kotchasan\Database
  • Kotchasan\Database\Db
  • Kotchasan\Database\DbCache
  • Kotchasan\Database\Driver
  • Kotchasan\Database\PdoMysqlDriver
  • Kotchasan\Database\Query
  • Kotchasan\Database\QueryBuilder
  • Kotchasan\Database\Schema
  • Kotchasan\Database\Sql
  • Kotchasan\DataTable
  • Kotchasan\Date
  • Kotchasan\DOMNode
  • Kotchasan\DOMParser
  • Kotchasan\Email
  • Kotchasan\File
  • Kotchasan\Files
  • Kotchasan\Form
  • Kotchasan\Grid
  • Kotchasan\Html
  • Kotchasan\Htmldoc
  • Kotchasan\HtmlTable
  • Kotchasan\Http\AbstractMessage
  • Kotchasan\Http\AbstractRequest
  • Kotchasan\Http\Message
  • Kotchasan\Http\NotFound
  • Kotchasan\Http\Request
  • Kotchasan\Http\Response
  • Kotchasan\Http\Stream
  • Kotchasan\Http\UploadedFile
  • Kotchasan\Http\Uri
  • Kotchasan\Image
  • Kotchasan\InputItem
  • Kotchasan\Inputs
  • Kotchasan\KBase
  • Kotchasan\Language
  • Kotchasan\ListItem
  • Kotchasan\Log\AbstractLogger
  • Kotchasan\Log\Logger
  • Kotchasan\Login
  • Kotchasan\Menu
  • Kotchasan\Mime
  • Kotchasan\Model
  • Kotchasan\Number
  • Kotchasan\ObjectTool
  • Kotchasan\Orm\Field
  • Kotchasan\Orm\Recordset
  • Kotchasan\Password
  • Kotchasan\Pdf
  • Kotchasan\Province
  • Kotchasan\Router
  • Kotchasan\Session
  • Kotchasan\Singleton
  • Kotchasan\Tab
  • Kotchasan\TableRow
  • Kotchasan\Template
  • Kotchasan\Text
  • Kotchasan\Validator
  • Kotchasan\View
  • PHPMailer
  • Psr\Log\AbstractLogger
  • Psr\Log\LogLevel
  • Psr\Log\NullLogger
  • SMTP

Interfaces

  • ArrayAccess
  • Countable
  • DateTimeInterface
  • Iterator
  • IteratorAggregate
  • Psr\Cache\CacheItemInterface
  • Psr\Cache\CacheItemPoolInterface
  • Psr\Http\Message\MessageInterface
  • Psr\Http\Message\RequestInterface
  • Psr\Http\Message\ResponseInterface
  • Psr\Http\Message\ServerRequestInterface
  • Psr\Http\Message\StreamInterface
  • Psr\Http\Message\UploadedFileInterface
  • Psr\Http\Message\UriInterface
  • Psr\Log\LoggerAwareInterface
  • Psr\Log\LoggerInterface
  • SeekableIterator
  • Serializable
  • Traversable

Traits

  • Psr\Log\LoggerTrait

Exceptions

  • Exception
  • InvalidArgumentException
  • Kotchasan\ApiException
  • Kotchasan\Cache\Exception
  • Kotchasan\Database\Exception
  • Kotchasan\InputItemException
  • LogicException
  • phpmailerException
  • RuntimeException

Functions

  • createClass
  • debug
  • getClassPath
  • Overview
  • Namespace
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * @filesource Kotchasan/View.php
  4:  *
  5:  * @copyright 2016 Goragod.com
  6:  * @license http://www.kotchasan.com/license/
  7:  *
  8:  * @see http://www.kotchasan.com/
  9:  */
 10: 
 11: namespace Kotchasan;
 12: 
 13: /**
 14:  * View base class
 15:  *
 16:  * @author Goragod Wiriya <admin@goragod.com>
 17:  *
 18:  * @since 1.0
 19:  */
 20: class View extends \Kotchasan\KBase
 21: {
 22:     /**
 23:      * ตัวแปรเก็บเนื่อหาของเว็บไซต์ ที่จะแทนที่หลังจาก render แล้ว.
 24:      *
 25:      * @var array
 26:      */
 27:     protected $after_contents = array();
 28:     /**
 29:      * ตัวแปรเก็บเนื่อหาของเว็บไซต์.
 30:      *
 31:      * @var array
 32:      */
 33:     protected $contents = array();
 34:     /**
 35:      * รายการ header.
 36:      *
 37:      * @var array
 38:      */
 39:     protected $headers = array();
 40:     /**
 41:      * meta tag.
 42:      *
 43:      * @var array
 44:      */
 45:     protected $metas = array();
 46:     /**
 47:      * คำสั่ง Javascript ที่จะแทรกไว้ใน head.
 48:      *
 49:      * @var array
 50:      */
 51:     protected $script = array();
 52: 
 53:     /**
 54:      * ใส่ไฟล์ CSS ลงใน header.
 55:      *
 56:      * @param string $url
 57:      */
 58:     public function addCSS($url)
 59:     {
 60:         $this->metas[$url] = '<link rel=stylesheet href="'.$url.'">';
 61:     }
 62: 
 63:     /**
 64:      * ใส่ไฟล์ Javascript ลงใน header.
 65:      *
 66:      * @param string $url
 67:      */
 68:     public function addJavascript($url)
 69:     {
 70:         $this->metas[$url] = '<script src="'.$url.'"></script>';
 71:     }
 72: 
 73:     /**
 74:      * เพิ่มคำสั่ง Javascript ใส่ลงใน head ก่อนปิด head.
 75:      *
 76:      * @param string $script
 77:      */
 78:     public function addScript($script)
 79:     {
 80:         $this->script[] = $script;
 81:     }
 82: 
 83:     /**
 84:      * ฟังก์ชั่น แทนที่ query string ด้วยข้อมูลจาก GET สำหรับส่งต่อไปยัง URL ถัดไป.
 85:      * array ส่งมาจาก preg_replace
 86:      * string กำหนดเอง
 87:      * คืนค่า query string ใหม่ ลบ id=0.
 88:      *
 89:      * @assert (array(2 => 'module=retmodule&id=0')) [==] "http://localhost/?module=retmodule&amp;page=1&amp;sort=id"  [[$_SERVER['QUERY_STRING'] = '_module=test&1234&_page=1&_sort=id&action=login&id=1']]
 90:      * @assert ('module=retmodule&5678') [==] "http://localhost/?module=retmodule&amp;page=1&amp;sort=id&amp;id=1&amp;5678"
 91:      *
 92:      * @param array|string $f รับค่าจากตัวแปร $f มาสร้าง query string
 93:      *
 94:      * @return string
 95:      */
 96:     public static function back($f)
 97:     {
 98:         $uri = self::$request->getUri();
 99:         $query_url = array();
100:         foreach (explode('&', $uri->getQuery()) as $item) {
101:             if (preg_match('/^(_)?(.*)=([^$]{1,})$/', $item, $match)) {
102:                 if ($match[2] == 'action' && ($match[3] == 'login' || $match[3] == 'logout')) {
103:                     // ไม่ใช้รายการ action=login, action=logout
104:                 } else {
105:                     $query_url[$match[2]] = $match[3];
106:                 }
107:             }
108:         }
109:         if (is_array($f)) {
110:             $f = isset($f[2]) ? $f[2] : null;
111:         }
112:         if (!empty($f)) {
113:             foreach (explode('&', $f) as $item) {
114:                 if (preg_match('/^([a-zA-Z0-9_\-]+)(=(.*))?$/', $item, $match)) {
115:                     if (!isset($match[3])) {
116:                         // ไม่มี value
117:                         $query_url[$match[1]] = null;
118:                     } elseif ($match[3] === '0') {
119:                         // ไม่ใช้รายการที่หลังเครื่องหมาย = เท่ากับ 0
120:                         unset($query_url[$match[1]]);
121:                     } else {
122:                         $query_url[$match[1]] = $match[3];
123:                     }
124:                 }
125:             }
126:         }
127: 
128:         return (string) $uri->withQuery($uri->paramsToQuery($query_url, true));
129:     }
130: 
131:     /**
132:      * ส่งออกเนื้อหา และ header ตามที่กำหนด.
133:      *
134:      * @param string $content เนื้อหา
135:      */
136:     public function output($content)
137:     {
138:         // send header
139:         foreach ($this->headers as $key => $value) {
140:             header("$key: $value");
141:         }
142:         // output content
143:         echo $content;
144:     }
145: 
146:     /**
147:      * ส่งออกเป็น HTML.
148:      *
149:      * @param string|null $template HTML Template ถ้าไม่กำหนด (null) จะใช้ index.html
150:      */
151:     public function renderHTML($template = null)
152:     {
153:         // default for template
154:         $this->contents['/{WEBTITLE}/'] = self::$cfg->web_title;
155:         $this->contents['/{WEBDESCRIPTION}/'] = self::$cfg->web_description;
156:         $this->contents['/{WEBURL}/'] = WEB_URL;
157:         $this->contents['/{SKIN}/'] = Template::get();
158:         foreach ($this->after_contents as $key => $value) {
159:             $this->contents[$key] = $value;
160:         }
161:         $head = '';
162:         if (!empty($this->metas)) {
163:             $head .= implode("\n", $this->metas);
164:         }
165:         if (!empty($this->script)) {
166:             $head .= "<script>\n".implode("\n", $this->script)."\n</script>";
167:         }
168:         if ($head != '') {
169:             $this->contents['/(<head.*)(<\/head>)/isu'] = '$1'.$head.'$2';
170:         }
171:         // แทนที่ลงใน Template
172:         if ($template === null) {
173:             // ถ้าไม่ได้กำหนดมาใช้ index.html
174:             $template = Template::load('', '', 'index');
175:         }
176: 
177:         return Template::pregReplace(array_keys($this->contents), array_values($this->contents), $template);
178:     }
179: 
180:     /**
181:      * ใส่เนื้อหาลงใน $contens.
182:      *
183:      * @param array $array ชื่อที่ปรากฏใน template รูปแบบ array(key1 => val1, key2 => val2)
184:      */
185:     public function setContents($array)
186:     {
187:         foreach ($array as $key => $value) {
188:             $this->contents[$key] = $value;
189:         }
190:     }
191: 
192:     /**
193:      * ใส่เนื้อหาลงใน $contens หลัง render แล้ว.
194:      *
195:      * @param array $array ชื่อที่ปรากฏใน template รูปแบบ array(key1 => val1, key2 => val2)
196:      */
197:     public function setContentsAfter($array)
198:     {
199:         foreach ($array as $key => $value) {
200:             $this->after_contents[$key] = $value;
201:         }
202:     }
203: 
204:     /**
205:      * กำหนด header ให้กับเอกสาร.
206:      *
207:      * @param array $array
208:      */
209:     public function setHeaders($array)
210:     {
211:         foreach ($array as $key => $value) {
212:             $this->headers[$key] = $value;
213:         }
214:     }
215: 
216:     /**
217:      * ใส่ Tag ลงใน Head ของ HTML.
218:      *
219:      * @param array $array
220:      */
221:     public function setMetas($array)
222:     {
223:         foreach ($array as $key => $value) {
224:             $this->metas[$key] = $value;
225:         }
226:     }
227: 
228:     /**
229:      * อ่านค่าจาก Array
230:      * คืนค่า $source[$key]
231:      * ถ้าไม่มี คืนค่า $default
232:      *
233:      * @param array $array
234:      * @param mixed $key
235:      * @param mixed $default
236:      *
237:      * @return mixed
238:      */
239:     public static function array_value($array, $key, $default = '')
240:     {
241:         return isset($array[$key]) ? $array[$key] : $default;
242:     }
243: 
244:     /**
245:      * อ่านค่าจาก Object
246:      * คืนค่า $source->{$key}
247:      * ถ้าไม่มี คืนค่า $default
248:      *
249:      * @param array|object $source
250:      * @param string|int $key
251:      * @param string|int $default
252:      *
253:      * @return mixed
254:      */
255:     public static function object_value($source, $key, $default = '')
256:     {
257:         return isset($source->{$key}) ? $source->{$key} : $default;
258:     }
259: }
260: 
Kotchasan API documentation generated by ApiGen