Example #1
0
 public function run()
 {
     if (($model = Widgets::findOne($this->id)) !== null) {
         $params = $model->getParams();
         $params['options'] = ['class' => $model->css_class];
         $params['cache'] = $model->cache;
         $params['name'] = $model->name;
         $params['id'] = $model->id;
         return Yii::$app->widgets->callWidget(['callback' => $model->widget, 'params' => $params]);
     }
 }
 protected function findModel($id)
 {
     if ($id === null && ($widget = $this->getWidget(Yii::$app->request->get('widget'))) !== null) {
         $model = new Widgets(['state' => 1]);
         $model->widgetModel = new $widget();
         $model->widget = Yii::$app->request->get('widget');
         return $model;
     } elseif (($model = Widgets::findOne($id)) !== null && ($widget = $this->getWidget($model->widget)) !== null) {
         $model->widgetModel = new $widget();
         $model->widgetModel->setAttributes(\yii\helpers\Json::decode($model->params));
         return $model;
     } else {
         //  throw new NotFoundHttpException('The requested page does not exist.');
     }
 }
Example #3
0
 public function search($params)
 {
     $query = Widgets::find()->orderBy('sort');
     $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;
     }
     if (!empty($this->lang)) {
         $query->andWhere(['lang' => [$this->lang, '', null]]);
     }
     $query->andFilterWhere(['widget' => $this->widget, 'position' => $this->position]);
     $query->andFilterWhere(['like', 'name', $this->name]);
     return $dataProvider;
 }
Example #4
0
 public function render($position)
 {
     $out = '';
     if (!empty($position)) {
         $key = 'worstinme_widgets_' . $position . '_' . Yii::$app->language;
         //Yii::$app->cache->flush();
         $data = Yii::$app->cache->get($key);
         if ($data === false) {
             $widgets = Widgets::find()->where(['state' => 1, 'position' => $position, 'lang' => ['', null, Yii::$app->language]])->with('bounds')->orderBY('sort')->all();
             $data = \yii\helpers\ArrayHelper::toArray($widgets, ['worstinme\\widgets\\models\\Widgets' => ['callback' => 'widget', 'bounds' => function ($model) {
                 $bounds = ['only' => [], 'except' => []];
                 if (count($model->bounds)) {
                     foreach ($model->bounds as $bound) {
                         if ($bound['except']) {
                             $bounds['except'][] = $bound['module'] . '/' . $bound['controller'] . '/' . $bound['action'];
                         } else {
                             $bounds['only'][] = $bound['module'] . '/' . $bound['controller'] . '/' . $bound['action'];
                         }
                     }
                 }
                 return $bounds;
             }, 'params' => function ($model) {
                 $params = $model->getParams();
                 $params['options'] = ['class' => $model->css_class];
                 $params['cache'] = $model->cache;
                 $params['name'] = $model->name;
                 $params['id'] = $model->id;
                 return $params;
             }]]);
             $dependency = new \yii\caching\ChainedDependency(['dependencies' => [new \yii\caching\DbDependency(['sql' => 'SELECT MAX(updated_at) FROM {{%widgets}}'])]]);
             Yii::$app->cache->set($key, $data, 0, $dependency);
         }
         $this->getWidgets();
         foreach ($data as $widget) {
             $out .= $this->callWidget($widget);
         }
     }
     return $out;
 }