- Templates for surfbar (admin/member) added when URL got added while unlocking
[mailer.git] / inc / libs / surfbar_functions.php
index 9680740da282f097a687db8f23229b2c6f5d34ab..6f8449488bb0878f2c7c26879e9f963cc1307006 100644 (file)
@@ -46,13 +46,156 @@ function SURFBAR_ADMIN_ADD_URL ($url, $uid, $reward) {
        } // END - if
 
        // Check if that URL does not exist
-       if (SURFBAR_LOOKUP_URL($url, $uid)) {
+       if (SURFBAR_LOOKUP_BY_URL($url, $uid)) {
                // Already found!
                return false;
        } // END - if
 
        // Register the new URL
-       return SURFBAR_REGISTER_URL($url, $uid, $reward, "CONFIRMED");
+       return SURFBAR_REGISTER_URL($url, $uid, $reward, "CONFIRMED", "unlock");
+}
+// Looks up by an URL
+function SURFBAR_LOOKUP_BY_URL ($url) {
+       // Now lookup that given URL by itself
+       $urlArray = SURFBAR_GET_URL_DATA($url, "url");
+
+       // Was it found?
+       return (count($urlArray) > 0);
+}
+// Load URL data by given search term and column
+function SURFBAR_GET_URL_DATA ($searchTerm, $column="id", $order="id", $sort="ASC", $group="id") {
+       global $lastUrlData;
+
+       // By default nothing is found
+       $lastUrlData = array();
+
+       // Is the column an id number?
+       if (($column == "id") || ($column == "userid")) {
+               // Extra secure input
+               $searchTerm = bigintval($searchTerm);
+       } // END - if
+
+       // Look up the record
+       $result = SQL_QUERY_ESC("SELECT id, userid, url, reward, views_total, status, registered, last_locked, lock_reason
+FROM "._MYSQL_PREFIX."_surfbar_urls
+WHERE %s='%s'
+ORDER BY %s %s",
+               array($column, $searchTerm, $order, $sort), __FILE__, __LINE__);
+
+       // Is there at least one record?
+       if (SQL_NUMROWS($result) > 0) {
+               // Then load all!
+               while ($dataRow = SQL_FETCHARRAY($result)) {
+                       // Shall we group these results?
+                       if ($group == "id") {
+                               // Add the row by id as index
+                               $lastUrlData[$dataRow['id']] = $dataRow;
+                       } else {
+                               // Group entries
+                               $lastUrlData[$dataRow[$group]][$dataRow['id']] = $dataRow;
+                       }
+               } // END - while
+       } // END - if
+
+       // Free the result
+       SQL_FREERESULT($result);
+
+       // Return the result
+       return $lastUrlData;
+}
+// Registers an URL with the surfbar. You should have called SURFBAR_LOOKUP_BY_URL() first!
+function SURFBAR_REGISTER_URL ($url, $uid, $reward, $status="PENDING", $addMode="reg") {
+       global $_CONFIG;
+
+       // Make sure by the user registered URLs are always pending
+       if ($addMode == "reg") $status = "PENDING";
+
+       // Prepare content
+       $content = array(
+               'url'         => $url,
+               'frametester' => FRAMETESTER($url),
+               'uid'         => $uid,
+               'reward'      => TRANSLATE_COMMA($reward),
+               'status'      => SURFBAR_TRANSLATE_STATUS($status)
+       );
+
+       // Insert the URL into database
+       $content['insert_id'] = SURFBAR_INSERT_URL_BY_ARRAY($content);
+
+       // If in reg-mode we notify admin
+       if (($addMode == "reg") || ($_CONFIG['surfbar_notify_admin_unlock'] == "Y")) {
+               // Notify admin even when he as unlocked an email
+               SURFBAR_NOTIFY_ADMIN("url_{$addMode}", $content);
+       } // END - if
+
+       // Send mail to user
+       SURFBAR_NOTIFY_USER("url_{$addMode}", $content);
+
+       // Return the insert id
+       return $content['insert_id'];
+}
+// Inserts an url by given data array and return the insert id
+function SURFBAR_INSERT_URL_BY_ARRAY ($urlData) {
+       // Just run the insert query for now
+       SQL_QUERY_ESC("INSERT INTO "._MYSQL_PREFIX."_surfbar_urls (userid, url, reward, status) VALUES(%s, '%s', %s, '%s')",
+               array(
+                       bigintval($urlData['uid']),
+                       bigintval($urlData['url']),
+                       (float)$urlData['reward'],
+                       $urlData['status']
+               ), __FILE__, __LINE__
+       );
+
+       // Return insert id
+       return SQL_INSERTID();
+}
+// Notify admin(s) with a selected message and content
+function SURFBAR_NOTIFY_ADMIN ($messageType, $content) {
+       // Prepare template name
+       $templateName = sprintf("admin_surfbar_%s", $messageType);
+
+       // Prepare subject
+       $eval = sprintf("\$subject = ADMIN_SURFBAR_NOTIFY_%s_SUBJECT;",
+               strtoupper($messageType)
+       );
+       eval($eval);
+
+       // Send the notification out
+       SEND_ADMIN_NOTIFICATION($subject, $templateName, $content, $content['uid']);
+}
+// Notify the user about the performed action
+function SURFBAR_NOTIFY_USER ($messageType, $content) {
+       // Prepare template name
+       $templateName = sprintf("member_surfbar_%s", $messageType);
+
+       // Prepare subject
+       $eval = sprintf("\$subject = MEMBER_SURFBAR_NOTIFY_%s_SUBJECT;",
+               strtoupper($messageType)
+       );
+       eval($eval);
+
+       // Load template
+       $mailText = LOAD_EMAIL_TEMPLATE($templateName, $content);
+
+       // Send the email
+       SEND_EMAIL($content['uid'], $subject, $mailText);
+}
+// Translate the URL status
+function SURFBAR_TRANSLATE_STATUS ($status) {
+       // Create constant name
+       $constantName = sprintf("SURFBAR_URL_STATUS_%s", strtoupper($status));
+
+       // Set default translated status
+       $statusTranslated = "!".$constantName."!";
+
+       // Generate eval() command
+       if (defined($constantName)) {
+               $eval = "\$statusTranslated = ".$constantName.";";
+               eval($eval);
+       } // END - if
+
+       // Return result
+       return $statusTranslated;
 }
 //
 ?>