/i', $content, $matches); if (empty($first_img) || !isset($matches[1][0])) { $first_img = Yii::$app->params['frontendUrl'] . '/img/nopic.png'; } else { $first_img = $matches[1][0]; } return $first_img; }*/ /** * ดึงรูปภาพแรกจาก HTML content * @param string $content * @return string */ public function getFirstImage($content) { $defaultImage = Yii::$app->params['frontendUrl'] . '/img/nopic.png'; if (empty($content)) { return $defaultImage; } preg_match('/]+src=["\']([^"\']+)["\']/i', $content, $matches); if (!empty($matches[1])) { return $matches[1]; } return $defaultImage; } /** * @param $channel * @param string $indexName * @return bool|string */ public function getRss($channel) { $returnString = ''; try { $xml = simplexml_load_file($channel); if ($xml) { $returnString .= ''; return $returnString; } else { return false; } } catch (\Exception $e) { return false; //$e->getMessage(); } } /** * @param $str * @return bool|false|string|string[]|null */ public function simpleSlug($str) { $slug = preg_replace('@[\s!:;_\?=\\\+\*/%&#]+@', '-', $str); $slug = mb_strtolower($slug, Yii::$app->charset); $slug = trim($slug, '-'); return $slug; } public function getNew($publish_at) { $date1 = date_create(date('Y-m-d')); $date2 = date_create($publish_at); $diff = date_diff($date1, $date2); //return $diff->format('%a'); if ($diff->format('%a') <= 7) { return Html::img(Yii::getAlias('@web') . '/img/new.gif', ['width' => 30]); } return ''; } /** ตัวเก็บสถิติ เหลือปีนี้กับเดือนนี้ ยัง error */ /** * ตรวจสอบว่าเป็น Bot หรือ Search Engine หรือไม่ * @return bool */ public function isBot() { $userAgent = Yii::$app->request->userAgent; if (empty($userAgent)) { return false; } $bots = [ 'googlebot', 'bingbot', 'slurp', 'duckduckbot', 'baiduspider', 'yandexbot', 'sogou', 'exabot', 'facebot', 'ia_archiver', 'facebookexternalhit', 'twitterbot', 'rogerbot', 'linkedinbot', 'embedly', 'quora link preview', 'showyoubot', 'outbrain', 'pinterest/0.', 'slackbot', 'vkShare', 'W3C_Validator', 'redditbot', 'Applebot', 'WhatsApp', 'flipboard', 'tumblr', 'bitlybot', 'SkypeShell', 'msnbot', 'crawler', 'spider', 'robot', 'bot' ]; foreach ($bots as $bot) { if (stripos($userAgent, $bot) !== false) { return true; } } return false; } public function setCounter() { // ไม่นับถ้าเป็น Bot if ($this->isBot()) { return; } $session = Yii::$app->session; if (!$session['visitor'] && $session['visitor'] != $_SERVER['REMOTE_ADDR']) { $session['visitor'] = $_SERVER['REMOTE_ADDR']; $model = Counter::findOne(['date_visit' => date('Y-m-d')]); if (!$model) { $model = new Counter(); $model->date_visit = date('Y-m-d'); $model->visit = 1; } else { $model->visit = $model->visit + 1; } $model->save(); } } public function getCounterToday() { $counter = Counter::find()->where(['date_visit' => date('Y-m-d')])->one(); return isset($counter) ? (int)$counter->visit : 0; } public function getCounterYesterday() { $counter = Counter::find()->where(['date_visit' => date('Y-m-d', strtotime('Yesterday'))])->one(); return isset($counter) ? (int)$counter->visit : 0; } public function getCounterThisMonth() { $firstDay = date('Y-m-01'); $lastDay = date('Y-m-t'); return (int)Counter::find() ->where(['between', 'date_visit', $firstDay, $lastDay]) ->sum('visit'); } public function getCounterLastMonth() { $firstDay = date('Y-m-01', strtotime('-1 month')); $lastDay = date('Y-m-t', strtotime('-1 month')); return (int)Counter::find() ->where(['between', 'date_visit', $firstDay, $lastDay]) ->sum('visit'); } public function getCounterThisYear() { $firstDay = date('Y-01-01'); $lastDay = date('Y-12-31'); return (int)Counter::find() ->where(['between', 'date_visit', $firstDay, $lastDay]) ->sum('visit'); } public function getCounterLastYear() { $firstDay = date('Y-01-01', strtotime('-1 year')); $lastDay = date('Y-12-31', strtotime('-1 year')); return (int)Counter::find() ->where(['between', 'date_visit', $firstDay, $lastDay]) ->sum('visit'); } public function getCounterTotal() { return (int)Counter::find()->sum('visit'); } /** * ดึงเมนูหน้าบ้านตามตำแหน่ง (รองรับหลายชั้น) * @param string $position 'navbar' หรือ 'left_menu' * @param int|null $parentId * @return array */ public function getFrontendMenu($position, $parentId = null) { $models = CmsMenuFrontend::find() ->where(['position' => $position, 'status' => 1, 'parent_id' => $parentId]) ->orderBy(['sort_order' => SORT_ASC]) ->all(); $menu = []; foreach ($models as $model) { $menu[] = [ 'name' => $model->name, 'link' => $model->link, 'icon' => $model->icon, 'items' => $this->getFrontendMenu($position, $model->id) // Recursive call ]; } return $menu; } }