PageRenderTime 43ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/dmCorePlugin/test/project/plugins/dmTagPlugin/lib/model/doctrine/PluginDmTagTable.class.php

https://bitbucket.org/h4ostudio/dm
PHP | 137 lines | 102 code | 25 blank | 10 comment | 8 complexity | f0c13d30bca57743224901f6d627112c MD5 | raw file
Possible License(s): BSD-3-Clause, MIT, GPL-3.0, LGPL-2.1, BSD-2-Clause, ISC, AGPL-3.0, LGPL-3.0
  1. <?php
  2. /**
  3. */
  4. class PluginDmTagTable extends myDoctrineTable
  5. {
  6. protected
  7. $taggableModelsLoaded = false;
  8. public function getAdminListQuery(dmDoctrineQuery $query)
  9. {
  10. $this->loadTaggableModels();
  11. return parent::getAdminListQuery($query);
  12. }
  13. public function getTagNames()
  14. {
  15. return $this->createQuery('t')
  16. ->select('t.name')
  17. ->fetchFlat();
  18. }
  19. public function getPopularTagsQuery($relations = null, $limit = null, dmDoctrineQuery $q = null)
  20. {
  21. if (empty($relations))
  22. {
  23. $this->loadTaggableModels();
  24. $relations = array_keys($this->getRelationHolder()->getAssociations());
  25. if(empty($relations))
  26. {
  27. throw new dmException('There is no taggable model');
  28. }
  29. }
  30. else
  31. {
  32. $relations = (array) $relations;
  33. }
  34. $q = $q ? $q : $this->createQuery('t')->select('t.*');
  35. $rootAlias = $q->getRootAlias();
  36. $counts = array();
  37. foreach ($relations as $relation)
  38. {
  39. $countAlias = 'num_' . Doctrine_Inflector::tableize($relation);
  40. $q->leftJoin($rootAlias.'.' . $relation . ' '.$relation);
  41. $q->addSelect('COUNT(DISTINCT ' . $relation . '.id) AS ' . $countAlias);
  42. $counts[] = 'COUNT(DISTINCT ' . $relation .'.id)';
  43. }
  44. $q->addSelect('(' . implode(' + ', $counts) . ') as total_num');
  45. $q->orderBy('total_num DESC');
  46. $q->groupBy($rootAlias.'.id');
  47. $q->addHaving('total_num > 0');
  48. if(null !== $limit)
  49. {
  50. $q->limit($limit);
  51. }
  52. return $q;
  53. }
  54. public function getPopularTags($relations = null, $limit = null, $hydrationMode = Doctrine::HYDRATE_RECORD)
  55. {
  56. return $this->getPopularTagsQuery($relations, $limit)->execute(array(), $hydrationMode);
  57. }
  58. public function loadTaggableModels()
  59. {
  60. if(!$this->taggableModelsLoaded)
  61. {
  62. $taggableModels = $this->getTaggableModels();
  63. $taggableModels = $this->getEventDispatcher()->filter(new sfEvent(
  64. $this, 'dm_tag.taggable_models.filter', array()
  65. ), $taggableModels)->getReturnValue();
  66. foreach($taggableModels as $model)
  67. {
  68. dmDb::table($model);
  69. }
  70. $this->taggableModelsLoaded = true;
  71. }
  72. }
  73. /**
  74. * @return array models that act as DmTaggable
  75. */
  76. public function getTaggableModels()
  77. {
  78. $cacheManager = $this->getService('cache_manager');
  79. if($cacheManager && $cacheManager->getCache('dm_tag')->has('taggable_models'))
  80. {
  81. return $cacheManager->getCache('dm_tag')->get('taggable_models');
  82. }
  83. $modelBaseFiles = array_merge(
  84. glob(dmOs::join(sfConfig::get('sf_lib_dir'), 'model/doctrine/base/Base*.class.php')),
  85. glob(dmOs::join(sfConfig::get('sf_lib_dir'), 'model/doctrine/*Plugin/base/Base*.class.php'))
  86. );
  87. $taggableModels = array();
  88. foreach($modelBaseFiles as $modelBaseFile)
  89. {
  90. if(strpos(file_get_contents($modelBaseFile), 'new Doctrine_Template_DmTaggable('))
  91. {
  92. $taggableModels[] = preg_replace('|^Base(\w+).class.php$|', '$1', basename($modelBaseFile));
  93. }
  94. }
  95. if($cacheManager)
  96. {
  97. $cacheManager->getCache('dm_tag')->set('taggable_models', $taggableModels);
  98. }
  99. return $taggableModels;
  100. }
  101. /**
  102. * Find a tag by its name
  103. * @param string $name
  104. * @return DmTag the founded DmTag, or null
  105. */
  106. public function findOneByName($name)
  107. {
  108. return $this->createQuery('r')
  109. ->where('r.name = ?', $name)
  110. ->fetchRecord();
  111. }
  112. }