/** * Saves the Form * * @param \yii\db\ActiveRecord $member * @return bool */ public function save($member) { // Find language $language = Language::find()->where(['id' => $this->languageId])->limit(1)->one(); if (!$language) { $this->addError('languageId', 'Could not find Language'); return false; } $slug = Util::generateUrlSafeSlug($this->name); $iteration = 0; // Generate a unique slug do { $exists = Snippet::find()->where(['slug' => $slug])->count(); if ($exists) { $iteration++; $slug = Util::generateUrlSafeSlug($this->name . '-' . $iteration); } } while ($exists); // TODO: strip <script tags from description $snippet = new Snippet(); $snippet->memberId = $member->id; $snippet->languageId = $language->id; $snippet->slug = $slug; $snippet->name = $this->name; $snippet->description = $this->description; $snippet->code = $this->code; if ($snippet->save()) { return '/snippets/' . $language->slug . '/' . $snippet->slug; } else { $this->addError('code', 'There was an error saving the Snippet!'); return false; } }
/** * @param \yii\db\ActiveRecord $member * @return array */ public static function get($member) { $request =& Yii::$app->request; $offset = (int) $request->getQueryParam('o', 0); $query = Snippet::find()->where(['isHidden' => 0, 'memberId' => $member->id]); $count = $query->count(); $snippets = $query->orderBy('createdTime DESC')->offset($offset)->limit(10)->all(); return ['snippets' => $snippets, 'pagination' => Util::getPagination($offset, $count, 20, 'o')]; }
/** * @param string $searchString * @return array */ public static function get($searchString) { $request =& Yii::$app->request; $offset = (int) $request->getQueryParam('o', 0); $query = Snippet::find()->where(['isHidden' => 0]); $query->andFilterWhere(['like', 'name', $searchString]); $count = $query->count(); $snippets = $query->orderBy('createdTime DESC')->offset($offset)->limit(20)->all(); return ['snippets' => $snippets, 'pagination' => Util::getPagination($offset, $count, 20, 'o')]; }
/** * @param \yii\db\ActiveRecord $language * @return array */ public static function get($language = null) { $request =& Yii::$app->request; $offset = (int) $request->getQueryParam('o', 0); $where = ['isHidden' => 0]; if ($language) { $where['languageId'] = $language->id; } $query = Snippet::find()->where($where); $count = $query->count(); $snippets = $query->orderBy('views DESC')->offset($offset)->limit(20)->all(); return ['snippets' => $snippets, 'pagination' => Util::getPagination($offset, $count, 20, 'o')]; }
/** * Site Homepage * * @return string */ public function actionView() { $memberId = (int) $this->request->getQueryParam('id'); if (!$memberId) { return $this->goHome(); } $member = Member::find()->where(['id' => $memberId])->limit(1)->one(); if (!$member) { return $this->goHome(); } $snippetCount = Snippet::find()->where(['memberId' => $member->id, 'isHidden' => 0])->count(); $commentCount = SnippetComment::find()->where(['memberId' => $member->id])->count(); return $this->render('view', ['member' => $member, 'snippetCount' => $snippetCount, 'commentCount' => $commentCount, 'snippets' => MemberSnippetsList::get($member)]); }
/** * @return \yii\db\ActiveQuery */ public function getSnippet() { return $this->hasOne(Snippet::className(), ['id' => 'snippetId']); }
/** * @return \yii\db\ActiveQuery */ public function getSnippets() { return $this->hasMany(Snippet::className(), ['languageId' => 'id']); }
/** * @param bool $skipSnippetCheck * @return bool */ protected function parseQueryParams($skipSnippetCheck = false) { $languageSlug = $this->request->getQueryParam('languageSlug'); $snippetSlug = $this->request->getQueryParam('snippetSlug'); // Make sure at least language slug is there if (!$languageSlug) { return $this->goHome(); } // Find the language row $this->language = Language::find()->where(['slug' => $languageSlug])->limit(1)->one(); if (!$this->language) { return false; } // Make sure at least language slug is there if (!$snippetSlug) { if ($skipSnippetCheck) { return true; } return $this->goHome(); } $this->snippet = Snippet::find()->where(['slug' => $snippetSlug, 'languageId' => $this->language->id])->limit(1)->one(); if (!$this->snippet && !$skipSnippetCheck) { return false; } return true; }