root/trunk/lib/model/SnippetTagPeer.php

Revision 17, 2.3 kB (checked in by fabien, 5 years ago)

tweaked appearance of tag cloud

  • 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 'model/om/BaseSnippetTagPeer.php';
5  
6   // include object class
7   include_once 'model/SnippetTag.php';
8
9
10 /**
11  * Skeleton subclass for performing query and update operations on the 'sn_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 SnippetTagPeer extends BaseSnippetTagPeer
22 {
23   public static function getAllWithCount($max = 100)
24   {
25     $tags = array();
26
27     $con = Propel::getConnection();
28     $query = '
29       SELECT t1.name AS tag,
30       COUNT(t1.name) AS count
31       FROM '.SnippetTagPeer::TABLE_NAME.' AS t1
32       GROUP BY t1.name
33       ORDER BY count DESC
34     ';
35
36     $stmt = $con->prepareStatement($query);
37     $stmt->setLimit($max);
38     $rs = $stmt->executeQuery();
39     $max_popularity = 0;
40     while ($rs->next())
41     {
42       $tags[$rs->getString('tag')] = $rs->getInt('count');
43     }
44
45     ksort($tags);
46
47     return $tags;
48   }
49  
50   public function getPopular($levels = 3, $floor = 0.2, $power = 0.4)
51   {
52     $all_tags = self::getAllWithCount(200);
53     $max_count = max($all_tags);
54     $tags = array();
55     foreach($all_tags as $tag => $count)
56     {
57       if($count/$max_count > $floor)
58       {
59         $tags[$tag] = intval(($levels-1)*pow($count/$max_count, $power)+1);
60       }
61     }
62     return $tags;
63   }
64
65   public static function getForSnippetsWithCount($snippets, $excluded_tags, $max = 100)
66   {
67     $snippet_ids = array();
68     foreach($snippets as $snippet)
69     {
70       $snippet_ids[] = $snippet->getId();
71     }
72
73     $con = Propel::getConnection();
74     $query = '
75       SELECT '.SnippetTagPeer::NAME.' AS tag,
76       COUNT('.SnippetTagPeer::NAME.') AS count
77       FROM '.SnippetTagPeer::TABLE_NAME.'
78       WHERE '.SnippetTagPeer::SNIPPET_ID.' IN ('.implode(' ,', $snippet_ids).')
79       GROUP BY '.SnippetTagPeer::NAME.'
80       ORDER BY '.SnippetTagPeer::NAME.' ASC
81     ';
82
83     $stmt = $con->prepareStatement($query);
84     $stmt->setLimit($max);
85     $rs = $stmt->executeQuery();
86
87     $tags = array();
88     while ($rs->next())
89     {
90       $tag = $rs->getString('tag');
91       if(array_search($tag, $excluded_tags) === false)
92       {
93         $tags[$tag] = $rs->getInt('count');
94       }
95     }
96
97     return $tags;
98   }
99 }
100
Note: See TracBrowser for help on using the browser.