folder and content fake
folder and content loops
This commit is contained in:
@@ -9,6 +9,7 @@
|
||||
<loop class="Thelia\Core\Template\Loop\Address" name="address"/>
|
||||
<loop class="Thelia\Core\Template\Loop\Auth" name="auth"/>
|
||||
<loop class="Thelia\Core\Template\Loop\Category" name="category"/>
|
||||
<loop class="Thelia\Core\Template\Loop\Content" name="content"/>
|
||||
<loop class="Thelia\Core\Template\Loop\Country" name="country"/>
|
||||
<loop class="Thelia\Core\Template\Loop\Customer" name="customer"/>
|
||||
<loop class="Thelia\Core\Template\Loop\Feature" name="feature"/>
|
||||
|
||||
238
core/lib/Thelia/Core/Template/Loop/Content.php
Executable file
238
core/lib/Thelia/Core/Template/Loop/Content.php
Executable file
@@ -0,0 +1,238 @@
|
||||
<?php
|
||||
/*************************************************************************************/
|
||||
/* */
|
||||
/* Thelia */
|
||||
/* */
|
||||
/* Copyright (c) OpenStudio */
|
||||
/* email : info@thelia.net */
|
||||
/* web : http://www.thelia.net */
|
||||
/* */
|
||||
/* This program is free software; you can redistribute it and/or modify */
|
||||
/* it under the terms of the GNU General Public License as published by */
|
||||
/* the Free Software Foundation; either version 3 of the License */
|
||||
/* */
|
||||
/* This program is distributed in the hope that it will be useful, */
|
||||
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
|
||||
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
|
||||
/* GNU General Public License for more details. */
|
||||
/* */
|
||||
/* You should have received a copy of the GNU General Public License */
|
||||
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
||||
/* */
|
||||
/*************************************************************************************/
|
||||
|
||||
namespace Thelia\Core\Template\Loop;
|
||||
|
||||
use Propel\Runtime\ActiveQuery\Criteria;
|
||||
use Propel\Runtime\ActiveQuery\Join;
|
||||
use Thelia\Core\Template\Element\BaseLoop;
|
||||
use Thelia\Core\Template\Element\LoopResult;
|
||||
use Thelia\Core\Template\Element\LoopResultRow;
|
||||
|
||||
use Thelia\Core\Template\Loop\Argument\ArgumentCollection;
|
||||
use Thelia\Core\Template\Loop\Argument\Argument;
|
||||
use Thelia\Log\Tlog;
|
||||
|
||||
use Thelia\Model\Base\FeatureContentQuery;
|
||||
use Thelia\Model\FolderQuery;
|
||||
use Thelia\Model\Map\ContentTableMap;
|
||||
use Thelia\Model\ContentFolderQuery;
|
||||
use Thelia\Model\ContentQuery;
|
||||
use Thelia\Model\ConfigQuery;
|
||||
use Thelia\Type\TypeCollection;
|
||||
use Thelia\Type;
|
||||
|
||||
/**
|
||||
*
|
||||
* Content loop
|
||||
*
|
||||
*
|
||||
* Class Content
|
||||
* @package Thelia\Core\Template\Loop
|
||||
* @author Etienne Roudeix <eroudeix@openstudio.fr>
|
||||
*/
|
||||
class Content extends BaseLoop
|
||||
{
|
||||
/**
|
||||
* @return ArgumentCollection
|
||||
*/
|
||||
protected function getArgDefinitions()
|
||||
{
|
||||
return new ArgumentCollection(
|
||||
Argument::createIntListTypeArgument('id'),
|
||||
Argument::createIntListTypeArgument('folder'),
|
||||
Argument::createBooleanTypeArgument('current'),
|
||||
Argument::createBooleanTypeArgument('current_folder'),
|
||||
Argument::createIntTypeArgument('depth', 1),
|
||||
Argument::createBooleanTypeArgument('visible', 1),
|
||||
new Argument(
|
||||
'order',
|
||||
new TypeCollection(
|
||||
new Type\EnumListType(array('alpha', 'alpha_reverse', 'manual', 'manual_reverse', 'random', 'given_id'))
|
||||
),
|
||||
'alpha'
|
||||
),
|
||||
Argument::createIntListTypeArgument('exclude'),
|
||||
Argument::createIntListTypeArgument('exclude_folder')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $pagination
|
||||
*
|
||||
* @return LoopResult
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
public function exec(&$pagination)
|
||||
{
|
||||
$search = ContentQuery::create();
|
||||
|
||||
$id = $this->getId();
|
||||
|
||||
if (!is_null($id)) {
|
||||
$search->filterById($id, Criteria::IN);
|
||||
}
|
||||
|
||||
$folder = $this->getFolder();
|
||||
|
||||
if (!is_null($folder)) {
|
||||
$folders = FolderQuery::create()->filterById($folder, Criteria::IN)->find();
|
||||
|
||||
$depth = $this->getDepth();
|
||||
|
||||
if(null !== $depth) {
|
||||
foreach(FolderQuery::findAllChild($folder, $depth) as $subFolder) {
|
||||
$folders->prepend($subFolder);
|
||||
}
|
||||
}
|
||||
|
||||
$search->filterByFolder(
|
||||
$folders,
|
||||
Criteria::IN
|
||||
);
|
||||
}
|
||||
|
||||
$current = $this->getCurrent();
|
||||
|
||||
if ($current === true) {
|
||||
$search->filterById($this->request->get("content_id"));
|
||||
} elseif($current === false) {
|
||||
$search->filterById($this->request->get("content_id"), Criteria::NOT_IN);
|
||||
}
|
||||
|
||||
$current_folder = $this->getCurrent_folder();
|
||||
|
||||
if ($current_folder === true) {
|
||||
$search->filterByFolder(
|
||||
FolderQuery::create()->filterByContent(
|
||||
ContentFolderQuery::create()->filterByContentId(
|
||||
$this->request->get("content_id"),
|
||||
Criteria::EQUAL
|
||||
)->find(),
|
||||
Criteria::IN
|
||||
)->find(),
|
||||
Criteria::IN
|
||||
);
|
||||
} elseif($current_folder === false) {
|
||||
$search->filterByFolder(
|
||||
FolderQuery::create()->filterByContent(
|
||||
ContentFolderQuery::create()->filterByContentId(
|
||||
$this->request->get("content_id"),
|
||||
Criteria::EQUAL
|
||||
)->find(),
|
||||
Criteria::IN
|
||||
)->find(),
|
||||
Criteria::NOT_IN
|
||||
);
|
||||
}
|
||||
|
||||
$visible = $this->getVisible();
|
||||
|
||||
$search->filterByVisible($visible);
|
||||
|
||||
$orders = $this->getOrder();
|
||||
|
||||
foreach($orders as $order) {
|
||||
switch ($order) {
|
||||
case "alpha":
|
||||
$search->addAscendingOrderByColumn(\Thelia\Model\Map\ContentI18nTableMap::TITLE);
|
||||
break;
|
||||
case "alpha_reverse":
|
||||
$search->addDescendingOrderByColumn(\Thelia\Model\Map\ContentI18nTableMap::TITLE);
|
||||
break;
|
||||
case "manual":
|
||||
if(null === $folder || count($folder) != 1)
|
||||
throw new \InvalidArgumentException('Manual order cannot be set without single folder argument');
|
||||
$search->orderByPosition(Criteria::ASC);
|
||||
break;
|
||||
case "manual_reverse":
|
||||
if(null === $folder || count($folder) != 1)
|
||||
throw new \InvalidArgumentException('Manual order cannot be set without single folder argument');
|
||||
$search->orderByPosition(Criteria::DESC);
|
||||
break;
|
||||
case "given_id":
|
||||
if(null === $id)
|
||||
throw new \InvalidArgumentException('Given_id order cannot be set without `id` argument');
|
||||
foreach($id as $singleId) {
|
||||
$givenIdMatched = 'given_id_matched_' . $singleId;
|
||||
$search->withColumn(ContentTableMap::ID . "='$singleId'", $givenIdMatched);
|
||||
$search->orderBy($givenIdMatched, Criteria::DESC);
|
||||
}
|
||||
break;
|
||||
case "random":
|
||||
$search->clearOrderByColumns();
|
||||
$search->addAscendingOrderByColumn('RAND()');
|
||||
break(2);
|
||||
}
|
||||
}
|
||||
|
||||
$exclude = $this->getExclude();
|
||||
|
||||
if (!is_null($exclude)) {
|
||||
$search->filterById($exclude, Criteria::NOT_IN);
|
||||
}
|
||||
|
||||
$exclude_folder = $this->getExclude_folder();
|
||||
|
||||
if (!is_null($exclude_folder)) {
|
||||
$search->filterByFolder(
|
||||
FolderQuery::create()->filterById($exclude_folder, Criteria::IN)->find(),
|
||||
Criteria::NOT_IN
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Criteria::INNER_JOIN in second parameter for joinWithI18n exclude query without translation.
|
||||
*
|
||||
* @todo : verify here if we want results for row without translations.
|
||||
*/
|
||||
|
||||
$search->joinWithI18n(
|
||||
$this->request->getSession()->getLocale(),
|
||||
(ConfigQuery::read("default_lang_without_translation", 1)) ? Criteria::LEFT_JOIN : Criteria::INNER_JOIN
|
||||
);
|
||||
|
||||
$search->groupBy(ContentTableMap::ID);
|
||||
|
||||
$contents = $this->search($search, $pagination);
|
||||
|
||||
$loopResult = new LoopResult();
|
||||
|
||||
foreach ($contents as $content) {
|
||||
$loopResultRow = new LoopResultRow();
|
||||
|
||||
$loopResultRow->set("ID", $content->getId())
|
||||
->set("TITLE",$content->getTitle())
|
||||
->set("CHAPO", $content->getChapo())
|
||||
->set("DESCRIPTION", $content->getDescription())
|
||||
->set("POSTSCRIPTUM", $content->getPostscriptum())
|
||||
->set("POSITION", $content->getPosition())
|
||||
;
|
||||
|
||||
$loopResult->addRow($loopResultRow);
|
||||
}
|
||||
|
||||
return $loopResult;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -141,13 +141,13 @@ class Folder extends BaseLoop
|
||||
(ConfigQuery::read("default_lang_without_translation", 1)) ? Criteria::LEFT_JOIN : Criteria::INNER_JOIN
|
||||
);
|
||||
|
||||
$categories = $this->search($search, $pagination);
|
||||
$folders = $this->search($search, $pagination);
|
||||
|
||||
$notEmpty = $this->getNot_empty();
|
||||
|
||||
$loopResult = new LoopResult();
|
||||
|
||||
foreach ($categories as $folder) {
|
||||
foreach ($folders as $folder) {
|
||||
|
||||
if ($notEmpty && $folder->countAllProducts() == 0) continue;
|
||||
|
||||
@@ -160,8 +160,7 @@ class Folder extends BaseLoop
|
||||
->set("DESCRIPTION", $folder->getDescription())
|
||||
->set("POSTSCRIPTUM", $folder->getPostscriptum())
|
||||
->set("PARENT", $folder->getParent())
|
||||
->set("URL", $folder->getUrl())
|
||||
->set("PRODUCT_COUNT", $folder->countChild())
|
||||
->set("CONTENT_COUNT", $folder->countChild())
|
||||
->set("VISIBLE", $folder->getVisible() ? "1" : "0")
|
||||
->set("POSITION", $folder->getPosition())
|
||||
|
||||
|
||||
@@ -109,6 +109,7 @@ class Product extends BaseLoop
|
||||
* @param $pagination
|
||||
*
|
||||
* @return \Thelia\Core\Template\Element\LoopResult
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
public function exec(&$pagination)
|
||||
{
|
||||
|
||||
@@ -4,6 +4,37 @@ namespace Thelia\Model;
|
||||
|
||||
use Thelia\Model\Base\Folder as BaseFolder;
|
||||
|
||||
class Folder extends BaseFolder {
|
||||
class Folder extends BaseFolder
|
||||
{
|
||||
/**
|
||||
* @return int number of contents for the folder
|
||||
*/
|
||||
public function countChild()
|
||||
{
|
||||
return FolderQuery::countChild($this->getId());
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* count all products for current category and sub categories
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function countAllContents()
|
||||
{
|
||||
$children = FolderQuery::findAllChild($this->getId());
|
||||
array_push($children, $this);
|
||||
|
||||
$contentsCount = 0;
|
||||
|
||||
foreach($children as $child)
|
||||
{
|
||||
$contentsCount += ProductQuery::create()
|
||||
->filterByCategory($child)
|
||||
->count();
|
||||
}
|
||||
|
||||
return $contentsCount;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,6 +15,52 @@ use Thelia\Model\Base\FolderQuery as BaseFolderQuery;
|
||||
* long as it does not already exist in the output directory.
|
||||
*
|
||||
*/
|
||||
class FolderQuery extends BaseFolderQuery {
|
||||
class FolderQuery extends BaseFolderQuery
|
||||
{
|
||||
/**
|
||||
*
|
||||
* count how many direct contents a folder has
|
||||
*
|
||||
* @param int $parent folder id
|
||||
* @return int
|
||||
*/
|
||||
public static function countChild($parent)
|
||||
{
|
||||
return self::create()->filterByParent($parent)->count();
|
||||
}
|
||||
|
||||
/**
|
||||
* find all contents for a given folder.
|
||||
*
|
||||
* @param $folderId the folder id or an array of id
|
||||
* @param int $depth max depth you want to search
|
||||
* @param int $currentPosition don't change this param, it is used for recursion
|
||||
* @return \Thelia\Model\Folder[]
|
||||
*/
|
||||
public static function findAllChild($folderId, $depth = 0, $currentPosition = 0)
|
||||
{
|
||||
$result = array();
|
||||
|
||||
if(is_array($folderId)) {
|
||||
foreach($folderId as $folderSingleId) {
|
||||
$result = array_merge($result, (array) self::findAllChild($folderSingleId, $depth, $currentPosition));
|
||||
}
|
||||
} else {
|
||||
$currentPosition++;
|
||||
|
||||
if($depth == $currentPosition && $depth != 0) return;
|
||||
|
||||
$categories = self::create()
|
||||
->filterByParent($folderId)
|
||||
->find();
|
||||
|
||||
|
||||
foreach ($categories as $folder) {
|
||||
array_push($result, $folder);
|
||||
$result = array_merge($result, (array) self::findAllChild($folder->getId(), $depth, $currentPosition));
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
} // FolderQuery
|
||||
|
||||
@@ -121,15 +121,26 @@ try {
|
||||
|
||||
$folder->save();
|
||||
|
||||
for($i=0; $i<rand(0, 4); $i++) {
|
||||
for($j=0; $j<rand(0, 4); $j++) {
|
||||
$subfolder = new Thelia\Model\Folder();
|
||||
$subfolder->setParent($folder->getId());
|
||||
$subfolder->setVisible(rand(1, 10)>7 ? 0 : 1);
|
||||
$subfolder->setPosition($i);
|
||||
$subfolder->setPosition($j);
|
||||
$subfolder->setTitle($faker->text(20));
|
||||
$subfolder->setDescription($faker->text(255));
|
||||
|
||||
$subfolder->save();
|
||||
|
||||
for($k=0; $k<rand(1, 5); $k++) {
|
||||
$content = new Thelia\Model\Content();
|
||||
$content->addFolder($subfolder);
|
||||
$content->setVisible(rand(1, 10)>7 ? 0 : 1);
|
||||
$content->setPosition($k);
|
||||
$content->setTitle($faker->text(20));
|
||||
$content->setDescription($faker->text(255));
|
||||
|
||||
$content->save();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,20 +1,16 @@
|
||||
{loop name="folder0" type="folder" parent="0"}
|
||||
{loop name="folder0" type="folder" parent="0" order="alpha_reverse"}
|
||||
<div style="border: solid 4px blue; padding: 20px; margin: 10px;">
|
||||
<h2>FOLDER : #TITLE</h2>
|
||||
{loop name="folder1" type="folder" parent="#ID"}
|
||||
<hr />
|
||||
<div style="border: double 4px lightseagreen; padding: 20px; margin: 10px;">
|
||||
<h3>SUBFOLDER : #TITLE (#LOOP_COUNT / #LOOP_TOTAL)</h3>
|
||||
{*loop name="content" type="content" folder="#ID"}
|
||||
<h3>CONTENT : #REF / #TITLE</h3>
|
||||
#PRICE €
|
||||
{/loop*}
|
||||
<hr />
|
||||
<hr />
|
||||
{loop name="content" type="content" folder="#ID"}
|
||||
<div style="border: solid 1px green; padding: 20px; margin: 10px;">
|
||||
<h3>CONTENT : #TITLE</h3>
|
||||
<p>#DESCRIPTION</p>
|
||||
</div>
|
||||
{/loop}
|
||||
</div>
|
||||
{/loop}
|
||||
|
||||
{*loop name="content" type="content" folder="#ID"}
|
||||
<h3>CONTENT : #REF / #TITLE</h3>
|
||||
#PRICE €
|
||||
{/loop*}
|
||||
<hr />
|
||||
<hr />
|
||||
</div>
|
||||
{/loop}
|
||||
Reference in New Issue
Block a user