PageRenderTime 52ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/mautic/app/bundles/PageBundle/Entity/PageRepository.php

https://gitlab.com/randydanniswara/website
PHP | 342 lines | 232 code | 42 blank | 68 comment | 28 complexity | 2aabc3d462b573b4eb9e43ac19ebe9fd MD5 | raw file
  1. <?php
  2. /**
  3. * @package Mautic
  4. * @copyright 2014 Mautic Contributors. All rights reserved.
  5. * @author Mautic
  6. * @link http://mautic.org
  7. * @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
  8. */
  9. namespace Mautic\PageBundle\Entity;
  10. use Doctrine\DBAL\Query\Expression\ExpressionBuilder;
  11. use Doctrine\ORM\Query\Expr;
  12. use Doctrine\ORM\Tools\Pagination\Paginator;
  13. use Mautic\CoreBundle\Entity\CommonRepository;
  14. /**
  15. * Class PageRepository
  16. */
  17. class PageRepository extends CommonRepository
  18. {
  19. /**
  20. * {@inheritdoc}
  21. */
  22. public function getEntities($args = array())
  23. {
  24. $q = $this
  25. ->createQueryBuilder('p')
  26. ->select('p')
  27. ->leftJoin('p.category', 'c');
  28. $args['qb'] = $q;
  29. return parent::getEntities($args);
  30. }
  31. /**
  32. * Get a list of popular (by hits) pages
  33. *
  34. * @param integer $limit
  35. *
  36. * @return array
  37. */
  38. public function getPopularPages($limit = 10)
  39. {
  40. $q = $this->createQueryBuilder('p');
  41. $q->select("partial p.{id, title, hits, alias}")
  42. ->orderBy('p.hits', 'DESC')
  43. ->where('p.hits > 0')
  44. ->setMaxResults($limit);
  45. $expr = $this->getPublishedByDateExpression($q, 'p');
  46. $q->andWhere($expr);
  47. return $q->getQuery()->getResult();
  48. }
  49. /**
  50. * @param string $alias
  51. * @param Page $entity
  52. *
  53. * @return mixed
  54. */
  55. public function checkUniqueAlias($alias, $entity = null)
  56. {
  57. $q = $this->createQueryBuilder('e')
  58. ->select('count(e.id) as alias_count')
  59. ->where('e.alias = :alias');
  60. $q->setParameter('alias', $alias);
  61. if (!empty($entity)) {
  62. $parent = $entity->getTranslationParent();
  63. $children = $entity->getTranslationChildren();
  64. if ($parent || count($children)) {
  65. //allow same alias among language group
  66. $ids = array();
  67. if (!empty($parent)) {
  68. $children = $parent->getTranslationChildren();
  69. $ids[] = $parent->getId();
  70. }
  71. foreach ($children as $child) {
  72. if ($child->getId() != $entity->getId()) {
  73. $ids[] = $child->getId();
  74. }
  75. }
  76. $q->andWhere($q->expr()->notIn('e.id', $ids));
  77. }
  78. $parent = $entity->getVariantParent();
  79. $children = $entity->getVariantChildren();
  80. if ($parent || count($children)) {
  81. //allow same alias among language group
  82. $ids = array();
  83. if (!empty($parent)) {
  84. $children = $parent->getVariantChildren();
  85. $ids[] = $parent->getId();
  86. }
  87. foreach ($children as $child) {
  88. if ($child->getId() != $entity->getId()) {
  89. $ids[] = $child->getId();
  90. }
  91. }
  92. $q->andWhere($q->expr()->notIn('e.id', $ids));
  93. }
  94. if ($entity->getId()) {
  95. $q->andWhere('e.id != :id');
  96. $q->setParameter('id', $entity->getId());
  97. }
  98. }
  99. $results = $q->getQuery()->getSingleResult();
  100. return $results['alias_count'];
  101. }
  102. /**
  103. * @param string $search
  104. * @param int $limit
  105. * @param int $start
  106. * @param bool $viewOther
  107. * @param bool $topLevel
  108. * @param array $ignoreIds
  109. *
  110. * @return array
  111. */
  112. public function getPageList($search = '', $limit = 10, $start = 0, $viewOther = false, $topLevel = false, $ignoreIds = array())
  113. {
  114. $q = $this->createQueryBuilder('p');
  115. $q->select('partial p.{id, title, language, alias}');
  116. if (!empty($search)) {
  117. $q->andWhere($q->expr()->like('p.title', ':search'))
  118. ->setParameter('search', "{$search}%");
  119. }
  120. if (!$viewOther) {
  121. $q->andWhere($q->expr()->eq('p.createdBy', ':id'))
  122. ->setParameter('id', $this->currentUser->getId());
  123. }
  124. if ($topLevel == 'translation') {
  125. //only get top level pages
  126. $q->andWhere($q->expr()->isNull('p.translationParent'));
  127. //translations cannot be assigned to a/b tests
  128. $q->andWhere($q->expr()->isNull('p.variantParent'));
  129. } elseif ($topLevel == 'variant') {
  130. $q->andWhere($q->expr()->isNull('p.variantParent'));
  131. }
  132. if (!empty($ignoreIds)) {
  133. $q->andWhere($q->expr()->notIn('p.id', ':pageIds'))
  134. ->setParameter('pageIds', $ignoreIds);
  135. }
  136. $q->orderBy('p.title');
  137. if (!empty($limit)) {
  138. $q->setFirstResult($start)
  139. ->setMaxResults($limit);
  140. }
  141. return $q->getQuery()->getArrayResult();
  142. }
  143. /**
  144. * {@inheritdoc}
  145. */
  146. protected function addCatchAllWhereClause(&$q, $filter)
  147. {
  148. $unique = $this->generateRandomParameterName(); //ensure that the string has a unique parameter identifier
  149. $string = ($filter->strict) ? $filter->string : "%{$filter->string}%";
  150. $expr = $q->expr()->orX(
  151. $q->expr()->like('p.title', ":$unique"),
  152. $q->expr()->like('p.alias', ":$unique")
  153. );
  154. if ($filter->not) {
  155. $expr = $q->expr()->not($expr);
  156. }
  157. return array(
  158. $expr,
  159. array("$unique" => $string)
  160. );
  161. }
  162. /**
  163. * {@inheritdoc}
  164. */
  165. protected function addSearchCommandWhereClause(&$q, $filter)
  166. {
  167. $command = $filter->command;
  168. $unique = $this->generateRandomParameterName();
  169. $returnParameter = true; //returning a parameter that is not used will lead to a Doctrine error
  170. $expr = false;
  171. switch ($command) {
  172. case $this->translator->trans('mautic.core.searchcommand.ispublished'):
  173. $expr = $q->expr()->eq("p.isPublished", ":$unique");
  174. $forceParameters = array($unique => true);
  175. break;
  176. case $this->translator->trans('mautic.core.searchcommand.isunpublished'):
  177. $expr = $q->expr()->eq("p.isPublished", ":$unique");
  178. $forceParameters = array($unique => false);
  179. break;
  180. case $this->translator->trans('mautic.core.searchcommand.isuncategorized'):
  181. $expr = $q->expr()->orX(
  182. $q->expr()->isNull('p.category'),
  183. $q->expr()->eq('p.category', $q->expr()->literal(''))
  184. );
  185. $returnParameter = false;
  186. break;
  187. case $this->translator->trans('mautic.core.searchcommand.ismine'):
  188. $expr = $q->expr()->eq("IDENTITY(p.createdBy)", $this->currentUser->getId());
  189. $returnParameter = false;
  190. break;
  191. case $this->translator->trans('mautic.core.searchcommand.category'):
  192. $expr = $q->expr()->like('c.alias', ":$unique");
  193. $filter->strict = true;
  194. break;
  195. case $this->translator->trans('mautic.core.searchcommand.lang'):
  196. $langUnique = $this->generateRandomParameterName();
  197. $langValue = $filter->string . "_%";
  198. $forceParameters = array(
  199. $langUnique => $langValue,
  200. $unique => $filter->string
  201. );
  202. $expr = $q->expr()->orX(
  203. $q->expr()->eq('p.language', ":$unique"),
  204. $q->expr()->like('p.language', ":$langUnique")
  205. );
  206. break;
  207. }
  208. if ($expr && $filter->not) {
  209. $expr = $q->expr()->not($expr);
  210. }
  211. if (!empty($forceParameters)) {
  212. $parameters = $forceParameters;
  213. } elseif (!$returnParameter) {
  214. $parameters = array();
  215. } else {
  216. $string = ($filter->strict) ? $filter->string : "%{$filter->string}%";
  217. $parameters = array("$unique" => $string);
  218. }
  219. return array( $expr, $parameters );
  220. }
  221. /**
  222. * {@inheritdoc}
  223. */
  224. public function getSearchCommands()
  225. {
  226. return array(
  227. 'mautic.core.searchcommand.ispublished',
  228. 'mautic.core.searchcommand.isunpublished',
  229. 'mautic.core.searchcommand.isuncategorized',
  230. 'mautic.core.searchcommand.ismine',
  231. 'mautic.core.searchcommand.category',
  232. 'mautic.core.searchcommand.lang'
  233. );
  234. }
  235. /**
  236. * {@inheritdoc}
  237. */
  238. protected function getDefaultOrder()
  239. {
  240. return array(
  241. array('p.title', 'ASC')
  242. );
  243. }
  244. /**
  245. * {@inheritdoc}
  246. */
  247. public function getTableAlias()
  248. {
  249. return 'p';
  250. }
  251. /**
  252. * Null variant and translation parent
  253. *
  254. * @param $ids
  255. */
  256. public function nullParents($ids)
  257. {
  258. if (!is_array($ids)) {
  259. $ids = array($ids);
  260. }
  261. $qb = $this->_em->getConnection()->createQueryBuilder();
  262. $qb->update(MAUTIC_TABLE_PREFIX . 'pages')
  263. ->set('variant_parent_id', ':null')
  264. ->setParameter('null', null)
  265. ->where(
  266. $qb->expr()->in('variant_parent_id', $ids)
  267. )
  268. ->execute();
  269. $qb = $this->_em->getConnection()->createQueryBuilder();
  270. $qb->update(MAUTIC_TABLE_PREFIX . 'pages')
  271. ->set('translation_parent_id', ':null')
  272. ->setParameter('null', null)
  273. ->where(
  274. $qb->expr()->in('translation_parent_id', $ids)
  275. )
  276. ->execute();
  277. }
  278. /**
  279. * Up the hit count
  280. *
  281. * @param $id
  282. * @param int $increaseBy
  283. * @param bool|false $unique
  284. * @param bool|false $variant
  285. */
  286. public function upHitCount($id, $increaseBy = 1, $unique = false, $variant = false)
  287. {
  288. $q = $this->_em->getConnection()->createQueryBuilder();
  289. $q->update(MAUTIC_TABLE_PREFIX.'pages')
  290. ->set('hits', 'hits + ' . (int) $increaseBy)
  291. ->where('id = ' . (int) $id);
  292. if ($unique) {
  293. $q->set('unique_hits', 'unique_hits + ' . (int) $increaseBy);
  294. }
  295. if ($variant) {
  296. $q->set('variant_hits', 'variant_hits + ' . (int) $increaseBy);
  297. }
  298. $q->execute();
  299. }
  300. }