public function actionCreateImgAjax() { if (!empty($_FILES)) { $imageType = array('.gif', '.jpg', '.jpeg', '.png'); if (!in_array(strrchr(strtolower($_FILES['imgFile']['name']), '.'), $imageType)) { echo Json::encode(['error' => 1, 'message' => Yii::t('app', "that's not an image, only allow '.gif', '.jpg', '.jpeg', '.png'")]); Yii::$app->end(); } $dir = BASE_PATH . '/upload/post/' . date('Ym') . '/'; if (!is_dir($dir)) { @mkdir(dirname($dir), 0777); @mkdir($dir, 0777); touch($dir . '/index.html'); } $name = date('His') . strtolower(Common::random(16)) . strrchr($_FILES['imgFile']['name'], '.'); $tmp_name = $_FILES['imgFile']['tmp_name']; move_uploaded_file($tmp_name, $dir . $name); $url = Yii::$app->homeUrl . 'upload/post/' . date('Ym') . '/' . $name; $name = $_FILES['imgFile']['name']; $size = $_FILES['imgFile']['size']; echo Json::encode(['error' => 0, 'url' => $url]); } else { echo Json::encode(['error' => 1, 'message' => Yii::t('app', "upload error")]); } Yii::$app->end(); }
public function beforeAction($action) { if (parent::beforeAction($action)) { if (!in_array(Yii::$app->user->identity->username, Yii::$app->params['admin']) || !$this->checkAccess()) { throw new ForbiddenHttpException(Yii::t('app', 'You are not allowed to access this page.')); } if (!Common::getLanguage()) { preg_match('/^([a-z\\-]+)/i', $_SERVER['HTTP_ACCEPT_LANGUAGE'], $matches); Common::setLanguage($matches[1]); Yii::$app->language = $matches[1]; } else { Yii::$app->language = Common::getLanguage(); } return true; } else { return false; } }
public function getAddressStr() { $addr = []; if ($this->city) { $addr[] = Common::fmtCity($this->city); } if ($this->street) { $addr[] = Common::fmtStreet($this->street); } if ($this->home) { $addr[] = Common::fmtHome($this->home); } if ($this->housing) { $addr[] = Common::fmtHousing($this->housing); } if ($this->building) { $addr[] = Common::fmtBuilding($this->building); } return implode(', ', $addr); }
function Comments($items, $pages, $id) { foreach ($items as $key => $value) { if ($key >= $pages->offset && $key < $pages->offset + $pages->limit) { if ($value->parent_id == 0) { if (isset($value->user)) { $username = $value->user->username; $avatar = $value->user->avatar; $url = Url::to(['user/view', 'id' => $value->user_id]); } else { $username = $value->author ? $value->author : "游客"; $avatar = Yii::$app->homeUrl . "upload/avatar/default.png"; $url = "javascript:;"; } echo '<li class="media"> <a class="pull-left" href="' . $url . '"> <img class="media-object img-circle" alt="' . Html::encode($username) . '" src="' . $avatar . '" style="width: 48px; height: 48px;"> </a> <div class="media-body"> <h4 class="media-heading"><a href="' . $url . '">' . Html::encode($username) . '</a> • <span title="' . date("Y-m-d H:i:s", $value->create_time) . '">' . Common::formatTime($value->create_time) . '</span></h4> <p>' . Html::encode($value->content) . '</p><div class="ops"><a href="" class="comment-up" data-id="' . $value->id . '"><i class="glyphicon glyphicon-thumbs-up"></i> (<span>' . $value->up . '</span>)</a> <a href="" class="comment-down" data-id="' . $value->id . '"><i class="glyphicon glyphicon-thumbs-down"></i> (<span>' . $value->down . '</span>)</a> <a href="" class="comment-reply" data-id="' . $value->id . '" data-postid="' . $id . '" title="回复"><i class="glyphicon glyphicon-share-alt"></i></a></div>'; renderItems($items, $value, $id); echo "</li>"; } } } }
public function actionView($id) { $model = $this->findModel($id); $userform = new UserForm(); $userform->username = $model->username; $userform->email = $model->email; if ($userform->load(Yii::$app->request->post()) && $userform->validate()) { $types = ['.gif', '.jpg', '.png']; $image = UploadedFile::getInstance($userform, 'avatar'); if (!empty($image->name) && in_array(strrchr(strtolower($image->name), '.'), $types)) { $dir = BASE_PATH . '/upload/avatar/'; if (!is_dir($dir)) { @mkdir($dir, 0777); touch($dir . '/index.html'); } $name = date('His') . strtolower(Common::random(16)) . strrchr($image->name, '.'); $image->saveAs($dir . $name); $model->avatar = Yii::$app->homeUrl . 'upload/avatar/' . $name; } if (!empty($userform->username)) { $model->username = $userform->username; } if (!empty($userform->password)) { $model->password = $userform->password; } if (!empty($userform->email)) { $model->email = $userform->email; } if ($model->save()) { Yii::$app->getSession()->setFlash('success', '保存成功.'); } else { Yii::$app->getSession()->setFlash('danger', '保存失败.'); } return $this->redirect(['view', 'id' => $model->id]); } else { return $this->render('view', ['model' => $model, 'userform' => $userform]); } }
public function actionLoadImage($id) { foreach ($_FILES as $file) { $fileTmpName = $file['tmp_name']; $fileName = Common::transl($file['name']); $fileRPath = '/data/' . $id . '/'; $fileFPath = Yii::$app->basePath . '/web' . $fileRPath; } if (!is_writable($fileFPath)) { chmod($fileFPath, 0777); } $mv = move_uploaded_file($fileTmpName, $fileFPath . $fileName); if ($mv) { $imagine = new Imagine(); $image = $imagine->open($fileFPath . $fileName); $box = new Box(700, 420); $image->resize($box)->save(); } Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; $status = $mv ? 'OK' : 'ERROR'; $responce = ['status' => $status, 'filename' => $fileRPath . $fileName]; return $responce; }
public function actionCommentAjax($id) { $model = new Comment(); if ($model->load(Yii::$app->request->post())) { if (Yii::$app->params['commentNeedApproval']) { $model->type = 0; } else { $model->type = 1; } $model->status = 1; $model->post_id = $id; if ($model->save()) { if (isset($model->user)) { $username = $model->user->username; $avatar = $model->user->avatar; $url = Url::to(['user/view', 'id' => $model->user_id]); } else { $username = $model->author ? $model->author : "游客"; $avatar = Yii::$app->homeUrl . "upload/avatar/default.png"; $url = "javascript:;"; } if ($model->parent_id) { $li = '<li class="media">'; $endli = '</li>'; } else { $li = ''; $endli = ''; } echo Json::encode($li . '<div class="media"><a class="pull-left" href="' . $url . '"> <img class="media-object img-circle" alt="' . Html::encode($username) . '" src="' . $avatar . '" style="width: 48px; height: 48px;"> </a> <div class="media-body"> <h4 class="media-heading"><a href="' . $url . '">' . Html::encode($username) . '</a> • <span title="' . date("Y-m-d H:i:s", $model->create_time) . '">' . Common::formatTime($model->create_time) . '</span></h4> <p>' . Html::encode($model->content) . '</p><div class="ops"><a href="" class="comment-up" data-id="' . $model->id . '"><i class="glyphicon glyphicon-thumbs-up"></i> (<span>0</span>)</a> <a href="" class="comment-down" data-id="' . $model->id . '"><i class="glyphicon glyphicon-thumbs-down"></i> (<span>0</span>)</a> <a href="" class="comment-reply" data-id="' . $model->id . '" data-postid="' . $id . '" title="回复"><i class="glyphicon glyphicon-share-alt"></i></a></div>' . $endli); } else { echo "0"; } } else { echo "0"; } Yii::$app->end(); }
public function actionLocale($language) { Common::setLanguage($language); return $this->redirect(['index']); }
/** * Updates an existing Link model. * If update is successful, the browser will be redirected to the 'view' page. * @param integer $id * @return mixed */ public function actionUpdate($id) { $model = $this->findModel($id); if ($model->load(Yii::$app->request->post())) { $image = UploadedFile::getInstance($model, 'logo'); if (!empty($image->name)) { $dir = BASE_PATH . '/upload/link/'; if (!is_dir($dir)) { @mkdir(dirname($dir), 0777); @mkdir($dir, 0777); touch($dir . '/index.html'); } $name = date('His') . strtolower(Common::random(16)) . strrchr($image->name, '.'); $image->saveAs($dir . $name); $model->logo = 'upload/link/' . $name; } if ($model->save()) { return $this->redirect(['view', 'id' => $model->id]); } } else { return $this->render('update', ['model' => $model]); } }
public function getMakeTime() { return Common::formatTime($this->published_at); }
if ($canEdit || $canDelete) { ?> <div class="blog-entry-admin"> <?php if ($canEdit) { $options = ['title' => Yii::t('yii', 'Update'), 'aria-label' => Yii::t('yii', 'Update'), 'data-pjax' => '0']; echo Html::a('<i class="fa fa-edit"></i>', '/post/update/' . $model->id, $options); } if ($canDelete) { $options = ['title' => Yii::t('yii', 'Delete'), 'aria-label' => Yii::t('yii', 'Delete'), 'data-confirm' => Yii::t('yii', 'Are you sure you want to delete this item?'), 'data-method' => 'post', 'data-pjax' => '0', 'class' => 'blog-entry-admin-delete']; echo Html::a('<i class="fa fa-remove"></i>', '/post/delete/' . $model->id, $options); } ?> </div> <?php } ?> </div> </header> <div class="post-content"> <p> <?php echo Common::substrBoundary($model->content, 600); ?> </p> </div> <footer class="post-footer"> <a class="btn-small btn-color">Read More</a> </footer> </article> <div class="blog-divider"></div>
$menuItems[] = ['label' => \Yii::t('app', 'Logout') . ' (' . Yii::$app->user->identity->username . ')', 'url' => ['/site/logout'], 'linkOptions' => ['data-method' => 'post']]; } if (Common::getLanguage()) { switch (Common::getLanguage()) { case 'en': $languageLable = '<i class="ficon-flag-GB"></i> English'; break; case 'zh-CN': $languageLable = '<i class="ficon-flag-CN"></i> 简体中文'; break; default: $languageLable = '<i class="ficon-flag-GB"></i> English'; break; } } $menuItems[] = ['label' => Common::getLanguage() ? $languageLable : \Yii::t('app', 'choose language'), 'url' => '#', 'items' => [['label' => '<i class="ficon-flag-GB"></i> English', 'url' => ['default/locale', 'language' => 'en']], ['label' => '<i class="ficon-flag-CN"></i> 简体中文', 'url' => ['default/locale', 'language' => 'zh-CN']]]]; echo Nav::widget(['options' => ['class' => 'navbar-nav navbar-right'], 'items' => $menuItems, 'encodeLabels' => false]); NavBar::end(); ?> <!-- <div class="container"> --> <?php echo $content; ?> <!-- </div> --> </div> <?php $this->endBody(); ?> </body> </html>
<?php use yii\bootstrap\Nav; use app\components\Common; use app\models\User; /* @var User $current_user */ $current_user = Yii::$app->user->identity; $menuItems = [['label' => '前台首页', 'url' => ['/site/index'], 'linkOptions' => ['target' => '_blank']]]; switch (Common::getLanguage()) { case 'en': $languageLabel = ' English'; break; case 'zh-CN': $languageLabel = '简体中文'; break; default: $languageLabel = 'English'; break; } $menuItems[] = ['label' => $languageLabel, 'url' => '#', 'items' => [['label' => ' English', 'url' => ['default/locale', 'language' => 'en']], ['label' => ' 简体中文', 'url' => ['default/locale', 'language' => 'zh-CN']]]]; $menuItems[] = ['label' => $current_user->nickname, 'url' => ['#'], 'active' => false, 'items' => [['label' => '<i class="fa fa-user"></i> ' . '个人资料', 'url' => ['/user']], ['label' => '<i class="fa fa-sign-out"></i> ' . '注销', 'url' => ['/logout']]]]; $menuItems[] = ['label' => "<i class=\"fa fa-cog\"> </i>", 'url' => '#', 'linkOptions' => ['data-toggle' => 'control-sidebar']]; echo Nav::widget(['options' => ['class' => 'nva navbar-nav'], 'items' => $menuItems, 'encodeLabels' => false]);
public static function getModules() { $modules = \app\components\Common::getModules(); $key = array_search('gii', $modules); unset($modules[$key]); $key = array_search('debug', $modules); unset($modules[$key]); return $modules; }