root/trunk/lib/model/QuestionTagPeer.php

Revision 88, 5.4 kB (checked in by fabien, 2 years ago)

updated to symfony 1.0 beta 1

  • Property svn:mime-type set to text/x-php
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1 <?php
2
3   // include base peer class
4   require_once 'lib/model/om/BaseQuestionTagPeer.php';
5  
6   // include object class
7   include_once 'lib/model/QuestionTag.php';
8
9
10 /**
11  * Skeleton subclass for performing query and update operations on the 'ask_question_tag' table.
12  *
13  *
14  *
15  * You should add additional methods to this class to meet the
16  * application requirements.  This class will only be generated as
17  * long as it does not already exist in the output directory.
18  *
19  * @package model
20  */   
21 class QuestionTagPeer extends BaseQuestionTagPeer
22 {
23   public static function getPopularTags($max = 5)
24   {
25     $tags = array();
26
27     $con = Propel::getConnection();
28     $query = '
29       SELECT t1.normalized_tag AS tag,
30       COUNT(t1.normalized_tag) AS count
31       FROM '.QuestionTagPeer::TABLE_NAME.' AS t1';
32
33     if (sfConfig::get('app_permanent_tag'))
34     {
35       $query .= '
36         INNER JOIN '.QuestionTagPeer::TABLE_NAME.' AS t2 ON t1.question_id = t2.question_id
37         WHERE t2.normalized_tag = ? AND t1.normalized_tag != ?
38       ';
39     }
40
41     $query .= '
42       GROUP BY t1.normalized_tag
43       ORDER BY count DESC
44     ';
45
46     $stmt = $con->prepareStatement($query);
47     if (sfConfig::get('app_permanent_tag'))
48     {
49       $stmt->setString(1, sfConfig::get('app_permanent_tag'));
50       $stmt->setString(2, sfConfig::get('app_permanent_tag'));
51     }
52     $stmt->setLimit($max);
53     $rs = $stmt->executeQuery();
54     $max_popularity = 0;
55     while ($rs->next())
56     {
57       if (!$max_popularity)
58       {
59         $max_popularity = $rs->getInt('count');
60       }
61
62       $tags[$rs->getString('tag')] = floor(($rs->getInt('count') / $max_popularity * 3) + 1);
63     }
64
65     ksort($tags);
66
67     return $tags;
68   }
69
70   public static function getPopularTagsFor($question, $max = 10)
71   {
72     $tags = array();
73
74     $con = Propel::getConnection();
75
76     // get popular tags
77     $query = '
78       SELECT '.QuestionTagPeer::NORMALIZED_TAG.' AS tag, COUNT('.QuestionTagPeer::NORMALIZED_TAG.') AS count
79       FROM '.QuestionTagPeer::TABLE_NAME;
80     if ($question !== null)
81     {
82       $query .= '  WHERE '.QuestionTagPeer::QUESTION_ID.' = ?';
83     }
84     $query .= '
85       GROUP BY '.QuestionTagPeer::NORMALIZED_TAG.'
86       ORDER BY count DESC
87     ';
88
89     $stmt = $con->prepareStatement($query);
90     if ($question !== null)
91     {
92       $stmt->setInt(1, $question->getId());
93     }
94     $stmt->setLimit($max);
95     $rs = $stmt->executeQuery();
96     $max_popularity = 0;
97     while ($rs->next())
98     {
99       if (sfConfig::get('app_permanent_tag') == $rs->getString('tag'))
100       {
101         continue;
102       }
103
104       if (!$max_popularity)
105       {
106         $max_popularity = $rs->getInt('count');
107       }
108
109       $tags[$rs->getString('tag')] = floor(($rs->getInt('count') / $max_popularity * 3) + 1);
110     }
111
112     ksort($tags);
113
114     return $tags;
115   }
116
117   public static function getForUserLike($user_id, $tag)
118   {
119     $tags = array();
120
121     $con = Propel::getConnection();
122     $query = '
123       SELECT DISTINCT %s AS tag
124       FROM %s
125       WHERE %s = ? AND %s LIKE ?
126       ORDER BY %s
127     ';
128
129     $query = sprintf($query,
130       QuestionTagPeer::TAG,
131       QuestionTagPeer::TABLE_NAME,
132       QuestionTagPeer::USER_ID,
133       QuestionTagPeer::TAG,
134       QuestionTagPeer::TAG
135     );
136
137     $stmt = $con->prepareStatement($query);
138     $stmt->setInt(1, $user_id);
139     $stmt->setString(2, $tag.'%');
140     $stmt->setLimit(10);
141     $rs = $stmt->executeQuery();
142     while ($rs->next())
143     {
144       $tags[] = $rs->getString('tag');
145     }
146
147     return $tags;
148   }
149
150   public static function getUnpopularTags()
151   {
152     $tags = array();
153
154     $con = Propel::getConnection();
155     $query = '
156       SELECT t1.normalized_tag AS tag,
157       COUNT(t1.normalized_tag) AS count
158       FROM '.QuestionTagPeer::TABLE_NAME.' AS t1';
159
160     if (sfConfig::get('app_permanent_tag'))
161     {
162       $query .= '
163         INNER JOIN '.QuestionTagPeer::TABLE_NAME.' AS t2 ON t1.question_id = t2.question_id
164         WHERE t2.normalized_tag = ? AND t1.normalized_tag != ?
165       ';
166     }
167
168     $query .= '
169       GROUP BY t1.normalized_tag
170       HAVING count < 5
171       ORDER BY count ASC
172     ';
173
174     $stmt = $con->prepareStatement($query);
175     if (sfConfig::get('app_permanent_tag'))
176     {
177       $stmt->setString(1, sfConfig::get('app_permanent_tag'));
178       $stmt->setString(2, sfConfig::get('app_permanent_tag'));
179     }
180     $rs = $stmt->executeQuery();
181     while ($rs->next())
182     {
183       $tags[$rs->getString('tag')] = $rs->getInt('count');
184     }
185
186     return $tags;
187   }
188
189   public static function getByNormalizedTag($tag)
190   {
191     $c = new Criteria();
192     $c->add(self::NORMALIZED_TAG, Tag::normalize($tag));
193
194     return self::doSelect($c);
195   }
196
197   public static function deleteSpam($moderator, $tag, $question_id = null)
198   {
199     if ($question_id === null)
200     {
201       $tags = self::getByNormalizedTag($tag);
202     }
203     else
204     {
205       $c = new Criteria();
206       $c->add(self::NORMALIZED_TAG, Tag::normalize($tag));
207       $c->add(self::QUESTION_ID, $question_id);
208
209       $tags = self::doSelect($c);
210     }
211
212     $con = Propel::getConnection();
213     try
214     {
215       $con->begin();
216
217       foreach ($tags as $tag)
218       {
219         $user = $tag->getUser();
220         $user->setDeletions($user->getDeletions() + 1);
221         $user->save();
222
223         $tag->delete();
224       }
225
226       $con->commit();
227
228       $log = 'moderator "%s" deleted tag "%s"';
229       $log = sprintf($log, $moderator->getNickname(), $tag->getNormalizedTag());
230       sfContext::getInstance()->getLogger()->warning($log);
231     }
232     catch (PropelException $e)
233     {
234       $con->rollback();
235       throw $e;
236     }
237   }
238 }
239
240 ?>
Note: See TracBrowser for help on using the browser.