Development

#3138: TagPeer.php

You must first sign up to be able to contribute.

Ticket #3138: TagPeer.php

File TagPeer.php, 10.1 kB (added by jug, 7 months ago)

I wrote this function ... (line 257)

Line 
1 <?php
2 /*
3  * This file is part of the sfPropelActAsTaggableBehavior package.
4  *
5  * (c) 2007 Xavier Lacot <xavier@lacot.org>
6  *
7  * For the full copyright and license information, please view the LICENSE
8  * file that was distributed with this source code.
9  */
10
11 /**
12  * Subclass for performing query and update operations on the 'tag' table.
13  *
14  * @package plugins.sfPropelActAsTaggableBehaviorPlugin.lib.model
15  */
16 class TagPeer extends BaseTagPeer
17 {
18   /**
19    * Returns all tags, eventually with a limit option.
20    * The first optionnal parameter permits to add some restrictions on the
21    * objects the selected tags are related to.
22    * The second optionnal parameter permits to restrict the tag selection with
23    * different criterias
24    *
25    * @param      Criteria    $c
26    * @param      array       $options
27    */
28   public static function getAll(Criteria $c = null, $options = array())
29   {
30     if ($c == null)
31     {
32       $c = new Criteria();
33     }
34
35     if (isset($options['limit']))
36     {
37       $c->setLimit($options['limit']);
38     }
39
40     if (isset($options['like']))
41     {
42       $c->add(TagPeer::NAME, $options['like'], Criteria::LIKE);
43     }
44
45     return TagPeer::doSelect($c);
46   }
47
48   /**
49    * Returns all tags, sorted by name, with their number of occurencies.
50    * The first optionnal parameter permits to add some restrictions on the
51    * objects the selected tags are related to.
52    * The second optionnal parameter permits to restrict the tag selection with
53    * different criterias
54    *
55    * @param      Criteria    $c
56    * @param      array       $options
57    */
58   public static function getAllWithCount(Criteria $c = null, $options = array())
59   {
60     $tags = array();
61
62     if ($c == null)
63     {
64       $c = new Criteria();
65     }
66
67     if (isset($options['model']))
68     {
69       $c->add(TaggingPeer::TAGGABLE_MODEL, $options['model']);
70     }
71
72     if (isset($options['like']))
73     {
74       $c->add(TagPeer::NAME, $options['like'], Criteria::LIKE);
75     }
76
77     $c->addSelectColumn(TagPeer::NAME);
78     $c->addSelectColumn(TaggingPeer::COUNT);
79     $c->addJoin(TagPeer::ID, TaggingPeer::TAG_ID);
80     $c->addGroupByColumn(TaggingPeer::TAG_ID);
81     $c->addDescendingOrderByColumn(TaggingPeer::COUNT);
82     $c->addAscendingOrderByColumn(TagPeer::NAME);
83     $rs = TagPeer::doSelectRS($c);
84
85     while ($rs->next())
86     {
87       $tags[$rs->getString(1)] = $rs->getInt(2);
88     }
89
90     ksort($tags);
91     return $tags;
92   }
93
94   /**
95    * Returns the names of the models that have instances tagged with one or
96    * several tags. The optionnal parameter might be a string, an array, or a
97    * comma separated string
98    *
99    * @param      mixed       $tags
100    */
101   public static function getModelsTaggedWith($tags = array())
102   {
103     $tags = self::string2array($tags);
104
105     $c = new Criteria();
106     $c->addJoin(TagPeer::ID, TaggingPeer::TAG_ID);
107     $c->add(TagPeer::NAME, $tags, Criteria::IN);
108     $c->addGroupByColumn(TaggingPeer::TAGGABLE_ID);
109     $having = $c->getNewCriterion(TagPeer::COUNT, count($tags), Criteria::GREATER_EQUAL);
110     $c->addHaving($having);
111     $c->clearSelectColumns();
112     $c->addSelectColumn(TaggingPeer::TAGGABLE_MODEL);
113     $c->addSelectColumn(TaggingPeer::TAGGABLE_ID);
114
115     $sql = BasePeer::createSelectSql($c, array());
116     $con = Propel::getConnection();
117     $stmt = $con->prepareStatement($sql);
118     $position = 1;
119
120     foreach ($tags as $tag)
121     {
122       $stmt->setString($position, $tag);
123       $position++;
124     }
125
126     $stmt->setString($position, count($tags));
127     $rs = $stmt->executeQuery(ResultSet::FETCHMODE_NUM);
128     $models = array();
129
130     while ($rs->next())
131     {
132       $models[] = $rs->getString(1);
133     }
134
135     return $models;
136   }
137
138   /**
139    * Returns the most popular tags with their associated weight. See
140    * sfPropelActAsTaggableToolkit::normalize for more details.
141    *
142    * The first optionnal parameter permits to add some restrictions on the
143    * objects the selected tags are related to.
144    * The second optionnal parameter permits to restrict the tag selection with
145    * different criterias
146    *
147    * @param      Criteria    $c
148    * @param      array       $options
149    */
150   public static function getPopulars($c = null, $options = array())
151   {
152     if ($c == null)
153     {
154       $c = new Criteria();
155     }
156
157     if (!$c->getLimit())
158     {
159       $c->setLimit(sfConfig::get('app_tags_limit', 100));
160     }
161
162     $all_tags = TagPeer::getAllWithCount($c, $options);
163     return sfPropelActAsTaggableToolkit::normalize($all_tags);
164   }
165
166   /**
167    * Returns the tags that are related to one or more other tags, with their
168    * associated weight (see sfPropelActAsTaggableToolkit::normalize for more
169    * details).
170    * The "related tags" of one tag are the ones which have at least one
171    * taggable object in common.
172    *
173    * The first optionnal parameter permits to add some restrictions on the
174    * objects the selected tags are related to.
175    * The second optionnal parameter permits to restrict the tag selection with
176    * different criterias
177    *
178    * @param      mixed       $tags
179    * @param      array       $options
180    */
181   public static function getRelatedTags($tags = array(), $options = array())
182   {
183     $tags = self::string2array($tags);
184
185     $tagging_options = $options;
186
187     if (isset($tagging_options['limit']))
188     {
189       unset($tagging_options['limit']);
190     }
191
192     $taggings = self::getTaggings($tags, $tagging_options);
193     $result = array();
194
195     foreach ($taggings as $key => $tagging)
196     {
197       $c = new Criteria();
198       $c->add(TagPeer::NAME, $tags, Criteria::NOT_IN);
199       $c->add(TaggingPeer::TAGGABLE_ID, $tagging, Criteria::IN);
200       $c->add(TaggingPeer::TAGGABLE_MODEL, $key);
201       $c->addJoin(TaggingPeer::TAG_ID, TagPeer::ID);
202       $tags = TagPeer::doSelect($c);
203
204       foreach ($tags as $tag)
205       {
206         if (!isset($result[$tag->getName()]))
207         {
208           $result[$tag->getName()] = 0;
209         }
210
211         $result[$tag->getName()]++;
212       }
213     }
214
215     if (isset($options['limit']))
216     {
217       arsort($result);
218       $result = array_slice($result, 0, $options['limit'], true);
219     }
220
221     ksort($result);
222     return sfPropelActAsTaggableToolkit::normalize($result);
223   }
224
225   /**
226    * Retrieves the objects tagged with one or several tags.
227    *
228    * The second optionnal parameter permits to restrict the tag selection with
229    * different criterias
230    *
231    * @param      mixed       $tags
232    * @param      array       $options
233    */
234   public static function getTaggedWith($tags = array(), $options = array())
235   {
236     $tags = self::string2array($tags);
237
238     $taggings = self::getTaggings($tags, $options);
239     $result = array();
240
241     foreach ($taggings as $key => $tagging)
242     {
243       $c = new Criteria();
244       $peer = $key.'Peer';
245       $objects = call_user_func(array($peer, 'retrieveByPKs'), $tagging);
246
247       foreach ($objects as $object)
248       {
249         $result[] = $object;
250       }
251     }
252
253     return $result;
254   }
255
256
257   /*!
258    * Returns a criteria to select only objects tags with on or a set of tags
259    *
260    * @example : $objects = MyModel::doSelect( TagPeer::getTaggedWithCriteria('MyModel', 'some, tags') );
261    *
262    * @param  string  $model
263    * @param  string/array  $tags
264    * @param  Criteria  $c  Optionnal, if specified conditions will be added to it, overwise a new criteria will be created
265    * @return Criteria
266    */
267   public static function getTaggedWithCriteria( $model, $tags = array(), $c = null )
268   {
269     $tags = self::string2array( $tags );
270     $peer = $model.'Peer';
271
272     if ( !$c instanceof Criteria ) $c = new Criteria;
273
274     $c->addJoin( constant("$peer::ID"), TaggingPeer::TAGGABLE_ID );
275     $c->add( TaggingPeer::TAGGABLE_MODEL, $model );
276     $c->addJoin( TaggingPeer::TAG_ID, TagPeer::ID );
277     $c->add( TagPeer::NAME, $tags, Criteria::IN );
278     return $c;
279   }
280
281   /**
282    * Returns the taggings associated to one tag or a set of tags.
283    *
284    * The second optionnal parameter permits to restrict the results with
285    * different criterias
286    *
287    * @param      mixed       $tags
288    * @param      array       $options
289    */
290   private static function getTaggings($tags = array(), $options = array())
291   {
292     $tags = self::string2array($tags);
293
294     $c = new Criteria();
295     $c->addJoin(TagPeer::ID, TaggingPeer::TAG_ID);
296     $c->add(TagPeer::NAME, $tags, Criteria::IN);
297     $c->addGroupByColumn(TaggingPeer::TAGGABLE_ID);
298     $having = $c->getNewCriterion(TagPeer::COUNT, count($tags), Criteria::GREATER_EQUAL);
299     $c->addHaving($having);
300     $c->clearSelectColumns();
301     $c->addSelectColumn(TaggingPeer::TAGGABLE_MODEL);
302     $c->addSelectColumn(TaggingPeer::TAGGABLE_ID);
303
304     if (isset($options['model']))
305     {
306       $c->add(TaggingPeer::TAGGABLE_MODEL, $options['model']);
307     }
308
309     $param = array();
310     $sql = BasePeer::createSelectSql($c, $param);
311     $con = Propel::getConnection();
312     $stmt = $con->prepareStatement($sql);
313     $position = 1;
314
315     foreach ($tags as $tag)
316     {
317       $stmt->setString($position, $tag);
318       $position++;
319     }
320
321     if (isset($options['model']))
322     {
323       $stmt->setString($position++, $options['model']);
324     }
325
326     $stmt->setString($position, count($tags));
327     $rs = $stmt->executeQuery(ResultSet::FETCHMODE_NUM);
328     $taggings = array();
329
330     while ($rs->next())
331     {
332       $model = $rs->getString(1);
333
334       if (!isset($taggings[$model]))
335       {
336         $taggings[$model] = array();
337       }
338
339       $taggings[$model][] = $rs->getInt(2);
340     }
341
342     return $taggings;
343   }
344
345   /**
346    * Retrives a tag by his name.
347    *
348    * @param      String      $tagname
349    */
350   public static function retrieveByTagname($tagname)
351   {
352     $c = new Criteria();
353     $c->add(TagPeer::NAME, $tagname);
354     return TagPeer::doSelectOne($c);
355   }
356
357   /**
358    * Retrives a tag by his name. If it does not exist, creates it (but does not
359    * save it)
360    *
361    * @param      String      $tagname
362    */
363   public static function retrieveOrCreateByTagname($tagname)
364   {
365     // retrieve or create the tag
366     $tag = TagPeer::retrieveByTagName($tagname);
367
368     if (!$tag)
369     {
370       $tag = new Tag();
371       $tag->setName($tagname);
372     }
373
374     return $tag;
375   }
376
377
378
379
380   private function string2array( $tags )
381   {
382     if (is_string($tags))
383     {
384       if (false !== strpos($tags, ','))
385       {
386         $tags = explode(',', $tags);
387       }
388       else
389       {
390         $tags = array($tags);
391       }
392     }
393     return $tags;
394   }
395 }