0209841f569ab7b518e1de70914046ea1c8a38fc
[mailer.git] / inc / libs / earning_functions.php
1 <?php
2 /************************************************************************
3  * Mailer v0.2.1-FINAL                                Start: 08/02/2011 *
4  * ===================                          Last change: 08/02/2011 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : earning_functions.php                            *
8  * -------------------------------------------------------------------- *
9  * Short description : Functions for ext-earning                        *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Funktionen fuer ext-earning                      *
12  * -------------------------------------------------------------------- *
13  * $Revision::                                                        $ *
14  * $Date::                                                            $ *
15  * $Tag:: 0.2.1-FINAL                                                 $ *
16  * $Author::                                                          $ *
17  * -------------------------------------------------------------------- *
18  * Copyright (c) 2003 - 2009 by Roland Haeder                           *
19  * Copyright (c) 2009 - 2011 by Mailer Developer Team                   *
20  * For more information visit: http://mxchange.org                      *
21  *                                                                      *
22  * This program is free software; you can redistribute it and/or modify *
23  * it under the terms of the GNU General Public License as published by *
24  * the Free Software Foundation; either version 2 of the License, or    *
25  * (at your option) any later version.                                  *
26  *                                                                      *
27  * This program is distributed in the hope that it will be useful,      *
28  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
29  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
30  * GNU General Public License for more details.                         *
31  *                                                                      *
32  * You should have received a copy of the GNU General Public License    *
33  * along with this program; if not, write to the Free Software          *
34  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
35  * MA  02110-1301  USA                                                  *
36  ************************************************************************/
37
38 // Some security stuff...
39 if (!defined('__SECURITY')) {
40         die();
41 } // END - if
42
43 // Translates the "earning group" into human-readable
44 function translateEarningGroup ($earningGroup) {
45         return sprintf("{--EARNING_GROUP_%s--}", $earningGroup);
46 }
47
48 // Translates the "earning name" into human-readable
49 function translateEarningProvider ($earningProvider) {
50         return sprintf("{--EARNING_PROVIDER_%s--}", $earningProvider);
51 }
52
53 /*
54  * Generates a table (with templates) to display data for extra earnings (to
55  * the member) by given "earning name".
56  *
57  * @param       $earningProvider        Name of the earning
58  * @return      $output                 Generated HTML output
59  */
60 function generateMemberEarningDataTable ($earningProvider) {
61         // Init array for filter
62         $filterData = array(
63                 // Minimum points to get
64                 'earning_min_points'     => 0,
65                 // Maximum points to get
66                 'earning_max_points'     => 0,
67                 // Count of all entries
68                 'earning_count'          => 0,
69                 // -- The following arry elements are "read-only": --
70                 // Exclude current userid
71                 'earning_exclude_userid' => getMemberId(),
72                 // "earning group"
73                 'earning_group'          => 'INVALID',
74                 // "earning name" again
75                 'earning_provider'       => $earningProvider
76         );
77
78         // Run the filter chain to get the data
79         $filterData = runFilterChain('member_earning_table_data', $filterData);
80
81         // Load the proper template and return it
82         $output = loadTemplate('member_earning_data_' . strtolower($filterData['earning_group']), true, $filterData);
83
84         // Return it
85         return $output;
86 }
87
88 // Handle earning id
89 function doMemberEarning ($earningId, $dailyAmount, $isActive = 'Y') {
90         // Does the user already have this earning?
91         $result = SQL_QUERY_ESC("SELECT COUNT(`id`) AS `cnt` FROM `{?_MYSQL_PREFIX?}_user_earning` WHERE `earning_id`=%s AND `earning_userid`=%s LIMIT 1",
92                 array(
93                         bigintval($earningId),
94                         getMemberId()
95                 ), __FUNCTION__, __LINE__);
96
97         // Get the count
98         list($count) = SQL_FETCHROW($result);
99
100         // Does the user have this?
101         if ($count == 1) {
102                 // Then update it
103                 $status = updateMemberEarning($earningId, $dailyAmount, $isActive);
104         } else {
105                 // Not found, so add it
106                 $status = insertMemberEarning($earningId, $dailyAmount, $isActive);
107         }
108
109         // Free result
110         SQL_FREERESULT($result);
111
112         // Return status
113         return $status;
114 }
115
116 // Insert member earning entry
117 function insertMemberEarning ($earningId, $dailyAmount, $isActive = 'Y') {
118         // Insert the record
119         SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_user_earning` (`earning_id`,`earning_userid`,`earning_daily_amount`,`earning_active`) VALUES(%s,%s,%s,'%s')",
120                 array(
121                         bigintval($earningId),
122                         getMemberId(),
123                         bigintval($dailyAmount),
124                         $isActive
125                 ), __FUNCTION__, __LINE__);
126
127         // Prepare content
128         $content = array(
129                 'insert_id'    => SQL_INSERTID(),
130                 'earning_id'   => bigintval($earningId),
131                 'daily_amount' => bigintval($dailyAmount),
132                 'is_active'    => $isActive
133         );
134
135         // Load email template
136         $message = loadEmailTemplate('member_earning_added', $content, getMemberId());
137
138         // Send email out
139         sendEmail(getMemberId(), '{--MEMBER_EARNING_ADDED_SUBJECT--}', $message);
140
141         // Send admin notification
142         sendAdminNotification('{--ADMIN_EARNING_INSERTED_SUBJECT--}', 'admin_earning_added', $content, getMemberId());
143
144         // Return status
145         return ($content['insert_id'] > 0);
146 }
147
148 // Update a given earning amount
149 function updateMemberEarning ($earningId, $dailyAmount, $isActive = 'Y') {
150         // By default the user does subscribe to an earning
151         $moreSql = '';
152
153         // Does the user cancel the earning?
154         if ($isActive == 'N') {
155                 // Then update cancellation timestamp as well
156                 $moreSql = ', `earning_canceled`=NOW()';
157         } // END - if
158
159         // Update database record
160         SQL_QUERY_ESC("UPDATE
161         `{?_MYSQL_PREFIX?}_user_earning`
162 SET
163         `earning_daily_amount`=%s,
164         `earning_active`='%s'
165         " . $moreSql . "
166 WHERE
167         `earning_id`=%s AND
168         `earning_userid`=%s
169 LIMIT 1", array(
170                 bigintval($dailyAmount),
171                 $isActive,
172                 bigintval($earningId),
173                 getMemberId()
174         ), __FUNCTION__, __LINE__);
175
176         // Determine wether something has changed
177         $status = (!SQL_HASZEROAFFECTED());
178
179         // Has the record changed?
180         if ($status === true) {
181                 // Prepare content
182                 $content = array(
183                         'earning_id'   => bigintval($earningId),
184                         'daily_amount' => bigintval($dailyAmount),
185                         'is_active'    => $isActive
186                 );
187
188                 // Then load email template for user
189                 $message = loadEmailTemplate('member_earning_updated', $content, getMemberId());
190
191                 // Send email out
192                 sendEmail(getMemberId(), '{--MEMBER_EARNING_UPDATED_SUBJECT--}', $message);
193
194                 // Send admin notification
195                 sendAdminNotification('{--ADMIN_EARNING_UPDATED_SUBJECT--}', 'admin_earning_updated', $content, getMemberId());
196         } // END - if
197
198         // Return status
199         return $status;
200 }
201
202 // [EOF]
203 ?>