PageRenderTime 45ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/mautic/app/bundles/CoreBundle/Model/CommonModel.php

https://gitlab.com/randydanniswara/website
PHP | 279 lines | 129 code | 48 blank | 102 comment | 16 complexity | fd79c4f81cbd4d51a551ed4011301abc 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\CoreBundle\Model;
  10. use Doctrine\ORM\Mapping\ClassMetadata;
  11. use Doctrine\ORM\Tools\Pagination\Paginator;
  12. use Mautic\CoreBundle\Entity\CommonRepository;
  13. use Mautic\CoreBundle\Factory\MauticFactory;
  14. use Symfony\Component\Intl\Intl;
  15. /**
  16. * Class CommonModel
  17. *
  18. * @package Mautic\CoreBundle\Model
  19. */
  20. class CommonModel
  21. {
  22. /**
  23. * @var \Doctrine\ORM\EntityManager
  24. */
  25. protected $em;
  26. /**
  27. * @var \Mautic\CoreBundle\Security\Permissions\CorePermissions
  28. */
  29. protected $security;
  30. /**
  31. * @var \Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher
  32. */
  33. protected $dispatcher;
  34. /**
  35. * @var \Symfony\Bundle\FrameworkBundle\Translation\Translator
  36. */
  37. protected $translator;
  38. /**
  39. * @var MauticFactory
  40. */
  41. protected $factory;
  42. /**
  43. * @param MauticFactory $factory
  44. */
  45. public function __construct(MauticFactory $factory)
  46. {
  47. $this->em = $factory->getEntityManager();
  48. $this->security = $factory->getSecurity();
  49. $this->dispatcher = $factory->getDispatcher();
  50. $this->translator = $factory->getTranslator();
  51. $this->factory = $factory;
  52. }
  53. /**
  54. * Retrieve the supported search commands for a repository
  55. *
  56. * @return array
  57. */
  58. public function getSupportedSearchCommands()
  59. {
  60. return array();
  61. }
  62. /**
  63. * Retrieve the search command list for a repository
  64. *
  65. * @return array
  66. */
  67. public function getCommandList()
  68. {
  69. $repo = $this->getRepository();
  70. return ($repo instanceof CommonRepository) ? $repo->getSearchCommands() : array();
  71. }
  72. /**
  73. * Retrieve the repository for an entity
  74. *
  75. * @return \Mautic\CoreBundle\Entity\CommonRepository|bool
  76. */
  77. public function getRepository()
  78. {
  79. static $commonRepo;
  80. if ($commonRepo === null) {
  81. $commonRepo = new CommonRepository($this->em, new ClassMetadata('MauticCoreBundle:FormEntity'));
  82. }
  83. return $commonRepo;
  84. }
  85. /**
  86. * Retrieve the permissions base
  87. *
  88. * @return string
  89. */
  90. public function getPermissionBase()
  91. {
  92. return '';
  93. }
  94. /**
  95. * Return a list of entities
  96. *
  97. * @param array $args [start, limit, filter, orderBy, orderByDir]
  98. *
  99. * @return \Doctrine\ORM\Tools\Pagination\Paginator|array
  100. */
  101. public function getEntities(array $args = array())
  102. {
  103. //set the translator
  104. $repo = $this->getRepository();
  105. if ($repo instanceof CommonRepository) {
  106. $repo->setTranslator($this->translator);
  107. $repo->setCurrentUser(
  108. $this->factory->getUser()
  109. );
  110. return $repo->getEntities($args);
  111. }
  112. return array();
  113. }
  114. /**
  115. * Get a specific entity
  116. *
  117. * @param $id
  118. *
  119. * @return null|object
  120. */
  121. public function getEntity($id = null)
  122. {
  123. if (null !== $id) {
  124. $repo = $this->getRepository();
  125. if (method_exists($repo, 'getEntity')) {
  126. return $repo->getEntity($id);
  127. }
  128. return $repo->find($id);
  129. }
  130. return null;
  131. }
  132. /**
  133. * Encode an array to append to a URL
  134. *
  135. * @param $array
  136. *
  137. * @return string
  138. */
  139. public function encodeArrayForUrl($array)
  140. {
  141. return urlencode(base64_encode(serialize($array)));
  142. }
  143. /**
  144. * Decode a string appended to URL into an array
  145. *
  146. * @param $string
  147. *
  148. * @return mixed
  149. */
  150. public function decodeArrayFromUrl($string)
  151. {
  152. return unserialize(base64_decode(urldecode($string)));
  153. }
  154. /**
  155. * @param $route
  156. * @param array $routeParams
  157. * @param bool $absolute
  158. * @param array $clickthrough
  159. *
  160. * @return string
  161. */
  162. public function buildUrl($route, $routeParams = array(), $absolute = true, $clickthrough = array())
  163. {
  164. $url = $this->factory->getRouter()->generate($route, $routeParams, $absolute);
  165. $url .= (!empty($clickthrough)) ? '?ct=' . $this->encodeArrayForUrl($clickthrough) : '';
  166. return $url;
  167. }
  168. /**
  169. * Retrieve entity based on id/alias slugs
  170. *
  171. * @param string $slug
  172. *
  173. * @return object|bool
  174. */
  175. public function getEntityBySlugs($slug)
  176. {
  177. $slugs = explode('/', $slug);
  178. $idSlug = '';
  179. $category = null;
  180. $lang = null;
  181. $slugCount = count($slugs);
  182. $locales = Intl::getLocaleBundle()->getLocaleNames();
  183. switch (true) {
  184. case ($slugCount === 3):
  185. list($lang, $category, $idSlug) = $slugs;
  186. break;
  187. case ($slugCount === 2):
  188. list($category, $idSlug) = $slugs;
  189. // Check if the first slug is actually a locale
  190. if (isset($locales[$category])) {
  191. $lang = $category;
  192. $category = null;
  193. }
  194. break;
  195. case ($slugCount === 1):
  196. $idSlug = $slugs[0];
  197. break;
  198. }
  199. // Check for uncategorized
  200. if ($this->translator->trans('mautic.core.url.uncategorized') == $category) {
  201. $category = null;
  202. }
  203. if ($lang && !isset($locales[$lang])) {
  204. // Language doesn't exist so return false
  205. return false;
  206. }
  207. if (strpos($idSlug, ':') !== false) {
  208. $parts = explode(':', $idSlug);
  209. if (count($parts) == 2) {
  210. $entity = $this->getEntity($parts[0]);
  211. if (!empty($entity)) {
  212. return $entity;
  213. }
  214. }
  215. } else {
  216. $entity = $this->getRepository()->findOneBySlugs($idSlug, $category, $lang);
  217. if (!empty($entity)) {
  218. return $entity;
  219. }
  220. }
  221. return false;
  222. }
  223. /**
  224. * @param $alias
  225. *
  226. * @return null|object
  227. */
  228. public function getEntityByAlias($alias, $categoryAlias = null, $lang = null)
  229. {
  230. }
  231. }