]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/Profile_tag_subscription.php
Merge branch 'people_tags_rebase' into 1.0.x
[quix0rs-gnu-social.git] / classes / Profile_tag_subscription.php
1 <?php
2 /**
3  * Table Definition for profile_tag_subscription
4  */
5 require_once INSTALLDIR.'/classes/Memcached_DataObject.php';
6
7 class Profile_tag_subscription 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_subscription';                     // table name
13     public $profile_tag_id;                         // int(4)  not_null
14     public $profile_id;                             // int(4)  not_null
15     public $created;                                // datetime   not_null default_0000-00-00%2000%3A00%3A00
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_subscription',$k,$v); }
21
22     /* the code above is auto generated do not remove the tag below */
23     ###END_AUTOCODE
24
25     function pkeyGet($kv)
26     {
27         return Memcached_DataObject::pkeyGet('Profile_tag_subscription', $kv);
28     }
29
30     static function add($peopletag, $profile)
31     {
32         if ($peopletag->private) {
33             return false;
34         }
35
36         if (Event::handle('StartSubscribePeopletag', array($peopletag, $profile))) {
37             $args = array('profile_tag_id' => $peopletag->id,
38                           'profile_id' => $profile->id);
39             $existing = Profile_tag_subscription::pkeyGet($args);
40             if(!empty($existing)) {
41                 return $existing;
42             }
43
44             $sub = new Profile_tag_subscription();
45             $sub->profile_tag_id = $peopletag->id;
46             $sub->profile_id = $profile->id;
47             $sub->created = common_sql_now();
48
49             $result = $sub->insert();
50
51             if (!$result) {
52                 common_log_db_error($sub, 'INSERT', __FILE__);
53                 throw new Exception(_("Adding people tag subscription failed."));
54             }
55
56             $ptag = Profile_list::staticGet('id', $peopletag->id);
57             $ptag->subscriberCount(true);
58
59             Event::handle('EndSubscribePeopletag', array($peopletag, $profile));
60             return $ptag;
61         }
62     }
63
64     static function remove($peopletag, $profile)
65     {
66         $sub = Profile_tag_subscription::pkeyGet(array('profile_tag_id' => $peopletag->id,
67                                               'profile_id' => $profile->id));
68
69         if (empty($sub)) {
70             // silence is golden?
71             return true;
72         }
73
74         if (Event::handle('StartUnsubscribePeopletag', array($peopletag, $profile))) {
75             $result = $sub->delete();
76
77             if (!$result) {
78                 common_log_db_error($sub, 'DELETE', __FILE__);
79                 throw new Exception(_("Removing people tag subscription failed."));
80             }
81
82             $peopletag->subscriberCount(true);
83
84             Event::handle('EndUnsubscribePeopletag', array($peopletag, $profile));
85             return true;
86         }
87     }
88
89     // called if a tag gets deleted / made private
90     static function cleanup($profile_list) {
91         $subs = new self();
92         $subs->profile_tag_id = $profile_list->id;
93         $subs->find();
94
95         while($subs->fetch()) {
96             $profile = Profile::staticGet('id', $subs->profile_id);
97             Event::handle('StartUnsubscribePeopletag', array($profile_list, $profile));
98             // Delete anyway
99             $subs->delete();
100             Event::handle('StartUnsubscribePeopletag', array($profile_list, $profile));
101         }
102     }
103 }