2 /************************************************************************
3 * Mailer v0.2.1-FINAL Start: 07/25/2011 *
4 * =================== Last change: 07/25/2011 *
6 * -------------------------------------------------------------------- *
7 * File : forced_functions.php *
8 * -------------------------------------------------------------------- *
9 * Short description : Functions for ext-forced *
10 * -------------------------------------------------------------------- *
11 * Kurzbeschreibung : Funktionen fuer ext-forced *
12 * -------------------------------------------------------------------- *
15 * $Tag:: 0.2.1-FINAL $ *
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 *
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. *
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. *
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, *
36 ************************************************************************/
38 // Some security stuff...
39 if (!defined('__SECURITY')) {
43 // Translate forced ad type
44 function translateForcedAdsType ($status) {
45 // Is the status null or empty?
46 if (is_null($status)) {
49 } elseif (empty($status)) {
54 // Default status is unknown
55 $return = '{%message,ADMIN_FORCED_ADS_TYPE_UNKNOWN=' . $status . '%}';
57 // Construct message id
58 $messageId = 'ADMIN_FORCED_ADS_TYPE_' . $status;
61 if (isMessageIdValid($messageId)) {
62 // Then use it as message string
63 $return = '{--' . $messageId . '--}';
70 // Translate forced campaign status
71 function translateForcedCampaignStatus ($status) {
72 // Is the status null or empty?
73 if (is_null($status)) {
76 } elseif (empty($status)) {
81 // Default status is unknown
82 $return = '{%message,ADMIN_FORCED_CAMPAIGN_TYPE_UNKNOWN=' . $status . '%}';
84 // Construct message id
85 $messageId = 'ADMIN_FORCED_CAMPAIGN_TYPE_' . $status;
88 if (isMessageIdValid($messageId)) {
89 // Then use it as message string
90 $return = '{--' . $messageId . '--}';
97 // Translate visibility
98 function translateForcedCostsVisibility ($visibility) {
99 // Is the status null or empty?
100 if (is_null($visibility)) {
102 $visibility = 'NULL';
103 } elseif (empty($visibility)) {
105 $visibility = 'EMPTY';
108 // Default status is unknown
109 $return = '{%message,ADMIN_FORCED_ADS_VISIBILITY_UNKNOWN=' . $visibility . '%}';
111 // Construct message id
112 $messageId = 'ADMIN_FORCED_ADS_VISIBILITY_' . $visibility;
115 if (isMessageIdValid($messageId)) {
116 // Then use it as message string
117 $return = '{--' . $messageId . '--}';
124 // "Getter" for an array of data from all forced-popups except user's own
125 function getForcedPopupDataByEarningArray ($content) {
126 die(__FUNCTION__.':content=<pre>' . print_r($content, TRUE) . '</pre>' . debug_get_printable_backtrace());
128 // Finally return the content
132 // Generator (somewhat getter) for forced-ads type options
133 function generateForcedAdsTypeOptions ($type = NULL) {
135 if (!isset($GLOBALS[__FUNCTION__][$type])) {
136 // Generate output and cache it
137 $GLOBALS[__FUNCTION__][$type] = generateOptions(
148 'translateForcedAdsType'
153 return $GLOBALS[__FUNCTION__][$type];
156 // Generator (somewhat getter) for forcec-costs visibility options
157 function generateForcedCostsVisibilityOptions ($visibility = NULL) {
159 if (!isset($GLOBALS[__FUNCTION__][$visibility])) {
160 // Generate output and cache it
161 $GLOBALS[__FUNCTION__][$visibility] = generateOptions(
173 'translateForcedCostsVisibility'
178 return $GLOBALS[__FUNCTION__][$visibility];
181 // "Getter" for an array of all given forced ads
182 function getArrayFromForcedAdsByType ($forcedType = NULL) {
184 $forcedAds = array();
186 // Init WHERE (default)
189 // Is forced type not NULL?
190 if (!is_null($forcedType)) {
191 // Then set proper WHERE
192 $where = sprintf("WHERE `forced_ads_type`='%s'", sqlEscapeString($forcedType));
195 // Search all given types
196 $result = sqlQuery('SELECT
199 `forced_ads_click_url`,
202 `forced_ads_alt_text`,
203 `forced_ads_framebreaker`,
205 `forced_ads_views_counter`,
206 `forced_ads_clicks_counter`,
207 UNIX_TIMESTAMP(`forced_ads_added`) AS `forced_ads_added`,
210 `{?_MYSQL_PREFIX?}_forced_ads`
213 `forced_ads_id` ASC', __FUNCTION__, __LINE__);
215 // Is there at least one entry?
216 if (!ifSqlHasZeroNums($result)) {
218 while ($content = sqlFetchArray($result)) {
220 $forcedAds[$content['forced_ads_id']] = $content;
225 sqlFreeResult($result);
231 // "Getter" for an array of all forced costs from given visibility or all
232 function getArrayFromForcedCostsByVisibility ($visibility = NULL) {
234 $forcedCosts = array();
237 // Is visibility not NULL?
238 if (!is_null($visibility)) {
239 // Then add WHERE statement
240 $where = sprintf(" WHERE `forced_costs_visibility`='%s'", sqlEscapeString($visibility));
243 // Search all given types
244 $result = sqlQuery("SELECT
246 `forced_costs_price_click`,
247 `forced_costs_payment_click`,
248 `forced_costs_visibility`
250 `{?_MYSQL_PREFIX?}_forced_costs`
253 `forced_costs_id` ASC", __FUNCTION__, __LINE__);
255 // Is there at least one entry?
256 if (!ifSqlHasZeroNums($result)) {
258 while ($content = sqlFetchArray($result)) {
260 $forcedCosts[$content['forced_costs_id']] = $content;
265 sqlFreeResult($result);
271 // "Getter" for an array of all forced campaigns from given array and status
272 function getArrayFromForcedCampaignsByArray ($ids, $status) {
274 $forcedCampaigns = array();
276 // Search all given types
277 $result = sqlQueryEscaped("SELECT
278 `forced_campaign_id`,
279 UNIX_TIMESTAMP(`forced_campaign_created`) AS `forced_campaign_created`,
280 `forced_campaign_status`,
281 UNIX_TIMESTAMP(`forced_campaign_last_locked`) AS `forced_campaign_last_locked`,
282 `forced_campaign_lock_reason`,
283 `forced_campaign_expired`,
284 `forced_campaign_ordered_clicks`,
285 `forced_campaign_payment_api`,
286 `forced_campaign_reload_lock`,
287 `forced_campaign_minimum_stay`,
289 `forced_campaign_userid`,
292 `{?_MYSQL_PREFIX?}_forced_campaigns`
294 `forced_campaign_id` IN (%s) AND
295 `forced_campaign_status`='%s'
297 `forced_campaign_id` ASC",
301 ), __FUNCTION__, __LINE__);
303 // Is there at least one entry?
304 if (!ifSqlHasZeroNums($result)) {
306 while ($content = sqlFetchArray($result)) {
308 $forcedCampaigns[$content['forced_campaign_id']] = $content;
313 sqlFreeResult($result);
316 return $forcedCampaigns;
319 // "Activates" forced campaigns from POST data
320 function doForcedAdminActivateCampaigns () {
321 // Do some sanity checks
323 // Not allowed to call
325 } elseif (countPostSelection('forced_campaign_id') == 0) {
326 // No selections made
330 // Get an array from all pending campaigns
331 $forcedCampaigns = getArrayFromForcedCampaignsByArray(array_keys(postRequestElement('forced_campaign_id')), 'PENDING');
333 // By default all worked (at this point)
336 // Run through all entries
337 foreach ($forcedCampaigns as $campaignId => $campaignData) {
338 // Change the status of this campaign
339 if (!changeForcedCampaignStatus($campaignId, 'PENDING', 'ACTIVE', $campaignData)) {
340 // One didn't work, but continue with others
349 // Changes the status of a forced campaign from previous to new status
350 function changeForcedCampaignStatus ($campaignId, $prevStatus, $newStatus, $data) {
351 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'campaignId=' . $campaignId . ',prevStatus=' . $prevStatus . ',data[]=' . gettype($data) . ',newStatus=' . $newStatus . ' - ENTERED!');
352 // Make new status always lower-case
353 $newStatus = strtolower($newStatus);
357 'campaign_id' => $campaignId,
358 'prev_status' => $prevStatus,
359 'new_status' => $newStatus,
363 // Run pre filter chain
364 $filterData = runFilterChain('pre_change_forced_campaign_status', $filterData);
367 if (!is_null($filterData['abort'])) {
369 return $filterData['abort'];
372 // Update the status now
373 // ---------- Comment out for debugging/developing member actions! ---------
374 sqlQueryEscaped("UPDATE `{?_MYSQL_PREFIX?}_forced_campaigns` SET `forced_campaign_status`='%s' WHERE `forced_campaign_id`=%s LIMIT 1",
377 bigintval($campaignId)
378 ), __FUNCTION__, __LINE__);
379 // ---------- Comment out for debugging/developing member actions! ---------
382 // ---------- Comment out for debugging/developing member actions! ---------
383 if (sqlAffectedRows() != 1) {
384 // No, something went wrong
387 // ---------- Comment out for debugging/developing member actions! ---------
389 // Run post filter chain
390 $filterData = runFilterChain('post_change_forced_campaign_status', $filterData);
392 // Check if generic 'data' is there
393 assert(isset($filterData['data']));
396 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'campaignId=' . $campaignId . ',prevStatus=' . $prevStatus . ',newStatus=' . $newStatus . ' - EXIT!');
400 // Notify admin(s) with a selected message and content
401 function doForcedNotifyAdmin ($messageType, $content) {
402 // Prepare template name
403 $templateName = sprintf('admin_forced_%s', $messageType);
405 // Is the user id valid?
406 if (!isValidId($content['forced_campaign_userid'])) {
407 // No, then use admin-only template
408 $templateName = sprintf('admin_only_forced_%s', $messageType);
412 $subject = sprintf('{--ADMIN_FORCED_NOTIFY_%s_SUBJECT--}', strtoupper($messageType));
414 // Is the subject line there?
415 if ((substr($subject, 0, 1) == '!') && (substr($subject, -1, 1) == '!')) {
416 // Set default subject if following eval() wents wrong
417 $subject = '{%message,ADMIN_FORCED_NOTIFY_DEFAULT_SUBJECT=' . strtoupper($messageType) . '%}';
420 // Translate some data if present
421 $content = prepareForcedContentForTemplate($content);
423 // Send the notification out
424 return sendAdminNotification($subject, $templateName, $content, $content['forced_campaign_userid']);
427 // Notify the user about the performed action
428 function doForcedNotifyMember ($messageType, $content) {
429 // Skip notification if userid is NULL
430 if (!isValidId($content['forced_campaign_userid'])) {
431 // NULL is silently ignored
435 // Prepare template name
436 $templateName = sprintf('member_forced_%s', $messageType);
439 $subject = sprintf('{--MEMBER_FORCED_NOTIFY_%s_SUBJECT--}', strtoupper($messageType));
441 // Is the subject line there?
442 if ((substr($subject, 0, 1) == '!') && (substr($subject, -1, 1) == '!')) {
443 // Set default subject if following eval() wents wrong
444 $subject = '{--MEMBER_FORCED_NOTIFY_DEFAULT_SUBJECT--}';
447 // Translate some data if present
448 $content = prepareForcedContentForTemplate($content);
451 $mailText = loadEmailTemplate($templateName, $content, $content['forced_campaign_userid']);
454 return sendEmail($content['forced_campaign_userid'], $subject, $mailText);
457 // Translates some data for template usage
458 // @TODO Can't we use our new expression language instead of this ugly code?
459 function prepareForcedContentForTemplate ($content) {
461 if (isset($content['forced_campaign_created'])) $content['forced_campaign_created'] = generateDateTime($content['forced_campaign_created'], 2);
463 // Return translated content
467 // ----------------------------------------------------------------------------
468 // Template helper functions
469 // ----------------------------------------------------------------------------
471 // Template helper function to create selection box for all forced-ad types
472 function doTemplateForcedAdTypeSelectionBox ($templateName, $clear = FALSE, $default = NULL) {
473 // Add all types through the filter
474 $forcedAdTypes = runFilterChain('add_forced_ads_types', array());
476 // Handle it over to generateSelectionBoxFromArray()
477 $content = generateSelectionBoxFromArray($forcedAdTypes, 'forced_ads_type', 'type', '', '', '', $default);
479 // Return prepared content
483 // Template helper function to create selection box for costs visibility
484 function doTemplateForcedCostsVisiblitySelectionBox ($templateName, $clear = FALSE, $default = NULL) {
486 $costsVisibility = array(
487 0 => array('visibility' => 'ALL'),
488 1 => array('visibility' => 'MEMBER'),
489 2 => array('visibility' => 'SPONSOR'),
490 3 => array('visibility' => 'NONE')
493 // Handle it over to generateSelectionBoxFromArray()
494 $content = generateSelectionBoxFromArray($costsVisibility, 'forced_costs_visibility', 'visibility', '', '', '', $default);
496 // Return prepared content
500 // Template helper function to create selection box for foreced ads
501 function doTemplateForcedAdSelectionBox ($templateName, $clear, $default = NULL) {
502 // Get back an array of all forced popups
503 $forcedAds = getArrayFromForcedAdsByType();
505 // Handle it over to generateSelectionBoxFromArray()
506 $content = generateSelectionBoxFromArray($forcedAds, 'forced_ads_id', 'forced_ads_id', '', '', '', $default, 'forced_ads_type');
508 // Return prepared content
512 // Template helper function to create selection box for foreced costs
513 function doTemplateForcedCostsSelectionBox ($templateName, $clear, $default = NULL) {
514 // Get back an array of all forced popups
515 $forcedCosts = getArrayFromForcedCostsByVisibility();
517 // Handle it over to generateSelectionBoxFromArray()
518 $content = generateSelectionBoxFromArray($forcedCosts, 'forced_costs_id', 'forced_costs_id', '', '', '', $default, '', TRUE);
520 // Return prepared content
524 // Template helper function to create selection box for foreced campaign userid
525 function doTemplateForcedCampaignUseridSelectionBox ($templateName, $clear, $default = NULL) {
526 // Generate the selection box without all
527 $content = addMemberSelectionBox($default, FALSE, TRUE, TRUE, 'forced_campaign_userid');
529 // Return prepared content
533 // ----------------------------------------------------------------------------
534 // XML call-back functions
535 // ----------------------------------------------------------------------------
537 // For 'doing' add forced-ads, the column-index is required
538 function addXmlSpecialAdminAddDoForcedAds () {
539 // So set it all here
540 $GLOBALS['__COLUMN_INDEX']['doXmlCallbackFunction'] = 'column';
541 $GLOBALS['__XML_ARGUMENTS']['doXmlCallbackFunction']['column_index'] = 'column';
544 // For 'doing' add forced-campaigns, the column-index is required
545 function addXmlSpecialAdminAddDoForcedCampaigns () {
546 // So set it all here
547 $GLOBALS['__COLUMN_INDEX']['doXmlCallbackFunction'] = 'column';
548 $GLOBALS['__XML_ARGUMENTS']['doXmlCallbackFunction']['column_index'] = 'column';
551 // For 'doing' add forced-costs, the column-index is required
552 function addXmlSpecialAdminAddDoForcedCosts () {
553 // So set it all here
554 $GLOBALS['__COLUMN_INDEX']['doXmlCallbackFunction'] = 'column';
555 $GLOBALS['__XML_ARGUMENTS']['doXmlCallbackFunction']['column_index'] = 'column';