public function a($_act = null)
 {
     switch ($_act) {
         case 'news':
             if (role_check()) {
                 if (is_post()) {
                     $news = new \app\models\News();
                     $this->loadHelper('Validator');
                     if ($news->countNews() < 15) {
                         $data = ['title' => strip_tags(validate('required', 'title')), 'content' => validate('required', 'content')];
                         if (validator($data)) {
                             $news->createNews($data);
                             cache_forgot('p.getJson.news.' . user('major'));
                         }
                     }
                     return redirect('');
                 } else {
                     return $this->view('createNews');
                 }
             }
             return redirect('back');
             break;
         case 'know':
             if (is_post()) {
                 $this->loadHelper('Validator');
                 $id = validate('required', 'token');
                 if (!is_null($id)) {
                     $id = base64_decode($id);
                     if (preg_match('/^[0-9]+$/', $id)) {
                         $data = cache_remember('p.n.' . $id, function () use($id) {
                             $news = new \app\models\News();
                             $data = $news->getNews($id);
                             if (!empty($data)) {
                                 return $data;
                             }
                             return false;
                         });
                         if (!in_array(auth('id'), explode(',', $data['readIn']))) {
                             $news = new \app\models\News();
                             $progress = $data['readIn'] === '' ? auth('id') . ',' : $data['readIn'] . auth('id') . ',';
                             if ($news->addKnow($id, ['readIn' => $progress])) {
                                 echo '1';
                                 cache_forgot('p.n.' . $id);
                                 cache_forgot('p.getJson.news.' . user('major'));
                                 return;
                             }
                         }
                     }
                 }
             }
             echo '0';
             return;
             break;
     }
 }
 public function run()
 {
     if (role_check(0)) {
         $data = cache_remember('admin.major.list', function () {
             $major = new \app\models\Faculty();
             return $major->getAllMajor();
         });
         return $this->view('layouts/loginbox', compact('data'));
     }
     return $this->view('layouts/loginbox');
 }
 public function pictures($action = null)
 {
     $_path = APP_PATH . 'contents/major/' . user('major') . '/';
     if (role_check()) {
         switch ($action) {
             case 'remove-all':
                 if (chdir($_path)) {
                     $files = glob('*.*');
                     foreach ($files as $key => $value) {
                         unlink($value);
                         unlink('thumbs/' . preg_replace('/\\.[A-z]+/', '', $value) . '.jpg');
                         echo "Delete {$value} success. <br/>";
                     }
                 }
                 break;
             case 'remove':
                 if (is_post()) {
                     if (chdir($_path)) {
                         if (unlink($_POST['pic']) && unlink('thumbs/' . $_POST['name'] . '.jpg')) {
                             echo '1';
                         }
                     }
                 }
                 break;
             default:
                 if (!file_exists($_path)) {
                     mkdir($_path, 0777, true);
                     mkdir($_path . 'thumbs', 0777, true);
                 }
                 if (chdir($_path)) {
                     $files = @glob('*.*', GLOB_BRACE);
                     $data = array();
                     foreach ($files as $key => $file) {
                         $data[$key] = array('name' => $file, 'size' => filesize($file));
                     }
                     unset($files);
                     $json = json_encode($data);
                     if (chdir(APP_PATH)) {
                         return $this->view('major/pictures', compact('json'));
                     }
                 }
                 break;
         }
     }
 }
 public function e($select)
 {
     switch ($select) {
         case 'avatar':
             $this->eAvatar();
             cache_forgot('user.members.' . user('major'));
             break;
         case 'cover':
             $this->eCover();
             break;
         case 'user_detail':
             $this->eDetail();
             break;
         case 'chpass':
             $this->updatePass();
             break;
         case 'allow':
             if (role_check(1)) {
                 if (preg_match('/^[0-9]+$/', $_POST['id'])) {
                     $user = new User();
                     $user->uActive($_POST['id']);
                     cache_forgot('user.i.' . $_POST['id']);
                     cache_forgot('user.inactive.' . $_POST['id']);
                     cache_forgot('user.members.' . user('major'));
                 }
             }
             break;
         case 'deny':
             if (role_check(1) && $_GET['id'] !== auth('id')) {
                 if (preg_match('/^[0-9]+$/', $_GET['id'])) {
                     $user = new User();
                     $user->delUser($_GET['id']);
                     cache_forgot('user.i.' . $_GET['id']);
                 }
             }
             break;
     }
 }
    echo url('user/i/' . $value['id']);
    ?>
"><?php 
    echo $value['firstname'];
    ?>
</a>
						<br/><?php 
    echo $value['nickname'];
    ?>
 ( <?php 
    echo position($value['role_id']);
    ?>
 )
						<br/>
				<?php 
    if (role_check(1)) {
        ?>
					<?php 
        if ($value['active'] === '1') {
            ?>
						<span class="label label-success">ยืนยันแล้ว</span>
					<?php 
        } else {
            ?>
						<span class="label label-danger">ยังไม่ได้ยืนยัน</span>
					<?php 
        }
        ?>
				<?php 
    }
    ?>
<?php 
} else {
    ?>
	<img class="avatar img-circle" src="<?php 
    echo content('users/avatars/' . user('avatar'));
    ?>
" style="width: 140px; height: 140px;">
<?php 
}
?>
	<br /><?php 
echo auth('username');
?>
	<br /><?php 
