Exemplo n.º 1
0
 /**
  * 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;
     }
 }
Exemplo n.º 2
0
 public static function getLanguage()
 {
     if (!($language = Language::findOne(['code' => Yii::$app->language, 'is_active' => 1]))) {
         if (!($language = Language::findOne(['is_default' => 1, 'is_active' => 1]))) {
             $language = new Language();
         }
     }
     return $language;
 }
Exemplo n.º 3
0
 /**
  * /lists/popular
  *
  * @return string
  */
 public function actionPopular()
 {
     $language = null;
     $languageSlug = $this->request->getQueryParam('languageSlug');
     if ($languageSlug) {
         $language = Language::find()->where(['isHidden' => 0, 'slug' => $languageSlug])->limit(1)->one();
     }
     return $this->render('popular', ['language' => $language, 'list' => PopularSnippets::get($language)]);
 }
Exemplo n.º 4
0
 /**
  * @param string $code
  *
  * @return bool
  */
 public static function setCurrent($code)
 {
     $language = Language::find()->where(['code' => $code])->one();
     if ($language) {
         self::$current = $language;
         return true;
     }
     return false;
 }
Exemplo n.º 5
0
 public function init()
 {
     parent::init();
     if ($this->currentLanguage === null) {
         $this->currentLanguage = Language::getCurrent();
     }
     if ($this->allowedLanguages === null) {
         $this->allowedLanguages = Language::find()->where('`id` != :id', [':id' => $this->currentLanguage->id])->all();
     }
     $this->cssClass .= ' dropdown dropdown-language';
     $this->options['class'] = trim($this->cssClass);
 }
Exemplo n.º 6
0
 public function getLangUrl()
 {
     if ($this->_lang_url === null) {
         $this->_lang_url = $this->getUrl();
         $url_list = explode('/', $this->_lang_url);
         $lang_url = isset($url_list[1]) ? $url_list[1] : null;
         Language::setCurrent($lang_url);
         if ($lang_url !== null && $lang_url === Language::getCurrent()->url_key && strpos($this->_lang_url, Language::getCurrent()->url_key) === 1) {
             $this->_lang_url = substr($this->_lang_url, strlen(Language::getCurrent()->url_key) + 1);
         }
     }
     return $this->_lang_url;
 }
Exemplo n.º 7
0
 /**
 * Creates data provider instance with search query applied
 *
 * @param array $params
 *
 * @return ActiveDataProvider
 */
 public function search($params)
 {
     $query = Language::find();
     $dataProvider = new ActiveDataProvider(['query' => $query]);
     $this->load($params);
     if (!$this->validate()) {
         // uncomment the following line if you do not want to any records when validation fails
         // $query->where('0=1');
         return $dataProvider;
     }
     $query->andFilterWhere(['id' => $this->id]);
     $query->andFilterWhere(['like', 'name', $this->name])->andFilterWhere(['like', 'code', $this->code]);
     return $dataProvider;
 }
Exemplo n.º 8
0
 /**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = Language::find();
     // add conditions that should always apply here
     $dataProvider = new ActiveDataProvider(['query' => $query]);
     $this->load($params);
     if (!$this->validate()) {
         // uncomment the following line if you do not want to return any records when validation fails
         // $query->where('0=1');
         return $dataProvider;
     }
     // grid filtering conditions
     $query->andFilterWhere(['id' => $this->id, 'default' => $this->default, 'created_at' => $this->created_at, 'updated_at' => $this->updated_at]);
     $query->andFilterWhere(['like', 'url_key', $this->url_key])->andFilterWhere(['like', 'local', $this->local])->andFilterWhere(['like', 'name', $this->name])->andFilterWhere(['like', 'image', $this->image]);
     return $dataProvider;
 }
Exemplo n.º 9
0
 public function createUrl($params)
 {
     if (isset($params['lang_id'])) {
         //Если указан идентификатор языка, то делаем попытку найти язык в БД,
         //иначе работаем с языком по умолчанию
         $lang = Language::findOne($params['lang_id']);
         if ($lang === null) {
             $lang = Language::getDefaultLang();
         }
         unset($params['lang_id']);
     } else {
         //Если не указан параметр языка, то работаем с текущим языком
         $lang = Language::getCurrent();
     }
     //Получаем сформированный URL(без префикса идентификатора языка)
     $url = parent::createUrl($params);
     //Добавляем к URL префикс - буквенный идентификатор языка
     if ($url == '/') {
         return '/' . $lang->url_key;
     } else {
         return '/' . $lang->url_key . $url;
     }
 }
Exemplo n.º 10
0
 /**
  * @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;
 }
Exemplo n.º 11
0
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getLanguage()
 {
     return $this->hasOne(\common\models\Language::className(), ['id' => 'language_id']);
 }
Exemplo n.º 12
0
 /**
  * @param null $url
  * @return array|null|ActiveRecord
  */
 static function getLangByUrl($url = null)
 {
     if ($url === null) {
         return null;
     } else {
         $language = Language::find()->where('url_key = :url_key', [':url_key' => $url])->one();
         if ($language === null) {
             return null;
         } else {
             return $language;
         }
     }
 }
