]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/Profile_tag.php
Merge branch 'master' of /var/www/mublog
[quix0rs-gnu-social.git] / classes / Profile_tag.php
1 <?php
2 /**
3  * Table Definition for profile_tag
4  */
5 require_once INSTALLDIR.'/classes/Memcached_DataObject.php';
6
7 class Profile_tag extends Memcached_DataObject 
8 {
9     ###START_AUTOCODE
10     /* the code below is auto generated do not remove the above tag */
11
12     public $__table = 'profile_tag';                     // table name
13     public $tagger;                          // int(4)  primary_key not_null
14     public $tagged;                          // int(4)  primary_key not_null
15     public $tag;                             // varchar(64)  primary_key not_null
16     public $modified;                        // timestamp()   not_null default_CURRENT_TIMESTAMP
17
18     /* Static get */
19     function staticGet($k,$v=null)
20     { return Memcached_DataObject::staticGet('Profile_tag',$k,$v); }
21
22     /* the code above is auto generated do not remove the tag below */
23     ###END_AUTOCODE
24
25     static function getTags($tagger, $tagged) {
26         
27         $tags = array();
28
29         # XXX: store this in memcached
30         
31         $profile_tag = new Profile_tag();
32         $profile_tag->tagger = $tagger;
33         $profile_tag->tagged = $tagged;
34         
35         $profile_tag->find();
36         
37         while ($profile_tag->fetch()) {
38             $tags[] = $profile_tag->tag;
39         }
40         
41         $profile_tag->free();
42         
43         return $tags;
44     }
45     
46     static function setTags($tagger, $tagged, $newtags) {
47         
48         $oldtags = Profile_tag::getTags($tagger, $tagged);
49         
50         # Delete stuff that's old that not in new
51         
52         $to_delete = array_diff($oldtags, $newtags);
53         
54         # Insert stuff that's in new and not in old
55         
56         $to_insert = array_diff($newtags, $oldtags);
57         
58         $profile_tag = new Profile_tag();
59         
60         $profile_tag->tagger = $tagger;
61         $profile_tag->tagged = $tagged;
62         
63         $profile_tag->query('BEGIN');
64         
65         foreach ($to_delete as $deltag) {
66             $profile_tag->tag = $deltag;
67             $result = $profile_tag->delete();
68             if (!$result) {
69                 common_log_db_error($profile_tag, 'DELETE', __FILE__);
70                 return false;
71             }
72         }
73         
74         foreach ($to_insert as $instag) {
75             $profile_tag->tag = $instag;
76             $result = $profile_tag->insert();
77             if (!$result) {
78                 common_log_db_error($profile_tag, 'INSERT', __FILE__);
79                 return false;
80             }
81         }
82         
83         $profile_tag->query('COMMIT');
84         
85         return true;
86     }
87     
88     # Return profiles with a given tag
89     static function getTagged($tagger, $tag) {
90         $profile = new Profile();
91         $profile->query('SELECT profile.* ' .
92                         'FROM profile JOIN profile_tag ' .
93                         'ON profile.id = profile_tag.tagged ' .
94                         'WHERE profile_tag.tagger = ' . $tagger . ' ' .
95                         'AND profile_tag.tag = "' . $tag . '" ');
96         $tagged = array();
97         while ($profile->fetch()) {
98             $tagged[] = clone($profile);
99         }
100         return $tagged;
101     }
102 }