To participate you must create an account on apostrophenow.org. If you have already done so, click Login.

root/plugins/apostrophePlugin/trunk/lib/model/doctrine/PluginaCategoryTable.class.php @ 2372

Revision 2372, 2.7 KB (checked in by dordille, 3 years ago)

Fixed potential bug in category merging.
Added category merging to tag admin.

Line 
1<?php
2
3/**
4 * PluginaCategoryTable
5 *
6 * This class has been auto-generated by the Doctrine ORM Framework
7 */
8class PluginaCategoryTable extends Doctrine_Table
9{
10
11  /**
12   * Returns an instance of this class.
13   *
14   * @return object PluginaCategoryTable
15   */
16  public static function getInstance()
17  {
18    return Doctrine_Core::getTable('aCategory');
19  }
20
21  public function addCategoriesForUser(sfGuardUser $user, $admin = false, Doctrine_Query $q = null)
22  {
23    if (is_null($q))
24    {
25      $q = $this->createQuery();
26    } else
27    {
28      $q = clone $q;
29    }
30    if (!$admin)
31    {
32      // This will perform well if the user is the current user and Doctrine has
33      // retained the relation to groups
34      $groups = $user->getGroups();
35      $groupIds = aArray::getIds($groups);
36      $q->leftJoin('aCategory.Groups')
37        ->leftJoin('aCategory.Users');
38      // This is necessary because Doctrine doesn't have proper grouping syntax
39      // available except via DQL
40      $where = '';
41      // Don't get burned by an empty IN clause matching everything
42      if (count($groupIds))
43      {
44        $where .= 'aCategory.Groups.id IN (' . implode(',', $groupIds) . ') OR ';
45      }
46      $q->andWhere('(' . $where . 'aCategory.Users.id = ?)', $user['id']);
47    }
48    return $q;
49  }
50
51  public static function getCategoriesForPage($page)
52  {
53    return Doctrine::getTable('aCategory')->createQuery('c')
54      ->innerJoin('c.Pages as p')
55      ->where('p.id = ?', $page['id'])
56      ->orderBy('c.name')
57      ->execute();
58  }
59
60  public static function getCategorizables()
61  {
62    $dispatcher = sfContext::getInstance()->getConfiguration()->getEventDispatcher();
63    $event = new sfEvent(null, 'apostrophe.get_categorizables');
64    $dispatcher->filter($event, array());
65    return $event->getReturnValue();
66  }
67
68  public static function retrieveCategoriesWithCounts()
69  {
70    $categorizables = self::getCategorizables();
71    $q = self::getInstance()->createQuery();
72
73    foreach($categorizables as $key => $info)
74    {
75      $q->leftJoin($info['relation'].' '.$key);
76      $q->addSelect(sprintf('COUNT(%s.id)'));
77    }
78
79  }
80
81  public function mergeCategory($old_id, $new_id, $tableClass, $category_column = 'category_id', $isRefClass = false, $ref_column = null)
82  {
83    if($isRefClass)
84    {
85      Doctrine_Query::create()
86      ->select('old.*')
87      ->from("$tableClass old, $tableClass new")
88      ->where("old.$ref_column = new.$ref_column AND old.$category_column = ? AND old.$category_column = ?", array($old_id, $new_id))
89      ->execute()->delete();
90    }
91
92    Doctrine::getTable($tableClass)->createQuery()
93      ->update()
94      ->set($category_column, $new_id)
95      ->where("$category_column = ?", $old_id)
96      ->execute();
97  }
98
99}
Note: See TracBrowser for help on using the browser.