Renamed all SQL-related functions to camel-case notation
[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 - 2013 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 have this?
91         if (countSumTotalData(bigintval($earningId), 'user_earning', 'id', 'earning_id', TRUE, ' AND `earning_userid`=' . getMemberId()) == 1) {
92                 // Then update it
93                 $status = updateMemberEarning($earningId, $dailyAmount, $isActive);
94         } else {
95                 // Not found, so add it
96                 $status = insertMemberEarning($earningId, $dailyAmount, $isActive);
97         }
98
99         // Return status
100         return $status;
101 }
102
103 // Insert member earning entry
104 function insertMemberEarning ($earningId, $dailyAmount, $isActive = 'Y') {
105         // Insert the record
106         sqlQueryEscaped("INSERT INTO `{?_MYSQL_PREFIX?}_user_earning` (`earning_id`, `earning_userid`, `earning_daily_amount`, `earning_active`) VALUES(%s,%s,%s,'%s')",
107                 array(
108                         bigintval($earningId),
109                         getMemberId(),
110                         bigintval($dailyAmount),
111                         $isActive
112                 ), __FUNCTION__, __LINE__);
113
114         // Prepare content
115         $content = array(
116                 'insert_id'    => getSqlInsertId(),
117                 'earning_id'   => bigintval($earningId),
118                 'daily_amount' => bigintval($dailyAmount),
119                 'is_active'    => $isActive
120         );
121
122         // Load email template
123         $message = loadEmailTemplate('member_earning_added', $content, getMemberId());
124
125         // Send email out
126         sendEmail(getMemberId(), '{--MEMBER_EARNING_ADDED_SUBJECT--}', $message);
127
128         // Send admin notification
129         sendAdminNotification('{--ADMIN_EARNING_INSERTED_SUBJECT--}', 'admin_earning_added', $content, getMemberId());
130
131         // Return status
132         return ($content['insert_id'] > 0);
133 }
134
135 // Update a given earning amount
136 function updateMemberEarning ($earningId, $dailyAmount, $isActive = 'Y') {
137         // By default the user does subscribe to an earning
138         $moreSql = '';
139
140         // Does the user cancel the earning?
141         if ($isActive == 'N') {
142                 // Then update cancellation timestamp as well
143                 $moreSql = ',`earning_canceled`=NOW()';
144         } // END - if
145
146         // Update database record
147         sqlQueryEscaped("UPDATE
148         `{?_MYSQL_PREFIX?}_user_earning`
149 SET
150         `earning_daily_amount`=%s,
151         `earning_active`='%s'
152         " . $moreSql . "
153 WHERE
154         `earning_id`=%s AND
155         `earning_userid`=%s
156 LIMIT 1", array(
157                 bigintval($dailyAmount),
158                 $isActive,
159                 bigintval($earningId),
160                 getMemberId()
161         ), __FUNCTION__, __LINE__);
162
163         // Determine whether something has changed
164         $status = (!ifSqlHasZeroAffectedRows());
165
166         // Has the record changed?
167         if ($status === TRUE) {
168                 // Prepare content
169                 $content = array(
170                         'earning_id'   => bigintval($earningId),
171                         'daily_amount' => bigintval($dailyAmount),
172                         'is_active'    => $isActive
173                 );
174
175                 // Then load email template for user
176                 $message = loadEmailTemplate('member_earning_updated', $content, getMemberId());
177
178                 // Send email out
179                 sendEmail(getMemberId(), '{--MEMBER_EARNING_UPDATED_SUBJECT--}', $message);
180
181                 // Send admin notification
182                 sendAdminNotification('{--ADMIN_EARNING_UPDATED_SUBJECT--}', 'admin_earning_updated', $content, getMemberId());
183         } // END - if
184
185         // Return status
186         return $status;
187 }
188
189 // [EOF]
190 ?>