src/Manager/CategoryManager.php line 174

Open in your IDE?
  1. <?php
  2. namespace App\Manager;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use App\Entity\Category;
  5. class CategoryManager {
  6. private $em;
  7. private $languageMgr;
  8. private $projectDir;
  9. public function __construct(EntityManagerInterface $em, \App\Manager\LanguageManager $languageMgr, \Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface $parameter)
  10. {
  11. $this->projectDir = $parameter->get('kernel.project_dir').'/';
  12. $this->em = $em;
  13. $this->languageMgr = $languageMgr;
  14. }
  15. public function sortCategory(Category $category, $position, $flush=true)
  16. {
  17. $category->setSortOrder($position);
  18. $this->em->persist($category);
  19. if($flush){
  20. $this->em->flush();
  21. }
  22. }
  23. public function export(?Category $category=null, $filename = null)
  24. {
  25. if(empty($category)){
  26. $categories = $this->em->getRepository('App:Category')->getAll();
  27. }else{
  28. $categories = $this->em->getRepository('App:Category')->getCategorysByCategory($category);
  29. }
  30. if(empty($filename)) {
  31. $filename = 'var/exports/categories'.date('YmdHis').'.csv';
  32. }
  33. $fp = fopen($this->projectDir.$filename,'w');
  34. \App\Helpers\Encoder::fputcsvEol($fp, $this->getCsvHeader(),";");
  35. foreach($categories as $category){
  36. \App\Helpers\Encoder::fputcsvEol($fp, $this->categoryToCsv($category),";");
  37. }
  38. fclose($fp);
  39. return $filename;
  40. }
  41. public function categoryToCsv(Category $category) : array {
  42. $descriptionEn = $category->getCategoryDescription('en');
  43. if(is_null($descriptionEn))
  44. $descriptionEn = $this->createCategoryDescription($category,'en');
  45. $descriptionFr = $category->getCategoryDescription('fr');
  46. if(is_null($descriptionFr))
  47. $descriptionFr = $this->createCategoryDescription($category,'fr');
  48. $tab = [
  49. /*1 ID categorie */ $category->getId(),
  50. /*2 ID parent */ $category->getParent()?$category->getParent()->getId():0,
  51. /*3 Active */ $category->getAffichage(),
  52. /*4 Position */ $category->getSortOrder(),
  53. /*5 Désignation FR */ $descriptionFr->getName(),
  54. /*6 H1 FR */ $descriptionFr->getHeadingTitle(),
  55. /*7 Description longue FR */ $descriptionFr->getDescription(),
  56. /*8 URL FR */ $descriptionFr->getUrl(),
  57. /*9 Meta Title FR */ $descriptionFr->getHeadTitleTag(),
  58. /*10 Meta Description FR */ $descriptionFr->getHeadDescTag(),
  59. /*11 Meta Keywords FR */ $descriptionFr->getHeadKeywordsTag(),
  60. /*12 Tags FR */ $descriptionFr->getTags(),
  61. /*13 Bannière FR */ $descriptionFr->getMenuBanner(),
  62. /*14 Désignation EN */ $descriptionEn->getName(),
  63. /*15 H1 EN */ $descriptionEn->getHeadingTitle(),
  64. /*16 Description longue EN */ $descriptionEn->getDescription(),
  65. /*17 URL EN */ $descriptionEn->getUrl(),
  66. /*18 Meta Title EN */ $descriptionEn->getHeadTitleTag(),
  67. /*19 Meta Description EN */ $descriptionEn->getHeadDescTag(),
  68. /*20 Meta Keywords EN */ $descriptionEn->getHeadKeywordsTag(),
  69. /*21 Tags EN */ $descriptionEn->getTags(),
  70. /*22 Bannière EN */ $descriptionEn->getMenuBanner(),
  71. /*23 Date de création */ $category->getDateAdded()?$category->getDateAdded()->format('YmdHis'):'',
  72. /*24 Date de modification */ ''
  73. ];
  74. return $tab;
  75. }
  76. protected function getCsvHeader() {
  77. return [
  78. 'ID categorie',
  79. 'ID parent',
  80. 'Active',
  81. 'Position',
  82. 'Désignation FR',
  83. 'H1 FR',
  84. 'Description longue FR',
  85. 'URL FR',
  86. 'Meta Title FR',
  87. 'Meta Description FR',
  88. 'Meta Keywords FR',
  89. 'Tags FR',
  90. 'Bannière FR',
  91. 'Désignation EN',
  92. 'H1 EN',
  93. 'Description longue EN',
  94. 'URL EN ',
  95. 'Meta Title EN',
  96. 'Meta Description EN',
  97. 'Meta Keywords EN ',
  98. 'Tags FR',
  99. 'Bannière FR',
  100. 'Date de création ',
  101. 'Date de modification'
  102. ];
  103. }
  104. /**
  105. * Builds the filter facets available for a category from Attribute values (Akeneo import),
  106. * restricted to attributes flagged "show in filter". The array key is the attribute code,
  107. * matching the format expected by getProducts()/getProductsCount().
  108. */
  109. public function getFilters(Category $category, $lang = 'fr') {
  110. $language = $this->languageMgr->getByCode($lang);
  111. $sql = 'select
  112. a.attributes_code,
  113. ad.attributes_title,
  114. a.attributes_order,
  115. pa.attributes_value
  116. from attributes a
  117. inner join attributes_description ad on ad.attributes_id = a.attributes_id and ad.language_id = :languageId
  118. inner join products_attributes pa on pa.attributes_id = a.attributes_id
  119. inner join products_to_categories ptc on ptc.products_id = pa.products_id
  120. inner join products p on p.products_id = pa.products_id
  121. where
  122. a.attributes_filter = 1 and
  123. a.attributes_status = 1 and
  124. p.products_status = 1 and
  125. ptc.categories_id in (:categoryIds) and
  126. pa.attributes_value is not null and pa.attributes_value <> \'\'
  127. group by a.attributes_code, pa.attributes_value
  128. order by a.attributes_order, pa.attributes_value';
  129. $conn = $this->em->getConnection();
  130. $st = $conn->executeQuery($sql, [
  131. 'languageId' => $language->getId(),
  132. 'categoryIds' => $this->getCategoryTreeIds($category)
  133. ], [
  134. 'categoryIds' => \Doctrine\DBAL\Connection::PARAM_INT_ARRAY
  135. ]);
  136. $filters = array();
  137. while ($result = $st->fetchAssociative()) {
  138. $code = $result['attributes_code'];
  139. if(!isset($filters[$code])){
  140. $filters[$code] = array(
  141. 'name' => $result['attributes_title'],
  142. 'attributes' => array()
  143. );
  144. }
  145. $filters[$code]['attributes'][] = array(
  146. 'id' => htmlentities($result['attributes_value']),
  147. 'name' => htmlentities($result['attributes_value'])
  148. );
  149. }
  150. return $filters;
  151. }
  152. /**
  153. * Lists a category's products, page by page. Variants sharing the same ProductModel are
  154. * grouped and returned as a single entry (see CategoryProductRepository::getProductsByCategory).
  155. * $filters maps an Attribute code to a value (or array of values) to filter by, e.g. ['main_color' => 'RED'].
  156. *
  157. * Products assigned to any descendant category are included as well, not only those
  158. * assigned directly to $category.
  159. */
  160. public function getProducts(Category $category, $filters = [], $sorts = [], $page = 1, $limit = 20, $locale = 'fr') {
  161. return $this->em->getRepository(\App\Entity\CategoryProduct::class)->getProductsByCategory($this->getCategoryTreeIds($category), true, $limit, $page, is_array($sorts) ? $sorts : [], $filters ?: []);
  162. }
  163. public function getProductsCount(Category $category, $filters = []) {
  164. return $this->em->getRepository(\App\Entity\CategoryProduct::class)->getProductCountByCategory($this->getCategoryTreeIds($category), true, $filters ?: []);
  165. }
  166. /**
  167. * Returns the id of $category plus the ids of all its descendant categories (recursively).
  168. */
  169. private function getCategoryTreeIds(Category $category) : array {
  170. return $this->em->getRepository(Category::class)->getChildrenIds($category);
  171. }
  172. public function createCategoryDescription(Category $category, $code = 'fr'){
  173. $description = new \App\Entity\CategoryDescription();
  174. $description->setLanguage($this->languageMgr->getByCode($code));
  175. $description->setCategory($category);
  176. $this->em->persist($description);
  177. $this->em->flush();
  178. return $description;
  179. }
  180. public function getPathTags(Category $category, $code = 'fr') {
  181. $tags = $category->getTags();
  182. if(empty($tags)){
  183. $path = $this->em->getRepository('App:Category')->getPath($category);
  184. $rpath = array_reverse($path);
  185. foreach($rpath as $p){
  186. $cDescription = $p->getCategoryDescription($code);
  187. $tags = $cDescription->getTags();
  188. if(!empty($tags))
  189. return $tags;
  190. }
  191. }
  192. return $tags;
  193. }
  194. public function orderChildrenByPathIds(Category &$category, $path = null) {
  195. if(is_null($path)){
  196. $path = [];
  197. $p_tmp = $this->em->getRepository('App:Category')->getPath($category);
  198. if($path){
  199. foreach($pids as $p){
  200. $pids[] = $p->getId();
  201. }
  202. }
  203. }
  204. if(!empty($path)){
  205. print_r($path);
  206. $children = [];
  207. foreach($category->getChildren() as $child){
  208. $this->orderChildrenByPathIds($child,$path);
  209. $child->setCurrentPath($path);
  210. $children[] = $child;
  211. }
  212. usort($children, function($a, $b)
  213. {
  214. if(in_array($a->getId(), $a->getCurrentPath())){
  215. return -1;
  216. }
  217. return $a->getSortOrder()<$b->getSortOrder()?-1:1;
  218. });
  219. $category->setChildren($children);
  220. }
  221. }
  222. }