echo auth('email');
if (role_check(0)) {
    ?>
	<hr />
	<p>
		<select id="ch_major" class="form-control input-sm">
		<?php 
    foreach ($data as $value) {
        ?>
			<option value="<?php 
        echo $value['id'];
        ?>
" <?php 
        echo user('major') === $value['id'] ? 'selected' : '';
        ?>
><?php 
        echo $value['name'];
				}
				function getNews(data) {
					var newsEl = $('#news');
					var news_countEl = $('#news_count');
					newsEl.fadeOut(function() {
						newsEl.empty();
						news_countEl.empty();
						news_countEl.html('<kbd>' + data.length + ' / 15</kbd><code>' + text1 + '</code>')
						if (data.length !== 0) {
							$.each(data, function(index, value) {
								var date = new Date(value.updated_at).toString().replace(/ ([0-9]+):([0-9]+):([0-9]+)(.*)/g, '');
								var read = '<span class="label label-danger">ยังไม่ได้อ่าน</span> ';
								if ($.inArray(user_id, value.readIn.split(',')) >= 0)
									read = '<span class="label label-success">อ่านแล้ว</span> ';
							<?php 
    if (role_check()) {
        ?>
								var html = '<div class="list-group-item"><div class="row"><a href="' + url + 'p/n/' + value.id + '-' + (value.title).replace(/<[A-Za-z\=\.\ \"\-]+>.*<\/[A-Za-z]+>/g,'').replace(' ', '-') + '" style="color:#666;"><div class="col-xs-8"><b>' + value.title + '</b><br/>' + read + date + '</div></a><div class="col-xs-4 text-right"><a href="' + url + 'p/e/news/' + value.id + '" class="btn btn-sm btn-default"><i class="glyphicon glyphicon-edit"></i></a> <a href="javascript:void(0);" onclick="del(' + value.id + ');" class="btn btn-sm btn-danger"><i class="glyphicon glyphicon-remove-sign"></i></a></div></div></div>';
							<?php 
    } else {
        ?>
								var html = '<a href="' + url + 'p/n/' + value.id + '-' + (value.title).replace(' ', '-') + '" class="list-group-item"><b>' + value.title + '</b><br/>' + read + date + '</a>';
							<?php 
    }
    ?>
								newsEl.append(html);
							});
						} else {
							var html = '<div class="list-group-item">ยังไม่มีข่าวใดๆ ในขณะนี้</div>';
							newsEl.append(html);
						}
 public function upload()
 {
     $this->middleware('Auth');
     $this->loadHelper('User');
     if (role_check() && is_post()) {
         if (!empty($_FILES['image'])) {
             $_path = APP_PATH . 'contents/major/' . user('major') . '/';
             if (!file_exists($_path)) {
                 mkdir($_path, 0755, true);
                 mkdir($_path . 'thumbs', 0755, true);
             }
             $getSize = @glob($_path . '*.*', GLOB_BRACE);
             $size = (int) 0;
             foreach ($getSize as $list) {
                 $size = $size + filesize($list);
             }
             unset($getSize, $list);
             $size = round($size / 1000 / 1000);
             if ($size <= 50) {
                 $file = $_FILES['image']['tmp_name'];
                 $filesize = filesize($file) / 1000;
                 if ($filesize <= 1024) {
                     $thumbs_width = 125;
                     $thumbs_height = 125;
                     $mime = @getimagesize($file);
                     switch ($mime['mime']) {
                         case 'image/jpeg':
                             $image = imagecreatefromjpeg($file);
                             $ext = '.jpg';
                             break;
                         case 'image/png':
                             $image = imagecreatefrompng($file);
                             $ext = '.png';
                             break;
                         case 'image/bmp':
                             $image = imagecreatefromwbmp($file);
                             $ext = '.bmp';
                             break;
                         case 'image/gif':
                             $image = imagecreatefromgif($file);
                             $ext = '.gif';
                             break;
                         default:
                             exit(' "{code": 0,"data":"Errors." }');
                             break;
                     }
                     $thumbs = ImageCreateTrueColor($thumbs_width, $thumbs_height);
                     $dst_ratio = $mime['0'] / $mime['1'];
                     $img_ratio = $thumbs_width / $thumbs_height;
                     if ($dst_ratio >= $img_ratio) {
                         $dst_h = $mime['1'];
                         $dst_w = $dst_h / $img_ratio;
                         $dst_x = ($mime['0'] - $dst_w) / 2;
                         $dst_y = 0;
                     } else {
                         $dst_w = $mime['0'];
                         $dst_h = $dst_w / $img_ratio;
                         $dst_x = 0;
                         $dst_y = ($mime['1'] - $dst_h) / 2;
                     }
                     $img_name = str_rand(10);
                     $thumbs_path = APP_PATH . 'contents/major/' . user('major') . '/thumbs/' . $img_name . '.jpg';
                     $image_path = 'major/' . user('major') . '/' . $img_name . $ext;
                     imagecopyresampled($thumbs, $image, 0, 0, $dst_x, $dst_y, $thumbs_width, $thumbs_height, $dst_w, $dst_h);
                     imagejpeg($thumbs, $thumbs_path, 70);
                     move_uploaded_file($file, APP_PATH . 'contents/' . $image_path);
                     imagedestroy($thumbs);
                     imagedestroy($image);
                     echo '{ "code": 1,"data":"' . content($image_path) . '" }';
                     exit;
                 }
                 echo '{ "code": 2,"data":"ขนาดไฟล์ใหญ่เกิน 1 Mb" }';
                 exit;
             }
             echo '{ "code": 3,"data":"พื้นที่เก็บข้อมูลเต็ม" }';
             exit;
         }
     }
     echo '{ "code": 0,"data":"Errors." }';
 }