mailer project continued:
[mailer.git] / inc / libs / forced_functions.php
index 011621f52e97865d6ebebc6d097dd8f178eac662..6d22963d26cfc1dd248df999d45dd7eb4177796f 100644 (file)
@@ -16,7 +16,7 @@
  * $Author::                                                          $ *
  * -------------------------------------------------------------------- *
  * Copyright (c) 2003 - 2009 by Roland Haeder                           *
- * Copyright (c) 2009 - 2011 by Mailer Developer Team                   *
+ * Copyright (c) 2009 - 2012 by Mailer Developer Team                   *
  * For more information visit: http://mxchange.org                      *
  *                                                                      *
  * This program is free software; you can redistribute it and/or modify *
@@ -40,20 +40,35 @@ if (!defined('__SECURITY')) {
        die();
 } // END - if
 
-// Template helper function to create selection box for all forced-ad types
-function doTemplateForcedAdTypeSelectionBox ($templateName, $clear = false, $default = NULL) {
-       // Add all types through the filter
-       $forcedAdTypes = runFilterChain('add_forced_ads_types', array());
+// Translate forced ad type
+function translateForcedAdsType ($status) {
+       // Is the status null or empty?
+       if (is_null($status)) {
+               // Is NULL
+               $status = 'NULL';
+       } elseif (empty($status)) {
+               // Is empty (string)
+               $status = 'EMPTY';
+       } // END - if
 
-       // Handle it over to generateSelectionBoxFromArray()
-       $content = generateSelectionBoxFromArray($forcedAdTypes, 'forced_ads_type', 'type');
+       // Default status is unknown
+       $return = '{%message,ADMIN_FORCED_ADS_TYPE_UNKNOWN=' . $status . '%}';
 
-       // Return prepared content
-       return $content;
+       // Construct message id
+       $messageId = 'ADMIN_FORCED_ADS_TYPE_' . $status;
+
+       // Is it there?
+       if (isMessageIdValid($messageId)) {
+               // Then use it as message string
+               $return = '{--' . $messageId . '--}';
+       } // END - if
+
+       // Return the status
+       return $return;
 }
 
-// Translate the status IN/OUT
-function translateForcedAdsType ($status) {
+// Translate forced campaign status
+function translateForcedCampaignStatus ($status) {
        // Is the status null or empty?
        if (is_null($status)) {
                // Is NULL
@@ -64,10 +79,10 @@ function translateForcedAdsType ($status) {
        } // END - if
 
        // Default status is unknown
-       $return = '{%message,ADMIN_FORCED_ADS_TYPE_UNKNOWN=' . $status . '%}';
+       $return = '{%message,ADMIN_FORCED_CAMPAIGN_TYPE_UNKNOWN=' . $status . '%}';
 
        // Construct message id
-       $messageId = 'ADMIN_FORCED_ADS_TYPE_' . $status;
+       $messageId = 'ADMIN_FORCED_CAMPAIGN_TYPE_' . $status;
 
        // Is it there?
        if (isMessageIdValid($messageId)) {
@@ -79,17 +94,47 @@ function translateForcedAdsType ($status) {
        return $return;
 }
 
-// "Getter" for an array of data from all Forced-PopUps
+// Translate visibility
+function translateForcedCostsVisibility ($visibility) {
+       // Is the status null or empty?
+       if (is_null($visibility)) {
+               // Is NULL
+               $visibility = 'NULL';
+       } elseif (empty($visibility)) {
+               // Is empty (string)
+               $visibility = 'EMPTY';
+       } // END - if
+
+       // Default status is unknown
+       $return = '{%message,ADMIN_FORCED_ADS_VISIBILITY_UNKNOWN=' . $visibility . '%}';
+
+       // Construct message id
+       $messageId = 'ADMIN_FORCED_ADS_VISIBILITY_' . $visibility;
+
+       // Is it there?
+       if (isMessageIdValid($messageId)) {
+               // Then use it as message string
+               $return = '{--' . $messageId . '--}';
+       } // END - if
+
+       // Return the status
+       return $return;
+}
+
+// "Getter" for an array of data from all Forced-PopUps except user's
 function getForcedPopupDataByEarningArray ($content) {
-       debug_report_bug(__FUNCTION__, __LINE__, 'content=' . print_r($content, true));
+       die('content=<pre>' . print_r($content, true) . '</pre>' . debug_get_printable_backtrace());
+
+       // Finally return the content
+       return $content;
 }
 
-// Generator (somewhat getter) for network type options
+// Generator (somewhat getter) for forced-ads type options
 function generateForcedAdsTypeOptions ($type =  NULL) {
        // Is this cached?
        if (!isset($GLOBALS[__FUNCTION__][$type])) {
                // Generate output and cache it
-               $GLOBALS[__FUNCTION__][$type] = generateOptionList(
+               $GLOBALS[__FUNCTION__][$type] = generateOptions(
                        '/ARRAY/',
                        array(
                                'BANNER',
@@ -108,5 +153,408 @@ function generateForcedAdsTypeOptions ($type =  NULL) {
        return $GLOBALS[__FUNCTION__][$type];
 }
 
+// Generator (somewhat getter) for forcec-costs visibility options
+function generateForcedCostsVisibilityOptions ($visibility =  NULL) {
+       // Is this cached?
+       if (!isset($GLOBALS[__FUNCTION__][$visibility])) {
+               // Generate output and cache it
+               $GLOBALS[__FUNCTION__][$visibility] = generateOptions(
+                       '/ARRAY/',
+                       array(
+                               'ALL',
+                               'MEMBER',
+                               'SPONSOR',
+                               'NONE'
+                       ),
+                       array(),
+                       $visibility,
+                       '', '',
+                       array(),
+                       'translateForcedCostsVisibility'
+               );
+       } // END - if
+
+       // Return content
+       return $GLOBALS[__FUNCTION__][$visibility];
+}
+
+// "Getter" for an array of all given forced ads
+function getArrayFromForcedAdsByType ($forcedType = NULL) {
+       // Init array
+       $forcedAds = array();
+
+       // Init WHERE (default)
+       $where = '';
+
+       // Is forced type not NULL?
+       if (!is_null($forcedType)) {
+               // Then set proper WHERE
+               $where = sprintf("WHERE `forced_ads_type`='%s'", SQL_ESCAPE($forcedType));
+       } // END - if
+
+       // Search all given types
+       $result = SQL_QUERY('SELECT
+       `forced_ads_id`,
+       `forced_ads_ad_url`,
+       `forced_ads_click_url`,
+       `forced_ads_width`,
+       `forced_ads_height`,
+       `forced_ads_alt_text`,
+       `forced_ads_framebreaker`,
+       `forced_ads_type`,
+       `forced_ads_views_counter`,
+       `forced_ads_clicks_counter`,
+       UNIX_TIMESTAMP(`forced_ads_added`) AS `forced_ads_added`,
+       `forced_ads_userid`
+FROM
+       `{?_MYSQL_PREFIX?}_forced_ads`
+' . $where . '
+ORDER BY
+       `forced_ads_id` ASC', __FUNCTION__, __LINE__);
+
+       // Do we have at least one entry?
+       if (!SQL_HASZERONUMS($result)) {
+               // Get all elements
+               while ($content = SQL_FETCHARRAY($result)) {
+                       // Add it
+                       $forcedAds[$content['forced_ads_id']] = $content;
+               } // END - while
+       } // END - if
+
+       // Free result
+       SQL_FREERESULT($result);
+
+       // Return it
+       return $forcedAds;
+}
+
+// "Getter" for an array of all forced costs from given visibility or all
+function getArrayFromForcedCostsByVisibility ($visibility = NULL) {
+       // Init array
+       $forcedCosts = array();
+       $where = '';
+
+       // Is visibility not NULL?
+       if (!is_null($visibility)) {
+               // Then add WHERE statement
+               $where = sprintf(" WHERE `forced_costs_visibility`='%s'", SQL_ESCAPE($visibility));
+       } // END - if
+
+       // Search all given types
+       $result = SQL_QUERY("SELECT
+       `forced_costs_id`,
+       `forced_costs_price_click`,
+       `forced_costs_payment_click`,
+       `forced_costs_visibility`
+FROM
+       `{?_MYSQL_PREFIX?}_forced_costs`
+".$where."
+ORDER BY
+       `forced_costs_id` ASC", __FUNCTION__, __LINE__);
+
+       // Do we have at least one entry?
+       if (!SQL_HASZERONUMS($result)) {
+               // Get all elements
+               while ($content = SQL_FETCHARRAY($result)) {
+                       // Add it
+                       $forcedCosts[$content['forced_costs_id']] = $content;
+               } // END - while
+       } // END - if
+
+       // Free result
+       SQL_FREERESULT($result);
+
+       // Return it
+       return $forcedCosts;
+}
+
+// "Getter" for an array of all forced campaigns from given array and status
+function getArrayFromForcedCampaignsByArray ($ids, $status) {
+       // Init array
+       $forcedCampaigns = array();
+
+       // Search all given types
+       $result = SQL_QUERY_ESC("SELECT
+       `forced_campaign_id`,
+       UNIX_TIMESTAMP(`forced_campaign_created`) AS `forced_campaign_created`,
+       `forced_campaign_status`,
+       UNIX_TIMESTAMP(`forced_campaign_last_locked`) AS `forced_campaign_last_locked`,
+       `forced_campaign_lock_reason`,
+       `forced_campaign_expired`,
+       `forced_campaign_ordered_clicks`,
+       `forced_campaign_payment_api`,
+       `forced_campaign_reload_lock`,
+       `forced_campaign_minimum_stay`,
+       `forced_ads_id`,
+       `forced_campaign_userid`,
+       `forced_costs_id`
+FROM
+       `{?_MYSQL_PREFIX?}_forced_campaigns`
+WHERE
+       `forced_campaign_id` IN (%s) AND
+       `forced_campaign_status`='%s'
+ORDER BY
+       `forced_campaign_id` ASC",
+               array(
+                       implode(', ', $ids),
+                       $status
+               ), __FUNCTION__, __LINE__);
+
+       // Do we have at least one entry?
+       if (!SQL_HASZERONUMS($result)) {
+               // Get all elements
+               while ($content = SQL_FETCHARRAY($result)) {
+                       // Add it
+                       $forcedCampaigns[$content['forced_campaign_id']] = $content;
+               } // END - while
+       } // END - if
+
+       // Free result
+       SQL_FREERESULT($result);
+
+       // Return it
+       return $forcedCampaigns;
+}
+
+// "Activates" forced campaigns from POST data
+function doForcedAdminActivateCampaigns () {
+       // Do some sanity checks
+       if (!isAdmin()) {
+               // Not allowed to call
+               return false;
+       } elseif (countPostSelection('forced_campaign_id') == 0) {
+               // No selections made
+               return false;
+       }
+
+       // Get an array from all pending campaigns
+       $forcedCampaigns = getArrayFromForcedCampaignsByArray(array_keys(postRequestElement('forced_campaign_id')), 'PENDING');
+
+       // By default all worked (at this point)
+       $activated = true;
+
+       // Run through all entries
+       foreach ($forcedCampaigns as $campaignId => $campaignData) {
+               // Change the status of this campaign
+               if (!changeForcedCampaignStatus($campaignId, 'PENDING', 'ACTIVE', $campaignData)) {
+                       // One didn't work, but continue with others
+                       $activated = false;
+               } // END - if
+       } // END - foreach
+
+       // Return status
+       return $activated;
+}
+
+// Changes the status of a forced campaign from previous to new status
+function changeForcedCampaignStatus ($campaignId, $prevStatus, $newStatus, $data) {
+       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'campaignId=' . $campaignId . ',prevStatus=' . $prevStatus . ',data[]=' . gettype($data) . ',newStatus=' . $newStatus . ' - ENTERED!');
+       // Make new status always lower-case
+       $newStatus = strtolower($newStatus);
+
+       // Prepare array
+       $filterData =  array(
+               'campaign_id' => $campaignId,
+               'prev_status' => $prevStatus,
+               'new_status'  => $newStatus,
+               'data'        => $data,
+               'abort'       => NULL
+       );
+
+       // Run pre filter chain
+       $filterData = runFilterChain('pre_change_forced_campaign_status', $filterData);
+
+       // Abort here?
+       if (!is_null($filterData['abort'])) {
+               // Abort here
+               return $filterData['abort'];
+       } // END - if
+
+       // Update the status now
+       // ---------- Comment out for debugging/developing member actions! ---------
+       SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_forced_campaigns` SET `forced_campaign_status`='%s' WHERE `forced_campaign_id`=%s LIMIT 1",
+               array(
+                       $newStatus,
+                       bigintval($campaignId)
+               ), __FUNCTION__, __LINE__);
+       // ---------- Comment out for debugging/developing member actions! ---------
+
+       // Was that fine?
+       // ---------- Comment out for debugging/developing member actions! ---------
+       if (SQL_AFFECTEDROWS() != 1) {
+               // No, something went wrong
+               return false;
+       } // END - if
+       // ---------- Comment out for debugging/developing member actions! ---------
+
+       // Run post filter chain
+       $filterData = runFilterChain('post_change_forced_campaign_status', $filterData);
+
+       // Check if generic 'data' is there
+       assert(isset($filterData['data']));
+
+       // All done!
+       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'campaignId=' . $campaignId . ',prevStatus=' . $prevStatus . ',newStatus=' . $newStatus . ' - EXIT!');
+       return true;
+}
+
+// Notify admin(s) with a selected message and content
+function doForcedNotifyAdmin ($messageType, $content) {
+       // Prepare template name
+       $templateName = sprintf("admin_forced_%s", $messageType);
+
+       // Prepare subject
+       $subject = sprintf("{--ADMIN_FORCED_NOTIFY_%s_SUBJECT--}",
+               strtoupper($messageType)
+       );
+
+       // Is the subject line there?
+       if ((substr($subject, 0, 1) == '!') && (substr($subject, -1, 1) == '!')) {
+               // Set default subject if following eval() wents wrong
+               $subject = '{%message,ADMIN_FORCED_NOTIFY_DEFAULT_SUBJECT=' . strtoupper($messageType) . '%}';
+       } // END - if
+
+       // Translate some data if present
+       $content = prepareForcedContentForTemplate($content);
+
+       // Send the notification out
+       return sendAdminNotification($subject, $templateName, $content, $content['forced_campaign_userid']);
+}
+
+// Notify the user about the performed action
+function doForcedNotifyMember ($messageType, $content) {
+       // Skip notification if userid is NULL
+       if (is_null($content['forced_campaign_userid'])) {
+               // NULL is silently ignored
+               return true;
+       } // END - if
+
+       // Prepare template name
+       $templateName = sprintf("member_forced_%s", $messageType);
+
+       // Prepare subject
+       $subject = sprintf("{--MEMBER_FORCED_NOTIFY_%s_SUBJECT--}",
+               strtoupper($messageType)
+       );
+
+       // Is the subject line there?
+       if ((substr($subject, 0, 1) == '!') && (substr($subject, -1, 1) == '!')) {
+               // Set default subject if following eval() wents wrong
+               $subject = '{--MEMBER_FORCED_NOTIFY_DEFAULT_SUBJECT--}';
+       } // END - if
+
+       // Translate some data if present
+       $content = prepareForcedContentForTemplate($content);
+
+       // Load template
+       $mailText = loadEmailTemplate($templateName, $content, $content['forced_campaign_userid']);
+
+       // Send the email
+       return sendEmail($content['forced_campaign_userid'], $subject, $mailText);
+}
+
+// Translates some data for template usage
+// @TODO Can't we use our new expression language instead of this ugly code?
+function prepareForcedContentForTemplate ($content) {
+       // Prepare some code
+       if (isset($content['forced_campaign_created'])) $content['forced_campaign_created'] = generateDateTime($content['forced_campaign_created'], 2);
+
+       // Return translated content
+       return $content;
+}
+
+// ----------------------------------------------------------------------------
+//                            Template helper functions
+// ----------------------------------------------------------------------------
+
+// Template helper function to create selection box for all forced-ad types
+function doTemplateForcedAdTypeSelectionBox ($templateName, $clear = false, $default = NULL) {
+       // Add all types through the filter
+       $forcedAdTypes = runFilterChain('add_forced_ads_types', array());
+
+       // Handle it over to generateSelectionBoxFromArray()
+       $content = generateSelectionBoxFromArray($forcedAdTypes, 'forced_ads_type', 'type', '', '', '', $default);
+
+       // Return prepared content
+       return $content;
+}
+
+// Template helper function to create selection box for costs visibility
+function doTemplateForcedCostsVisiblitySelectionBox ($templateName, $clear = false, $default = NULL) {
+       // Init array
+       $costsVisibility = array(
+               0 => array('visibility' => 'ALL'),
+               1 => array('visibility' => 'MEMBER'),
+               2 => array('visibility' => 'SPONSOR'),
+               3 => array('visibility' => 'NONE')
+       );
+
+       // Handle it over to generateSelectionBoxFromArray()
+       $content = generateSelectionBoxFromArray($costsVisibility, 'forced_costs_visibility', 'visibility', '', '', '', $default);
+
+       // Return prepared content
+       return $content;
+}
+
+// Template helper function to create selection box for foreced ads
+function doTemplateForcedAdSelectionBox ($templateName, $clear, $default = NULL) {
+       // Get back an array of all forced popups
+       $forcedAds = getArrayFromForcedAdsByType();
+
+       // Handle it over to generateSelectionBoxFromArray()
+       $content = generateSelectionBoxFromArray($forcedAds, 'forced_ads_id', 'forced_ads_id', '', '', '', $default, 'forced_ads_type');
+
+       // Return prepared content
+       return $content;
+}
+
+// Template helper function to create selection box for foreced costs
+function doTemplateForcedCostsSelectionBox ($templateName, $clear, $default = NULL) {
+       // Get back an array of all forced popups
+       $forcedCosts = getArrayFromForcedCostsByVisibility();
+
+       // Handle it over to generateSelectionBoxFromArray()
+       $content = generateSelectionBoxFromArray($forcedCosts, 'forced_costs_id', 'forced_costs_id', '', '', '', $default, '', true);
+
+       // Return prepared content
+       return $content;
+}
+
+// Template helper function to create selection box for foreced campaign userid
+function doTemplateForcedCampaignUseridSelectionBox ($templateName, $clear, $default = NULL) {
+       // Generate the selection box without all
+       $content = addMemberSelectionBox($default, false, true, true, 'forced_campaign_userid');
+
+       // Return prepared content
+       return $content;
+}
+
+// ----------------------------------------------------------------------------
+//                             XML call-back functions
+// ----------------------------------------------------------------------------
+
+// For 'doing' add forced-ads, the column-index is required
+function addXmlSpecialAdminAddDoForcedAds () {
+       // So set it all here
+       $GLOBALS['__COLUMN_INDEX']['doXmlCallbackFunction']  = 'column';
+       $GLOBALS['__XML_ARGUMENTS']['doXmlCallbackFunction']['extra_values'] = array();
+       $GLOBALS['__XML_ARGUMENTS']['doXmlCallbackFunction']['column_index'] = 'column';
+}
+
+// For 'doing' add forced-campaigns, the column-index is required
+function addXmlSpecialAdminAddDoForcedCampaigns () {
+       // So set it all here
+       $GLOBALS['__COLUMN_INDEX']['doXmlCallbackFunction']  = 'column';
+       $GLOBALS['__XML_ARGUMENTS']['doXmlCallbackFunction']['column_index'] = 'column';
+}
+
+// For 'doing' add forced-costs, the column-index is required
+function addXmlSpecialAdminAddDoForcedCosts () {
+       // So set it all here
+       $GLOBALS['__COLUMN_INDEX']['doXmlCallbackFunction']  = 'column';
+       $GLOBALS['__XML_ARGUMENTS']['doXmlCallbackFunction']['extra_values'] = array();
+       $GLOBALS['__XML_ARGUMENTS']['doXmlCallbackFunction']['column_index'] = 'column';
+}
+
 // [EOF]
 ?>