Exemplo n.º 13
0
 /**
  * Finds the Language model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * @param integer $id
  * @return Language the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($id)
 {
     if (($model = Language::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }
Exemplo n.º 14
0
 /**
  * loadLanguages
  *
  * @return bool
  */
 protected function loadLanguages()
 {
     $cacheKey = Yii::$app->params['cacheKeys']['languages'];
     $languages = $this->cache->get($cacheKey);
     if (!$languages) {
         $languages = [];
         $result = Language::find()->where(['isHidden' => 0])->orderBy('name')->all();
         foreach ($result as $language) {
             if (count($language->snippets)) {
                 $languages[$language->id] = $language->attributes;
                 $languages[$language->id]['count'] = count($language->snippets);
             }
         }
         $this->cache->set($cacheKey, $languages, 600);
     }
     $this->languages = $languages;
 }
Exemplo n.º 15
0
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getLanguage()
 {
     return $this->hasOne(Language::className(), ['id' => 'languageId']);
 }
Exemplo n.º 16
0
$this->title = 'Create a Snippet';
?>

<p>Here you can submit your snippets for everyone to see. If this is not your code, please be sure to credit those responsible. All fields are required.</p>


<h4>Submission</h4>

<?php 
$form = ActiveForm::begin(['id' => 'snippet-form', 'options' => ['class' => 'form'], 'fieldConfig' => ['inputOptions' => ['class' => 'form-control', 'required' => 'required']]]);
?>

<div class="form-group">
    <label for="languageId">Language</label>
    <?php 
echo Html::activeDropDownList($model, 'languageId', ArrayHelper::map(Language::find()->orderBy('name ASC')->all(), 'id', 'name'), ['class' => 'form-control', 'id' => 'languageId']);
?>
</div>

<?php 
echo $form->field($model, 'name', ['inputOptions' => ['type' => 'text', 'required' => 'required', 'placeholder' => 'Title of your Snippet']]);
?>

<?php 
echo $form->field($model, 'description')->textArea(['rows' => '4', 'placeholder' => 'Github style markdown is allowed', 'required' => 'required']);
?>

<?php 
echo $form->field($model, 'code')->textArea(['rows' => '12', 'placeholder' => '', 'required' => 'required']);
?>
Exemplo n.º 17
0
 public function init()
 {
     $model = Language::find()->where(['publish' => 1])->one();
     echo $model->name;
 }
Exemplo n.º 18
0
 public function getAllLanguagesAsMappedArray()
 {
     $models = Language::find()->all();
     return ArrayHelper::map($models, 'id', 'name');
 }
Exemplo n.º 19
0
 /**
  * function ->delete ()
  */
 public function delete()
 {
     $now = strtotime('now');
     $username = Yii::$app->user->identity->username;
     $model = $this;
     if ($log = new UserLog()) {
         $log->username = $username;
         $log->action = "Delete";
         $log->object_class = "Language";
         $log->object_pk = $model->id;
         $log->created_at = $now;
         $log->is_success = 0;
         $log->save();
     }
     if (parent::delete()) {
         if ($log) {
             $log->is_success = 1;
             $log->save();
         }
         FileUtils::removeFolder(Yii::$app->params['images_folder'] . $model->image_path);
         return true;
     }
     return false;
 }