public function actionDiaryCategory($categoryId)
 {
     if (!$this->isAjax()) {
         throw new BadRequestHttpException();
     }
     $model = new Diary();
     $model->recipeCategoryId = $categoryId;
     return $this->renderPartial('@app/views/diary/_category-elements-dropdown-list', ['model' => $model, 'form' => ActiveForm::begin(), 'param' => 'recipesList', 'items' => $model->getRecipeItems()]);
 }
Exemplo n.º 2
0
 /**
  * Finds the Diary model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * @param integer $id
  * @return Diary the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($id)
 {
     if (($model = Diary::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }
 /**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ArrayDataProvider
  */
 public function search($params = [])
 {
     $caloriesQuery = RecipeRepository::getCaloriesQuery('dr.recipe_id')->createCommand()->sql;
     $proteinsQuery = RecipeRepository::getProteinsQuery('dr.recipe_id')->createCommand()->sql;
     $fatsQuery = RecipeRepository::getFatsQuery('dr.recipe_id')->createCommand()->sql;
     $carbohydratesQuery = RecipeRepository::getCarbohydratesQuery('dr.recipe_id')->createCommand()->sql;
     $query = IngredientEntity::find()->select(['(\'' . IngredientEntity::TYPE_WEIGHT . '\') AS type', 'r.id', 'r.name', 'dr.weight', "({$caloriesQuery}) * `dr`.`weight` AS `calories`", "({$proteinsQuery}) * `dr`.`weight` AS `protein`", "({$fatsQuery}) * `dr`.`weight` AS `fat`", "({$carbohydratesQuery}) * `dr`.`weight` AS `carbohydrate`"])->from(['dr' => Diary::diary2recipesTableName()])->leftJoin(['r' => Recipe::tableName()], 'r.id = dr.recipe_id')->where(['dr.diary_id' => $this->id])->orderBy('calories DESC');
     $dataProvider = new ArrayDataProvider(['allModels' => $query->all(), 'sort' => ['attributes' => ['name', 'weight', 'calories', 'protein', 'fat', 'carbohydrate'], 'defaultOrder' => ['name' => SORT_ASC]], 'pagination' => false]);
     return $dataProvider;
 }
 /**
  * @param array $params
  * @param bool $filterByUser
  * @return \yii\db\Query
  */
 public static function getQuery($params = [], $filterByUser = true)
 {
     $portionCaloriesSql = self::getPortionCaloriesQuery('`d`.`id`')->createCommand()->sql;
     $recipeCaloriesSql = self::getRecipeCaloriesQuery('`d`.`id`')->createCommand()->sql;
     $productCaloriesSql = self::getProductCaloriesQuery('`d`.`id`')->createCommand()->sql;
     $portWeightSql = self::getPortionsWeightQuery('`d`.`id`')->createCommand()->sql;
     $recWeightSql = self::getRecipesWeightQuery('`d`.`id`')->createCommand()->sql;
     $prodWeightSql = self::getProductsWeightQuery('`d`.`id`')->createCommand()->sql;
     $query = Diary::find()->select(['`d`.*', '`u`.`weighing_day` AS `weighingDay`', "COALESCE(({$portionCaloriesSql}), 0) + COALESCE(({$recipeCaloriesSql}), 0) + COALESCE(({$productCaloriesSql}), 0) AS `calories`", "COALESCE(({$prodWeightSql}), 0) + COALESCE(({$recWeightSql}), 0) + COALESCE(({$portWeightSql}), 0) AS `weight`"])->from(self::tableName() . ' `d`')->leftJoin(User::tableName() . ' `u`', '`u`.`id` = `d`.`user_id`');
     if ($filterByUser) {
         $query->where(['`d`.`user_id`' => Yii::$app->user->id]);
     }
     return $query;
 }
