]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/Profile_tag_subscription.php
Merge branch '1.0.x' into emailregistration
[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                 // TRANS: Exception thrown when inserting a list subscription in the database fails.
54                 throw new Exception(_('Adding list subscription failed.'));
55             }
56
57             $ptag = Profile_list::staticGet('id', $peopletag->id);
58             $ptag->subscriberCount(true);
59
60             Event::handle('EndSubscribePeopletag', array($peopletag, $profile));
61             return $ptag;
62         }
63     }
64
65     static function remove($peopletag, $profile)
66     {
67         $sub = Profile_tag_subscription::pkeyGet(array('profile_tag_id' => $peopletag->id,
68                                               'profile_id' => $profile->id));
69
70         if (empty($sub)) {
71             // silence is golden?
72             return true;
73         }
74
75         if (Event::handle('StartUnsubscribePeopletag', array($peopletag, $profile))) {
76             $result = $sub->delete();
77
78             if (!$result) {
79                 common_log_db_error($sub, 'DELETE', __FILE__);
80                 // TRANS: Exception thrown when deleting a list subscription from the database fails.
81                 throw new Exception(_('Removing list subscription failed.'));
82             }
83
84             $peopletag->subscriberCount(true);
85
86             Event::handle('EndUnsubscribePeopletag', array($peopletag, $profile));
87             return true;
88         }
89     }
90
91     // called if a tag gets deleted / made private
92     static function cleanup($profile_list) {
93         $subs = new self();
94         $subs->profile_tag_id = $profile_list->id;
95         $subs->find();
96
97         while($subs->fetch()) {
98             $profile = Profile::staticGet('id', $subs->profile_id);
99             Event::handle('StartUnsubscribePeopletag', array($profile_list, $profile));
100             // Delete anyway
101             $subs->delete();
102             Event::handle('StartUnsubscribePeopletag', array($profile_list, $profile));
103         }
104     }
105
106     function insert()
107     {
108         $result = parent::insert();
109         if ($result) {
110             self::blow('profile_list:subscriber_count:%d', 
111                        $this->profile_tag_id);
112         }
113         return $result;
114     }
115
116     function delete()
117     {
118         $result = parent::delete();
119         if ($result) {
120             self::blow('profile_list:subscriber_count:%d', 
121                        $this->profile_tag_id);
122         }
123         return $result;
124     }
125 }