]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - classes/Profile_tag_subscription.php
staticGet for sub-Managed_DataObject classes now calls parent
[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 Managed_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     /* the code above is auto generated do not remove the tag below */
19     ###END_AUTOCODE
20
21     public static function schemaDef()
22     {
23         return array(
24             'fields' => array(
25                 'profile_tag_id' => array('type' => 'int', 'not null' => true, 'description' => 'foreign key to profile_tag'),
26                 'profile_id' => array('type' => 'int', 'not null' => true, 'description' => 'foreign key to profile table'),
27
28                 'created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date this record was created'),
29                 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),
30             ),
31             'primary key' => array('profile_tag_id', 'profile_id'),
32             'foreign keys' => array(
33                 'profile_tag_subscription_profile_list_id_fkey' => array('profile_list', array('profile_tag_id' => 'id')),
34                 'profile_tag_subscription_profile_id_fkey' => array('profile', array('profile_id' => 'id')),
35             ),
36             'indexes' => array(
37                 // @fixme probably we want a (profile_id, created) index here?
38                 'profile_tag_subscription_profile_id_idx' => array('profile_id'),
39                 'profile_tag_subscription_created_idx' => array('created'),
40             ),
41         );
42     }
43
44     function pkeyGet($kv)
45     {
46         return Memcached_DataObject::pkeyGet('Profile_tag_subscription', $kv);
47     }
48
49     static function add($peopletag, $profile)
50     {
51         if ($peopletag->private) {
52             return false;
53         }
54
55         if (Event::handle('StartSubscribePeopletag', array($peopletag, $profile))) {
56             $args = array('profile_tag_id' => $peopletag->id,
57                           'profile_id' => $profile->id);
58             $existing = Profile_tag_subscription::pkeyGet($args);
59             if(!empty($existing)) {
60                 return $existing;
61             }
62
63             $sub = new Profile_tag_subscription();
64             $sub->profile_tag_id = $peopletag->id;
65             $sub->profile_id = $profile->id;
66             $sub->created = common_sql_now();
67
68             $result = $sub->insert();
69
70             if (!$result) {
71                 common_log_db_error($sub, 'INSERT', __FILE__);
72                 // TRANS: Exception thrown when inserting a list subscription in the database fails.
73                 throw new Exception(_('Adding list subscription failed.'));
74             }
75
76             $ptag = Profile_list::staticGet('id', $peopletag->id);
77             $ptag->subscriberCount(true);
78
79             Event::handle('EndSubscribePeopletag', array($peopletag, $profile));
80             return $ptag;
81         }
82     }
83
84     static function remove($peopletag, $profile)
85     {
86         $sub = Profile_tag_subscription::pkeyGet(array('profile_tag_id' => $peopletag->id,
87                                               'profile_id' => $profile->id));
88
89         if (empty($sub)) {
90             // silence is golden?
91             return true;
92         }
93
94         if (Event::handle('StartUnsubscribePeopletag', array($peopletag, $profile))) {
95             $result = $sub->delete();
96
97             if (!$result) {
98                 common_log_db_error($sub, 'DELETE', __FILE__);
99                 // TRANS: Exception thrown when deleting a list subscription from the database fails.
100                 throw new Exception(_('Removing list subscription failed.'));
101             }
102
103             $peopletag->subscriberCount(true);
104
105             Event::handle('EndUnsubscribePeopletag', array($peopletag, $profile));
106             return true;
107         }
108     }
109
110     // called if a tag gets deleted / made private
111     static function cleanup($profile_list) {
112         $subs = new self();
113         $subs->profile_tag_id = $profile_list->id;
114         $subs->find();
115
116         while($subs->fetch()) {
117             $profile = Profile::staticGet('id', $subs->profile_id);
118             Event::handle('StartUnsubscribePeopletag', array($profile_list, $profile));
119             // Delete anyway
120             $subs->delete();
121             Event::handle('StartUnsubscribePeopletag', array($profile_list, $profile));
122         }
123     }
124
125     function insert()
126     {
127         $result = parent::insert();
128         if ($result) {
129             self::blow('profile_list:subscriber_count:%d', 
130                        $this->profile_tag_id);
131         }
132         return $result;
133     }
134
135     function delete()
136     {
137         $result = parent::delete();
138         if ($result) {
139             self::blow('profile_list:subscriber_count:%d', 
140                        $this->profile_tag_id);
141         }
142         return $result;
143     }
144 }