/**
  * @return \yii\db\ActiveQuery
  */
 public function getContentTemplateHasWidgets()
 {
     return $this->hasMany(ContentTemplateHasWidget::className(), ['content_template_widget_id' => 'id']);
 }
 /**
  * @param int    $id ContentTemplate ID
  * @param string $position
  *
  * @return string
  * @throws \yii\web\BadRequestHttpException
  */
 public function actionExistingWidgets($id, $position)
 {
     if (!Yii::$app->request->isAjax) {
         throw new BadRequestHttpException('AJAX only');
     }
     $widgets = [];
     $templateHasWidgets = ContentTemplateHasWidget::find()->joinWith('contentTemplateWidget')->andWhere(['content_template_widget.active' => 1])->andWhere(['content_template_has_widget.content_template_id' => $id, 'content_template_has_widget.position' => $position, 'content_template_widget.active' => 1])->orderBy('content_template_has_widget.sorter ASC')->all();
     foreach ($templateHasWidgets as $templateHasWidget) {
         $widgets[] = $templateHasWidget->contentTemplateWidget;
     }
     return $this->renderAjax('existingWidgets', compact('widgets', 'position'));
 }