public function init() { if (is_array($this->remap)) { $this->isClosure = false; } elseif (is_callable($this->remap)) { $this->isClosure = true; } else { throw new ErrorException('remap is wrong type!.'); } //init dataProvider $this->dataProvider->prepare(); return parent::init(); }
/** * Lists all MyClick models. * @return mixed */ public function actionIndex() { $query = (new \yii\db\Query())->select('article.*')->from('my_click')->where(['user_id' => \Yii::$app->user->id])->orderBy('my_click.id DESC'); $click_query = $query->limit(50)->join('LEFT JOIN', 'article', 'article.id = my_click.article_id'); $provide = new ActiveDataProvider(['query' => $click_query, 'pagination' => ['pageSize' => 10]]); $provide->prepare(); $pagination = $provide->getPagination(); return $this->render('index', ['data' => $provide->getModels(), 'pageCount' => $pagination->pageCount, 'page' => $pagination->page, 'links' => $pagination->getLinks()]); }
public function actionIndex() { if (\Yii::$app->user->isGuest) { $this->redirect(['user/sign-in/login']); } $article = (new Query())->from('article')->all(); $query = Article::find(); $provide = new ActiveDataProvider(['query' => $query, 'pagination' => ['pageSize' => 10], 'sort' => ['defaultOrder' => ['created_at' => SORT_DESC, 'title' => 'simon']]]); $provide->prepare(); $pagination = $provide->getPagination(); return $this->render('index', ['article' => $provide->getModels(), 'pageCount' => $pagination->pageCount, 'page' => $pagination->page, 'links' => $pagination->getLinks()]); }
/** * Lists all words. * @param string $sort * @param string $search * @return mixed */ public function actionIndex($sort = null, $search = null) { $query = Word::find()->userId(Yii::$app->user->id); if ($search === null) { $query->sort($sort); } else { $query->search($search); } $provider = new ActiveDataProvider(['query' => $query, 'sort' => false, 'pagination' => ['pageSize' => Yii::$app->params['wordPerPage']]]); $provider->prepare(true); return $this->render('index', ['provider' => $provider, 'search' => $search]); }
public function search($params) { $query = Url::find(); $dataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => []]); $this->load($params); if (!$this->validate()) { return $dataProvider; } $query->andFilterWhere(['user_id' => $this->user_id]); $query->andFilterWhere(['like', 'link', $this->link])->andFilterWhere(['like', 'name', $this->name]); $dependency = new \yii\caching\DbDependency(['sql' => 'SELECT MAX(updated_at) FROM url', 'reusable' => true]); Yii::$app->db->cache(function ($db) use($dataProvider) { $dataProvider->prepare(); }, Yii::$app->params['cacheExpire'], $dependency); return $dataProvider; }
/** // * @param integer|null $category * @return array - dataProvider and pagination. */ public function actionCategory() { // controller->action->id: e.g table-lights $categoryID = $this->findCategoryBySlug(\Yii::$app->controller->action->id); // $productsQuery = Product::find(); // $productsQuery->joinWith('category'); // $productsQuery // ->orFilterWhere(['category.parent_id' => $categoryID]) // ->orWhere(['category.id' => $categoryID]); $productsQuery = Product::find()->joinWith('category')->orFilterWhere(['category.parent_id' => $categoryID])->orWhere(['category.id' => $categoryID]); $dataProvider = new ActiveDataProvider(['query' => $productsQuery, 'pagination' => ['pagesize' => 3]]); $dependency = new DbDependency(); $dependency->sql = 'SELECT count(id) FROM product'; \Yii::$app->db->cache(function ($db) use($dataProvider) { $dataProvider->prepare(); }, 3600, $dependency); return $dataProvider; }
/** * Create sitemap files * @return array */ public function buildPages() { /** @var \yii\db\ActiveRecord $owner */ $owner = $this->owner; $query = $owner::find(); $basePath = $this->module->getFilesPath(); preg_match('/([A-Za-z]+)$/', get_class($owner), $matchResult); $fileSuffix = strtolower($matchResult[0]); // Apply scopes if (is_callable($this->scope)) { call_user_func($this->scope, $query); } // Build data provider for separated on pages $dataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => ['pageSize' => $this->module->perPage, 'pageSizeLimit' => [10, 50000]]]); // Build pages $pages = []; $dataProvider->prepare(); $pageCount = $dataProvider->pagination->getPageCount(); for ($page = 0; $page < $pageCount; $page++) { $dataProvider->pagination->setPage($page); $dataProvider->prepare(true); // Processing one page $pageUrls = []; $n = 0; foreach ($dataProvider->getModels() as $model) { $urlData = call_user_func($this->dataClosure, $model); $pageUrls[$n]['loc'] = $urlData['loc']; $pageUrls[$n]['lastmod'] = $urlData['lastmod']; if (isset($urlData['changefreq'])) { $pageUrls[$n]['changefreq'] = $urlData['changefreq']; } elseif ($this->defaultChangefreq !== false) { $pageUrls[$n]['changefreq'] = $this->defaultChangefreq; } if (isset($urlData['priority'])) { $pageUrls[$n]['priority'] = $urlData['priority']; } elseif ($this->defaultPriority !== false) { $pageUrls[$n]['priority'] = $this->defaultPriority; } if (isset($urlData['news'])) { $pageUrls[$n]['news'] = $urlData['news']; } if (isset($urlData['images'])) { $pageUrls[$n]['images'] = $urlData['images']; } ++$n; } $xmlData = Yii::$app->view->renderPhpFile($this->module->viewPath . '/default/page-template.php', ['urls' => $pageUrls]); $fileUrl = "sitemap_files/{$fileSuffix}_{$page}.xml"; if (file_put_contents($basePath . '/' . $fileUrl, $xmlData)) { $pages[] = ['loc' => $fileUrl, 'lastmod' => time()]; echo "OK: {$basePath}/{$fileUrl}\n"; } } return $pages; }
/** * init */ public function init() { //init dataProvider $this->dataProvider->prepare(); return parent::init(); }
/** * @param $params * @return ActiveDataProvider * @throws \yii\base\InvalidConfigException */ public static function getDataProvider($params) { $query = Post::find()->with('user', 'topic')->orderBy(['created_at' => SORT_ASC]); if ($params['topic_id']) { $query->andWhere(['topic_id' => $params['topic_id']]); } $dataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => ['route' => '/topic/default/view', 'params' => ['id' => $params['topic_id'], 'page' => $params['page']], 'forcePageParam' => false, 'pageSizeLimit' => false, 'defaultPageSize' => Yii::$app->config->get('display_posts_count')]]); $dataProvider->prepare(); return $dataProvider; }
/** * Вывод в файл * @param ActiveDataProvider $dataProvider * @param string $sFileName * @return boolean */ public function exportToFile($dataProvider, $sFileName) { $dataProvider->prepare(); $nMaxCount = $dataProvider->pagination->totalCount; $objPHPExcel = new PHPExcel(); $oSheet = $objPHPExcel->getSheet(0); $cacheMethod = PHPExcel_CachedObjectStorageFactory::cache_to_sqlite3; $bCache = PHPExcel_Settings::setCacheStorageMethod($cacheMethod); $oDefaultStyle = $objPHPExcel->getDefaultStyle(); $oDefaultStyle->getFont()->setName('Arial'); $oDefaultStyle->getFont()->setSize(8); // $oSheet->getPageSetup() // ->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_PORTRAIT) // ORIENTATION_LANDSCAPE // ->setPaperSize(PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4) // ->setFitToPage(true) // ->setFitToWidth(1) // ->setFitToHeight(0); // // $oSheet->getPageMargins() // ->setTop(0.5) // ->setRight(0.35) // ->setLeft(0.35) // ->setBottom(1); // $oSheet->getHeaderFooter() // ->setEvenFooter('&CСтраница &P [&N]') // ->setOddFooter('&CСтраница &P [&N]'); $nPageCount = $dataProvider->pagination->pageCount; if ($dataProvider->pagination->totalCount > $nMaxCount) { $nPageCount = floor($nMaxCount / $dataProvider->pagination->pageSize); } $cou = 0; $nRow = $this->nStartRow; foreach ($this->columnWidth as $k => $v) { if ($v !== null) { $oSheet->getColumnDimension($this->colIndexToName($k + 1))->setWidth($v); } } $sLastCol = $this->colIndexToName(max(count($this->columnWidth), count($this->columnTitles), count($this->columnValues))); $n = $nRow; $sTit = 'A' . $nRow++; $oSheet->setCellValue($sTit, 'Выгрузка от ' . date('d.m.Y H:i')); $objPHPExcel->getActiveSheet()->mergeCells($sTit . ':' . $sLastCol . $n); $sdataTitle = $this->dataTitle === null ? Yii::$app->name : $this->dataTitle; if ($sdataTitle != '') { $n = $nRow; $sTit = 'A' . $nRow++; $oSheet->setCellValue($sTit, $sdataTitle); $objPHPExcel->getActiveSheet()->mergeCells($sTit . ':' . $sLastCol . $n); } if (count($this->columnTitles) > 0) { $nRow++; $oSheet->fromArray($this->columnTitles, null, 'A' . $nRow++); } for ($page = 0; $page < $nPageCount; $page++) { $dataProvider->pagination->setPage($page); $dataProvider->refresh(); foreach ($dataProvider->getModels() as $model) { $aData = []; foreach ($this->columnValues as $v) { $aData[] = $v instanceof Closure ? call_user_func($v, $model, $cou) : $model->{$v}; } $oSheet->fromArray($aData, null, 'A' . $nRow); $cou++; $nRow++; } } $oSheet->getPageSetup()->setPrintArea('A' . $this->nStartRow . ':' . $sLastCol . ($nRow - 1)); $format = $this->getFileExt($sFileName, 'xls'); if ($format == 'xls') { $objWriter = new PHPExcel_Writer_Excel5($objPHPExcel); } else { $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel); } $objWriter->save($sFileName); return true; }