Exemplo n.º 5
0
 public function getRead(Request $req, $id)
 {
     $diary = Diary::with(['comments', 'tags'])->find($id);
     if ($diary->status == 0) {
         if (!Auth::check()) {
             return redirect('diary');
         } elseif (Auth::user()->id != $diary->user_id) {
             return redirect('diary');
         }
     }
     //$comments=Comments::where('diary_id', $id)->get();
     $diary->visits += 1;
     $diary->save();
     return view('site.diary.read', ['pageInfo' => ['pageLogo' => 'diary', 'siteTitle' => $diary->title, 'pageHeading' => 'Diary', 'pageHeadingSlogan' => 'I write here what I learn', 'siteImage' => $diary->featured_image, 'siteContents' => strShorten($diary->note, 200)], 'diary' => $diary, 'comments' => $diary->comments, 'request' => $req]);
 }
 protected function sidebarCategory()
 {
     if (!Cache::has('_tags')) {
         $tags = Tagged::join('tags', 'tagged.tag_id', '=', 'tags.id')->select('tags.id', DB::raw('count(tag_id) as total_tags'), 'tags.tag_name')->groupBy('tagged.tag_id')->take(20)->get();
         Cache::put('_tags', $tags, 60);
     }
     if (!Cache::has('_category')) {
         $category = Category::get();
         Cache::put('_category', $category, 60);
     }
     if (!Cache::has('_recent')) {
         $recent = Diary::where('status', 1)->orderBy('created_at', 'desc')->take(5)->get(['title', 'id']);
         Cache::put('_recent', $recent, 60);
     }
     view()->composer('site.partials.sidebar', function ($view) {
         $view->with('navData', ['category' => Cache::get('_category'), 'recents' => Cache::get('_recent'), 'tags' => Cache::get('_tags')]);
     });
 }
 /**
  * @param integer $id
  * @return Query
  */
 public static function getWeightQuery($id)
 {
     $prodWeightSql = self::getProductsWeightQuery('`d`.`id`')->createCommand()->sql;
     $recWeightSql = self::getRecipesWeightQuery('`d`.`id`')->createCommand()->sql;
     $portWeightSql = self::getPortionsWeightQuery('`d`.`id`')->createCommand()->sql;
     return (new Query())->select("COALESCE(({$prodWeightSql}), 0) + COALESCE(({$recWeightSql}), 0) + COALESCE(({$portWeightSql}), 0) AS `weight`")->from(Diary::tableName() . ' `d`')->where(['`d`.`id`' => $id]);
 }
Exemplo n.º 8
0
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getDiary()
 {
     return $this->hasOne(Diary::className(), ['diary_id' => 'diary_id']);
 }
Exemplo n.º 9
0
 public function getDelete($id)
 {
     if (Auth::user()->role == 'admin') {
         if (Diary::find($id)->delete()) {
             return redirect()->back()->with('msg', 'deleted');
         }
     }
     if (Diary::where('user_id', Auth::user()->id)->where('id', $id)->exists()) {
         if (Diary::find($id)->delete()) {
             return redirect()->back()->with('msg', 'deleted');
         }
     }
     return redirect()->back();
 }
 /**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ArrayDataProvider
  */
 public function search($params = [])
 {
     $query = IngredientEntity::find()->select(['(\'' . IngredientEntity::TYPE_WEIGHT . '\') AS type', 'p.id', 'p.name', 'dp.weight', '`dp`.`weight` * `p`.`calories` AS `calories`', '`dp`.`weight` * `p`.`protein` AS `protein`', '`dp`.`weight` * `p`.`fat` AS `fat`', '`dp`.`weight` * `p`.`carbohydrate` AS `carbohydrate`'])->from(['dp' => Diary::diary2productsTableName()])->leftJoin(['p' => Product::tableName()], 'p.id = dp.product_id')->where(['dp.diary_id' => $this->id])->orderBy('calories DESC');
     $dataProvider = new ArrayDataProvider(['allModels' => $query->all(), 'sort' => ['attributes' => ['name', 'weight', 'calories', 'protein', 'fat', 'carbohydrate'], 'defaultOrder' => ['name' => SORT_ASC]], 'pagination' => false]);
     return $dataProvider;
 }
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getDiaries()
 {
     return $this->hasMany(Diary::className(), ['id' => 'diary_id'])->viaTable(Diary::diary2portionsTableName(), ['portion_id' => 'id']);
 }
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getDiary()
 {
     return $this->hasMany(Diary::className(), ['id' => 'calculating_id'])->viaTable(Diary::diary2recipesTableName(), ['recipe_id' => 'id']);
 }