+<?php
+/************************************************************************
+ * Mailer v0.2.1-FINAL Start: 08/02/2011 *
+ * =================== Last change: 08/02/2011 *
+ * *
+ * -------------------------------------------------------------------- *
+ * File : earning_functions.php *
+ * -------------------------------------------------------------------- *
+ * Short description : Functions for ext-earning *
+ * -------------------------------------------------------------------- *
+ * Kurzbeschreibung : Funktionen fuer ext-earning *
+ * -------------------------------------------------------------------- *
+ * $Revision:: $ *
+ * $Date:: $ *
+ * $Tag:: 0.2.1-FINAL $ *
+ * $Author:: $ *
+ * -------------------------------------------------------------------- *
+ * Copyright (c) 2003 - 2009 by Roland Haeder *
+ * Copyright (c) 2009 - 2011 by Mailer Developer Team *
+ * For more information visit: http://www.mxchange.org *
+ * *
+ * This program is free software; you can redistribute it and/or modify *
+ * it under the terms of the GNU General Public License as published by *
+ * the Free Software Foundation; either version 2 of the License, or *
+ * (at your option) any later version. *
+ * *
+ * This program is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
+ * GNU General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU General Public License *
+ * along with this program; if not, write to the Free Software *
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, *
+ * MA 02110-1301 USA *
+ ************************************************************************/
+
+// Some security stuff...
+if (!defined('__SECURITY')) {
+ die();
+} // END - if
+
+// Translates the "earning group" into human-readable
+function translateEarningGroup ($earningGroup) {
+ return sprintf("{--EARNING_GROUP_%s--}", $earningGroup);
+}
+
+// Translates the "earning name" into human-readable
+function translateEarningName ($earningName) {
+ return sprintf("{--EARNING_NAME_%s--}", $earningName);
+}
+
+/*
+ * Generates a table (with templates) to display data for extra earnings (to
+ * the member) by given "earning name".
+ *
+ * @param $earningName Name of the earning
+ * @return $output Generated HTML output
+ */
+function generateMemberEarningDataTable ($earningName) {
+ // Init array for filter
+ $filterData = array(
+ // Minimum points to get
+ 'earning_min_points' => 0,
+ // Maximum points to get
+ 'earning_max_points' => 0,
+ // Count of all entries
+ 'earning_count' => 0,
+ // -- The following arry elements are "read-only": --
+ // Exclude current userid
+ 'earning_exclude_userid' => getMemberId(),
+ // "earning group"
+ 'earning_group' => 'INVALID',
+ // "earning name" again
+ 'earning_name' => $earningName
+ );
+
+ // Run the filter chain to get the data
+ $filterData = runFilterChain('member_earning_table_data', $filterData);
+
+ // Load the proper template and return it
+ $output = loadTemplate('member_earning_data_' . strtolower($filterData['earning_group']), true, $filterData);
+
+ // Return it
+ return $output;
+}
+
+// Handle earning id
+function doMemberEarning ($earningId, $dailyAmount, $isActive = 'Y') {
+ // Does the user already have this earning?
+ $result = SQL_QUERY_ESC("SELECT COUNT(`id`) AS `cnt` FROM `{?_MYSQL_PREFIX?}_user_earning` WHERE `earning_id`=%s AND `earning_userid`=%s LIMIT 1",
+ array(
+ bigintval($earningId),
+ getMemberId()
+ ), __FUNCTION__, __LINE__);
+
+ // Get the count
+ list($count) = SQL_FETCHROW($result);
+
+ // Does the user have this?
+ if ($count == 1) {
+ // Then update it
+ $status = updateMemberEarning($earningId, $dailyAmount, $isActive);
+ } else {
+ // Not found, so add it
+ $status = insertMemberEarning($earningId, $dailyAmount, $isActive);
+ }
+
+ // Free result
+ SQL_FREERESULT($result);
+
+ // Return status
+ return $status;
+}
+
+// Insert member earning entry
+function insertMemberEarning ($earningId, $dailyAmount, $isActive = 'Y') {
+ // Insert the record
+ SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_user_earning` (`earning_id`,`earning_userid`,`earning_daily_amount`,`earning_active`) VALUES(%s,%s,%s,'%s')",
+ array(
+ bigintval($earningId),
+ getMemberId(),
+ bigintval($dailyAmount),
+ $isActive
+ ), __FUNCTION__, __LINE__);
+
+ // Prepare content
+ $content = array(
+ 'insert_id' => SQL_INSERTID(),
+ 'earning_id' => bigintval($earningId),
+ 'daily_amount' => bigintval($dailyAmount),
+ 'is_active' => $isActive
+ );
+
+ // Load email template
+ $message = loadEmailTemplate('member_earning_added', $content, getMemberId());
+
+ // Send email out
+ sendEmail(getMemberId(), '{--MEMBER_EARNING_ADDED_SUBJECT--}', $message);
+
+ // Send admin notification
+ sendAdminNotification('{--ADMIN_EARNING_INSERTED_SUBJECT--}', 'admin_earning_added', $content, getMemberId());
+
+ // Return status
+ return ($content['insert_id'] > 0);
+}
+
+// Update a given earning amount
+function updateMemberEarning ($earningId, $dailyAmount, $isActive = 'Y') {
+ // By default the user does subscribe to an earning
+ $moreSql = '';
+
+ // Does the user cancel the earning?
+ if ($isActive == 'N') {
+ // Then update cancellation timestamp as well
+ $moreSql = ', `earning_cancelled`=NOW()';
+ } // END - if
+
+ // Update database record
+ SQL_QUERY_ESC("UPDATE
+ `{?_MYSQL_PREFIX?}_user_earning`
+SET
+ `earning_daily_amount`=%s,
+ `earning_active`='%s'
+ " . $moreSql . "
+WHERE
+ `earning_id`=%s AND
+ `earning_userid`=%s
+LIMIT 1", array(
+ bigintval($dailyAmount),
+ $isActive,
+ bigintval($earningId),
+ getMemberId()
+ ), __FUNCTION__, __LINE__);
+
+ // Determine wether something has changed
+ $status = (!SQL_HASZEROAFFECTED());
+
+ // Has the record changed?
+ if ($status === true) {
+ // Prepare content
+ $content = array(
+ 'earning_id' => bigintval($earningId),
+ 'daily_amount' => bigintval($dailyAmount),
+ 'is_active' => $isActive
+ );
+
+ // Then load email template for user
+ $message = loadEmailTemplate('member_earning_updated', $content, getMemberId());
+
+ // Send email out
+ sendEmail(getMemberId(), '{--MEMBER_EARNING_UPDATED_SUBJECT--}', $message);
+
+ // Send admin notification
+ sendAdminNotification('{--ADMIN_EARNING_UPDATED_SUBJECT--}', 'admin_earning_updated', $content, getMemberId());
+ } // END - if
+
+ // Return status
+ return $status;
+}
+
+// [EOF]
+?>