/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 */ public function setCounter() { $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' * @return array */ public function getFrontendMenu($position) { $models = CmsMenuFrontend::find() ->where(['position' => $position, 'status' => 1, 'parent_id' => null]) ->orderBy(['sort_order' => SORT_ASC]) ->all(); $menu = []; foreach ($models as $model) { $children = []; foreach ($model->cmsMenuFrontends as $child) { if ($child->status == 1) { $children[] = [ 'name' => $child->name, 'link' => $child->link, 'icon' => $child->icon, ]; } } // จัดการเรียงลำดับลูก (ถ้ามีฟิลด์ sort_order ในลูกด้วย แต่อิงตามความสัมพันธ์ใน Model) // ในที่นี้เราใช้ relation cmsMenuFrontends ซึ่งเราอาจจะอยาก sort มันด้วย $menu[] = [ 'name' => $model->name, 'link' => $model->link, 'icon' => $model->icon, 'items' => $children ]; } return $menu; } }