+ return doSurfbarRegisterUrl($url, getMemberId(), 'PENDING', 'reg', array('limit' => $limit));
+}
+
+// Create list of actions depending on status for the user
+function generateSurfbarMemberActions ($urlId, $status) {
+ // Load all actions in an array for given status
+ $actionArray = getSurfbarArrayFromStatus($status);
+
+ // Calculate width
+ $width = round(100 / count($actionArray));
+
+ // "Walk" through all actions and create forms
+ $OUT = '';
+ foreach ($actionArray as $actionId => $action) {
+ // Add form for this action
+ $OUT .= loadTemplate('member_list_surfbar_form', TRUE, array(
+ 'width' => $width,
+ 'url_id' => bigintval($urlId),
+ 'action' => strtolower($action)
+ ));
+ } // END - foreach
+
+ // Load main template
+ $output = loadTemplate('member_list_surfbar_table', TRUE, $OUT);
+
+ // Return code
+ return $output;
+}
+
+// Do the member form request
+function doSurfbarMemberByFormData ($formData, $urlArray) {
+ // By default no action is performed
+ $performed = FALSE;
+
+ // Is this a member?
+ if (!isMember()) {
+ // No member!
+ //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Cannot add URL: isMember()=false');
+ return FALSE;
+ } elseif ((!isset($formData['url_id'])) || (!isset($formData['action']))) {
+ // Important form elements are missing!
+ //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Cannot add URL: Invalid form data, required field id/action not found');
+ return FALSE;
+ } elseif (!isset($urlArray[$formData['url_id']])) {
+ // Id not found in cache
+ //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Cannot add URL: Field url_id not found in cache');
+ return FALSE;
+ } elseif (!isSurfbarMemberActionStatusValid($formData['action'], $urlArray[$formData['url_id']]['url_status'])) {
+ // Action not allowed for current URL status
+ //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Cannot add URL: Action not allowed to perform. action=' . $formData['action'] . ',url_status=' . $urlArray[$formData['url_id']]['url_status'] . ',url_id=' . $formData['url_id']);
+ return FALSE;
+ }
+
+ // Secure action
+ $action = secureString($formData['action']);
+
+ // Has it changed?
+ if ($action != $formData['action']) {
+ // Invalid data in action found
+ return FALSE;
+ } // END - if
+
+ // Create the function name for selected action
+ $functionName = sprintf("doSurfbarMember%sAction", firstCharUpperCase($action));
+
+ // Is the function there?
+ if (function_exists($functionName)) {
+ // Load data and add it to globals array
+ $urlData = getSurfbarUrlData($formData['url_id']);
+ $GLOBALS['surfbar_cache'] = merge_array($GLOBALS['surfbar_cache'], $urlData[$formData['url_id']]);
+
+ // Add new status
+ $urlArray[$formData['url_id']]['new_status'] = getSurfbarNewStatus('new_status');
+
+ // Extract URL data for call-back
+ $urlData = array(merge_array($urlArray[$formData['url_id']], array($action => $formData)));
+
+ // Action found so execute it
+ $performed = call_user_func_array($functionName, $urlData);
+ } else {
+ // Log invalid request
+ reportBug(__FUNCTION__, __LINE__, 'Invalid member action! action=' . $formData['action'] . ',url_id=' . $formData['url_id'] . ',function=' . $functionName);
+ }
+
+ // Return status
+ return $performed;
+}
+
+// Getter for surfbar_actions table by given id number
+function getSurfbarActionsDataById ($columnName, $id) {
+ // Is cache set?
+ if (!isset($GLOBALS[__FUNCTION__][$id][$columnName])) {
+ // Default is not found
+ $GLOBALS[__FUNCTION__][$id][$columnName] = '*INVALID*';
+
+ // Search for it
+ $result = SQL_QUERY_ESC("SELECT `%s` FROM `{?_MYSQL_PREFIX?}_surfbar_actions` WHERE `actions_id`=%s LIMIT 1",
+ array(
+ $columnName,
+ bigintval($id)
+ ), __FUNCTION__, __LINE__);
+
+ // Is there an entry?
+ if (SQL_NUMROWS($result) == 1) {
+ // Load it
+ list($GLOBALS[__FUNCTION__][$id][$columnName]) = SQL_FETCHROW($result);
+ } // END - if
+
+ // Free result
+ SQL_FREERESULT($result);
+ } // END - if
+
+ // Return value
+ return $GLOBALS[__FUNCTION__][$id][$columnName];
+}
+
+// Validate if the requested action can be performed on current URL status
+function isSurfbarMemberActionStatusValid ($action, $status) {
+ // Search for the requested action/status combination in database
+ $result = SQL_QUERY_ESC("SELECT `actions_new_status` FROM `{?_MYSQL_PREFIX?}_surfbar_actions` WHERE `actions_action`='%s' AND `actions_status`='%s' LIMIT 1",
+ array(
+ strtoupper($action),
+ strtoupper($status)
+ ), __FUNCTION__, __LINE__);
+
+ // Is the entry there?
+ $isValid = (SQL_NUMROWS($result) == 1);
+
+ // Debug message
+ //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'action=' . $action . ',status=' . $status . ',isValid=' . intval($isValid));
+
+ // Fetch the new status if found
+ if ($isValid === TRUE) {
+ // Load new status
+ list($GLOBALS['surfbar_cache']['new_status']) = SQL_FETCHROW($result);
+ } // END - if
+
+ // Free result
+ SQL_FREERESULT($result);
+
+ // Return status
+ return $isValid;
+}
+
+//------------------------------------------------------------------------------
+// Member actions
+//------------------------------------------------------------------------------
+
+// Retreat a booked URL
+function doSurfbarMemberRetreatAction ($urlData) {
+ // Create the data array for next function call
+ $data = array(
+ $urlData['url_id'] => $urlData
+ );
+
+ // Simply change the status here
+ return changeSurfbarUrlStatus ($urlData['url_id'], $urlData['url_status'], $urlData['new_status'], $data);
+}
+
+// Book an URL now (from migration)
+function doSurfbarMemberBooknowAction ($urlData) {
+ // Create the data array for next function call
+ $data = array(
+ $urlData['url_id'] => $urlData
+ );
+
+ // Simply change the status here
+ return changeSurfbarUrlStatus ($urlData['url_id'], $urlData['url_status'], $urlData['new_status'], $data);
+}
+
+// Show edit form or do the changes
+function doSurfbarMemberEditAction ($urlData) {
+ // Is the "execute" flag there?
+ if (isset($urlData['edit']['execute'])) {
+ // Execute the changes
+ return executeSurfbarMemberAction('edit', $urlData);
+ } // END - if
+
+ // Display form
+ return displaySurfbarMemberActionForm('edit', $urlData);
+}
+
+// Show delete form or do the changes
+function doSurfbarMemberDeleteAction ($urlData) {
+ // Is the "execute" flag there?
+ if (isset($urlData['delete']['execute'])) {
+ // Execute the changes
+ return executeSurfbarMemberAction('delete', $urlData);
+ } // END - if
+
+ // Display form
+ return displaySurfbarMemberActionForm('delete', $urlData);
+}
+
+// Pause active banner
+function doSurfbarMemberPauseAction ($urlData) {
+ return changeSurfbarUrlStatus($urlData['url_id'], $urlData['url_status'], $urlData['new_status'], array($urlData['url_id'] => $urlData));
+}
+
+// Unpause stopped banner
+function doSurfbarMemberUnpauseAction ($urlData) {
+ // Fix missing entry for template
+ $urlData['edit'] = $urlData['unpause'];
+ $urlData['edit']['url'] = $urlData['url'];
+ $urlData['edit']['limit'] = getSurfbarViewsMax();
+
+ // Return status change
+ return changeSurfbarUrlStatus($urlData['url_id'], $urlData['url_status'], $urlData['new_status'], array($urlData['url_id'] => $urlData));
+}
+
+// Resubmit locked URL
+function doSurfbarMemberResubmitAction ($urlData) {
+ return changeSurfbarUrlStatus($urlData['url_id'], $urlData['url_status'], $urlData['new_status'], array($urlData['url_id'] => $urlData));
+}
+
+// Display selected "action form"
+function displaySurfbarMemberActionForm ($action, $urlData) {
+ // Translate some data if present
+ $content = prepareSurfbarContentForTemplate($urlData);
+
+ // Include fields only for action 'edit'
+ if ($action == 'edit') {
+ // Default is not limited
+ $urlData['limited_yes'] = '';
+ $urlData['limited_no'] = ' checked="checked"';
+ $urlData['limited'] = 'false';
+
+ // Is this URL limited?
+ if (getSurfbarViewsMax() > 0) {
+ // Then rewrite form data
+ $urlData['limited_yes'] = ' checked="checked"';
+ $urlData['limited_no'] = '';
+ $urlData['limited'] = 'true';
+ } // END - if
+ } // END - if
+
+ // Load the form and display it
+ loadTemplate(sprintf("member_surfbar_%s_action_form", $action), FALSE, $urlData);
+
+ // All fine by default ... ;-)
+ return TRUE;
+}
+
+// Execute choosen action
+function executeSurfbarMemberAction ($action, $urlData) {
+ // By default nothing is executed
+ $executed = FALSE;
+
+ // Is limitation "no" and "limit" is > 0?
+ if ((isset($urlData[$action]['limited'])) && ($urlData[$action]['limited'] != 'Y') && ((isset($urlData[$action]['limit'])) && ($urlData[$action]['limit'] > 0)) || (!isset($urlData[$action]['limit']))) {
+ // Set it to unlimited
+ $urlData[$action]['limit'] = '0';
+ } // END - if
+
+ // Construct function name
+ $functionName = sprintf("executeSurfbarMember%sAction", firstCharUpperCase($action));
+
+ // Is that function there?
+ if (function_exists($functionName)) {
+ // Execute the function
+ if (call_user_func_array($functionName, array($urlData)) == TRUE) {
+ // Update status as well
+ $executed = changeSurfbarUrlStatus($urlData['url_id'], $urlData['url_status'], $urlData['new_status'], array($urlData['url_id'] => $urlData));
+ } // END - if
+ } else {
+ // Not found
+ reportBug(__FUNCTION__, __LINE__, 'Callback function ' . $functionName . ' does not exist.');
+ }
+
+ // Return status
+ return $executed;
+}
+
+// "Execute edit" function: Update changed data
+function executeSurfbarMemberEditAction ($urlData) {
+ // Default is nothing done
+ $status = FALSE;
+
+ // Has the URL or limit changed?
+ if (TRUE) {
+ // @TODO if (($urlData['url_views_allowed'] != $urlData['edit']['limit']) || ($url1 != $url2)) {
+ // Run the query
+ SQL_QUERY_ESC("UPDATE
+ `{?_MYSQL_PREFIX?}_surfbar_urls`
+SET
+ `url`='%s',
+ `url_views_allowed`=%s,
+ `url_views_max`=%s
+WHERE
+ `url_id`=%s AND
+ `url_status`='%s'
+LIMIT 1",
+ array(
+ $urlData['url'],
+ $urlData['edit']['limit'],
+ $urlData['edit']['limit'],
+ $urlData['url_id'],
+ $urlData['url_status']
+ ), __FUNCTION__, __LINE__);
+
+ // All fine
+ $status = TRUE;
+ } // END - if
+
+ // Return status
+ return $status;
+}
+
+// "Execute delete" function: Does nothing...
+function executeSurfbarMemberDeleteAction ($urlData) {
+ // Nothing special to do (see above function for such "special actions" to perform)
+ return TRUE;
+}
+
+//------------------------------------------------------------------------------
+// Self-maintenance functions
+//------------------------------------------------------------------------------
+
+// Main function
+function doSurfbarSelfMaintenance () {
+ // Handle URLs which limit has depleted so we can stop them
+ doHandleSurfbarDepletedViews();
+
+ // Handle low-points amounts
+ doHandleSurfbarLowPoints();
+}
+
+// Handle URLs which limit has depleted
+function doHandleSurfbarDepletedViews () {
+ // Get all URLs
+ $urlArray = getSurfbarUrlData(0, 'url_views_max', 'url_id', 'ASC', 'url_id', " AND `url_views_allowed` > 0 AND `url_status`='ACTIVE'");
+
+ // Are there some entries?
+ if (count($urlArray) > 0) {
+ // Then handle all!
+ foreach ($urlArray as $id => $urlData) {
+ // Backup data
+ $data = $urlData;
+
+ // Rewrite array for next call
+ $urlData = array(
+ $id => $data
+ );
+
+ // Handle the status
+ changeSurfbarUrlStatus($id, 'ACTIVE', 'DEPLETED', $urlData);
+ } // END - foreach
+ } // END - if