]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/Profile_tag.php
fix problem with dupe tags in profile
[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         $newtags = array_unique($newtags);
49         $oldtags = Profile_tag::getTags($tagger, $tagged);
50
51         # Delete stuff that's old that not in new
52
53         $to_delete = array_diff($oldtags, $newtags);
54
55         # Insert stuff that's in new and not in old
56
57         $to_insert = array_diff($newtags, $oldtags);
58
59         $profile_tag = new Profile_tag();
60
61         $profile_tag->tagger = $tagger;
62         $profile_tag->tagged = $tagged;
63
64         $profile_tag->query('BEGIN');
65
66         foreach ($to_delete as $deltag) {
67             $profile_tag->tag = $deltag;
68             $result = $profile_tag->delete();
69             if (!$result) {
70                 common_log_db_error($profile_tag, 'DELETE', __FILE__);
71                 return false;
72             }
73         }
74
75         foreach ($to_insert as $instag) {
76             $profile_tag->tag = $instag;
77             $result = $profile_tag->insert();
78             if (!$result) {
79                 common_log_db_error($profile_tag, 'INSERT', __FILE__);
80                 return false;
81             }
82         }
83
84         $profile_tag->query('COMMIT');
85
86         return true;
87     }
88
89     # Return profiles with a given tag
90     static function getTagged($tagger, $tag) {
91         $profile = new Profile();
92         $profile->query('SELECT profile.* ' .
93                         'FROM profile JOIN profile_tag ' .
94                         'ON profile.id = profile_tag.tagged ' .
95                         'WHERE profile_tag.tagger = ' . $tagger . ' ' .
96                         'AND profile_tag.tag = "' . $tag . '" ');
97         $tagged = array();
98         while ($profile->fetch()) {
99             $tagged[] = clone($profile);
100         }
101         return $tagged;
102     }
103 }