]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Sample/User_greeting_count.php
More info for a proper, fancy-url lighttpd setup
[quix0rs-gnu-social.git] / plugins / Sample / User_greeting_count.php
1 <?php
2 /**
3  * Data class for counting greetings
4  *
5  * PHP version 5
6  *
7  * @category Data
8  * @package  StatusNet
9  * @author   Evan Prodromou <evan@status.net>
10  * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
11  * @link     http://status.net/
12  *
13  * StatusNet - the distributed open-source microblogging tool
14  * Copyright (C) 2009, StatusNet, Inc.
15  *
16  * This program is free software: you can redistribute it and/or modify
17  * it under the terms of the GNU Affero General Public License as published by
18  * the Free Software Foundation, either version 3 of the License, or
19  * (at your option) any later version.
20  *
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.     See the
24  * GNU Affero General Public License for more details.
25  *
26  * You should have received a copy of the GNU Affero General Public License
27  * along with this program. If not, see <http://www.gnu.org/licenses/>.
28  */
29
30 if (!defined('STATUSNET')) {
31     exit(1);
32 }
33
34 require_once INSTALLDIR . '/classes/Memcached_DataObject.php';
35
36 /**
37  * Data class for counting greetings
38  *
39  * We use the DB_DataObject framework for data classes in StatusNet. Each
40  * table maps to a particular data class, making it easier to manipulate
41  * data.
42  *
43  * Data classes should extend Memcached_DataObject, the (slightly misnamed)
44  * extension of DB_DataObject that provides caching, internationalization,
45  * and other bits of good functionality to StatusNet-specific data classes.
46  *
47  * @category Action
48  * @package  StatusNet
49  * @author   Evan Prodromou <evan@status.net>
50  * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
51  * @link     http://status.net/
52  *
53  * @see      DB_DataObject
54  */
55 class User_greeting_count extends Managed_DataObject
56 {
57     public $__table = 'user_greeting_count'; // table name
58     public $user_id;                         // int(4)  primary_key not_null
59     public $greeting_count;                  // int(4)
60     public $created;                         // datetime()   not_null
61     public $modified;                        // datetime   not_null default_0000-00-00%2000%3A00%3A00
62
63     public static function schemaDef()
64     {
65         return array(
66             'fields' => array(
67                 'user_id' => array('type' => 'int', 'not null' => true, 'description' => 'user id'),
68                 'greeting_count' => array('type' => 'int', 'not null' => true, 'description' => 'the greeting count'),
69                 'created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date this record was created'),
70                 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),
71             ),
72             'primary key' => array('user_id'),
73             'foreign keys' => array(
74                 'user_greeting_count_user_id_fkey' => array('user', array('user_id' => 'id')),
75             ),
76         );
77     }
78
79     /**
80      * Increment a user's greeting count and return instance
81      *
82      * This method handles the ins and outs of creating a new greeting_count for a
83      * user or fetching the existing greeting count and incrementing its value.
84      *
85      * @param integer $user_id ID of the user to get a count for
86      *
87      * @return User_greeting_count instance for this user, with count already incremented.
88      */
89     static function inc($user_id)
90     {
91         $gc = User_greeting_count::getKV('user_id', $user_id);
92
93         if (empty($gc)) {
94             $gc = new User_greeting_count();
95
96             $gc->user_id        = $user_id;
97             $gc->greeting_count = 1;
98
99             $result = $gc->insert();
100
101             if (!$result) {
102                 // TRANS: Exception thrown when the user greeting count could not be saved in the database.
103                 // TRANS: %d is a user ID (number).
104                 throw Exception(sprintf(_m('Could not save new greeting count for %d.'),
105                                         $user_id));
106             }
107         } else {
108             $orig = clone($gc);
109
110             $gc->greeting_count++;
111
112             $result = $gc->update($orig);
113
114             if (!$result) {
115                 // TRANS: Exception thrown when the user greeting count could not be saved in the database.
116                 // TRANS: %d is a user ID (number).
117                 throw Exception(sprintf(_m('Could not increment greeting count for %d.'),
118                                         $user_id));
119             }
120         }
121
122         return $gc;
123     }
124 }