Renamed function so it might be more understandable
[mailer.git] / inc / template-functions.php
index f8e5da36b78e16e5e12412884112e8800c7b22cf..b675c6b6dc522617cd22de76010508efe72eed31 100644 (file)
@@ -213,10 +213,10 @@ function doFinalCompilation ($code, $insertComments = true, $enableCodes = true)
        enableTemplateHtml($insertComments);
 
        // Init counter
-       $cnt = 0;
+       $count = 0;
 
        // Compile all out
-       while (((strpos($code, '{--') !== false) || (strpos($code, '{DQUOTE}') !== false) || (strpos($code, '{?') !== false) || (strpos($code, '{%') !== false)) && ($cnt < 5)) {
+       while (((strpos($code, '{--') !== false) || (strpos($code, '{DQUOTE}') !== false) || (strpos($code, '{?') !== false) || (strpos($code, '{%') !== false)) && ($count < 5)) {
                // Init common variables
                $content = array();
                $newContent = '';
@@ -239,7 +239,7 @@ function doFinalCompilation ($code, $insertComments = true, $enableCodes = true)
                $code = $newContent;
 
                // Count round
-               $cnt++;
+               $count++;
        } // END - while
 
        // Return the compiled code
@@ -415,7 +415,7 @@ function detectExtraTemplatePath ($template) {
 }
 
 // Loads an email template and compiles it
-function loadEmailTemplate ($template, $content = array(), $userid = '0') {
+function loadEmailTemplate ($template, $content = array(), $userid = '0', $loadUserData = true) {
        global $DATA;
 
        // Make sure all template names are lowercase!
@@ -452,7 +452,7 @@ function loadEmailTemplate ($template, $content = array(), $userid = '0') {
 
                // Merge data if valid
                //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "content()=".count($content)." - PRE<br />");
-               if (isUserDataValid()) {
+               if ((isUserDataValid()) && ($loadUserData === true)) {
                        $content = merge_array($content, getUserDataArray());
                } // END - if
                //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "content()=".count($content)." - AFTER<br />");
@@ -766,9 +766,9 @@ function addSelectionBox ($type, $default, $prefix = '', $id = '0', $class = 'fo
 // Insert the code in $img_code into jpeg or PNG image
 function generateImageOrCode ($img_code, $headerSent = true) {
        // Is the code size oversized or shouldn't we display it?
-       if ((strlen($img_code) > 6) || (empty($img_code)) || (getConfig('code_length') == '0')) {
+       if ((strlen($img_code) > 6) || (empty($img_code)) || (getCodeLength() == '0')) {
                // Stop execution of function here because of over-sized code length
-               debug_report_bug(__FUNCTION__, __LINE__, 'img_code ' . $img_code .' has invalid length. img_code()=' . strlen($img_code) . ' code_length=' . getConfig('code_length'));
+               debug_report_bug(__FUNCTION__, __LINE__, 'img_code ' . $img_code .' has invalid length. img_code()=' . strlen($img_code) . ' code_length=' . getCodeLength());
        } elseif ($headerSent === false) {
                // Return an HTML code here
                return '<img src="{%url=img.php?code=' . $img_code . '%}" alt="Image" />';
@@ -778,26 +778,24 @@ function generateImageOrCode ($img_code, $headerSent = true) {
        $img = sprintf("%s/theme/%s/images/code_bg.%s",
                getPath(),
                getCurrentTheme(),
-               getConfig('img_type')
+               getImgType()
        );
 
        // Is it readable?
        if (isFileReadable($img)) {
                // Switch image type
-               switch (getConfig('img_type')) {
-                       case 'jpg':
-                               // Okay, load image and hide all errors
+               switch (getImgType()) {
+                       case 'jpg': // Okay, load image and hide all errors
                                $image = imagecreatefromjpeg($img);
                                break;
 
-                       case 'png':
-                               // Okay, load image and hide all errors
+                       case 'png': // Okay, load image and hide all errors
                                $image = imagecreatefrompng($img);
                                break;
                } // END - switch
        } else {
-               // Exit function here
-               logDebugMessage(__FUNCTION__, __LINE__, sprintf("File for image type %s not found.", getConfig('img_type')));
+               // Silently log the error
+               logDebugMessage(__FUNCTION__, __LINE__, sprintf("File for image-type %s in theme %s not found.", getImgType(), getCurrentTheme()));
                return;
        }
 
@@ -808,10 +806,10 @@ function generateImageOrCode ($img_code, $headerSent = true) {
        imagestring($image, 5, 14, 2, $img_code, $text_color);
 
        // Return to browser
-       sendHeader('Content-Type: image/' . getConfig('img_type'));
+       setContentType('image/' . getImgType());
 
        // Output image with matching image factory
-       switch (getConfig('img_type')) {
+       switch (getImgType()) {
                case 'jpg': imagejpeg($image); break;
                case 'png': imagepng($image);  break;
        } // END - switch
@@ -1097,28 +1095,28 @@ function generateEmailLink ($email, $table = 'admins') {
 // Output error messages in a fasioned way and die...
 function app_die ($F, $L, $message) {
        // Check if Script is already dieing and not let it kill itself another 1000 times
-       if (!isset($GLOBALS['app_died'])) {
-               // Make sure, that the script realy realy diese here and now
-               $GLOBALS['app_died'] = true;
+       if (isset($GLOBALS['app_died'])) {
+               // Script tried to kill itself twice
+               die('[' . __FUNCTION__ . ':' . __LINE__ . ']: Script wanted to kill itself more than once! Raw message=' . $message . ', file/function=' . $F . ', line=' . $L);
+       } // END - if
 
-               // Set content type as text/html
-               setContentType('text/html');
+       // Make sure, that the script realy realy diese here and now
+       $GLOBALS['app_died'] = true;
 
-               // Load header
-               loadIncludeOnce('inc/header.php');
+       // Set content type as text/html
+       setContentType('text/html');
 
-               // Rewrite message for output
-               $message = sprintf(getMessage('MAILER_HAS_DIED'), basename($F), $L, $message);
+       // Load header
+       loadIncludeOnce('inc/header.php');
 
-               // Load the message template
-               loadTemplate('app_die_message', false, $message);
+       // Rewrite message for output
+       $message = sprintf(getMessage('MAILER_HAS_DIED'), basename($F), $L, $message);
 
-               // Load footer
-               loadIncludeOnce('inc/footer.php');
-       } else {
-               // Script tried to kill itself twice
-               die('['.__FUNCTION__.':'.__LINE__.']: Script wanted to kill itself more than once! Raw message=' . $message . ', file/function=' . $F . ', line=' . $L);
-       }
+       // Load the message template
+       loadTemplate('app_die_message', false, $message);
+
+       // Load footer
+       loadIncludeOnce('inc/footer.php');
 }
 
 // Display parsing time and number of SQL queries in footer
@@ -1325,7 +1323,7 @@ function linenumberCode ($code)    {
 
                // Add code
                $r .= '<span class="linetext">' . encodeEntities($c) . '</span></div>';
-       }
+       } // END - foreach
 
        return '<div class="code">' . $r . '</div>';
 }
@@ -1464,6 +1462,9 @@ function escapeJavaScriptQuotes ($str) {
 // Send out mails depending on the 'mod/modes' combination
 // @TODO Lame description for this function
 function sendModeMails ($mod, $modes) {
+       // Init user data
+       $content = array ();
+
        // Load hash
        if (fetchUserData(getMemberId())) {
                // Extract salt from cookie
@@ -1527,45 +1528,43 @@ function sendModeMails ($mod, $modes) {
                                        $sub_mem = '{--MEMBER_CHANGED_DATA--}';
 
                                        // Output success message
-                                       $content = '<span class="message">{--MEMBER_MYDATA_MAIL_SENT--}</span>';
+                                       $content['message'] = '<span class="message">{--MEMBER_MYDATA_MAIL_SENT--}</span>';
                                        break;
 
                                default: // Unsupported module!
                                        logDebugMessage(__FUNCTION__, __LINE__, sprintf("Unsupported module %s detected.", $mod));
-                                       $content = '<span class="notice">{--UNKNOWN_MODULE--}</span>';
+                                       $content['message'] = '<span class="notice">{--UNKNOWN_MODULE--}</span>';
                                        break;
                        } // END - switch
                } else {
                        // Passwords mismatch
-                       $content = '<span class="notice">{--MEMBER_PASSWORD_ERROR--}</span>';
+                       $content['message'] = '<span class="notice">{--MEMBER_PASSWORD_ERROR--}</span>';
                }
        } else {
                // Could not load profile
-               $content = '<span class="notice">{--MEMBER_CANNOT_LOAD_PROFILE--}</span>';
+               $content['message'] = '<span class="notice">{--MEMBER_CANNOT_LOAD_PROFILE--}</span>';
        }
 
        // Send email to user if required
-       if ((!empty($sub_mem)) && (!empty($message))) {
+       if ((!empty($sub_mem)) && (!empty($message)) && (!empty($content['email']))) {
                // Send member mail
                sendEmail($content['email'], $sub_mem, $message);
        } // END - if
 
        // Send only if no other error has occured
-       if (empty($content)) {
-               if ((!empty($sub_adm)) && (!empty($message_admin))) {
-                       // Send admin mail
-                       sendAdminNotification($sub_adm, $message_admin, $content, getMemberId());
-               } elseif (isAdminNotificationEnabled()) {
-                       // Cannot send mails to admin!
-                       $content = '{--CANNOT_SEND_ADMIN_MAILS--}';
-               } else {
-                       // No mail to admin
-                       $content = '<span class="message">{--MEMBER_MYDATA_MAIL_SENT--}</span>';
-               }
-       } // END - if
+       if ((!empty($sub_adm)) && (!empty($message_admin)) && (isAdminNotificationEnabled())) {
+               // Send admin mail
+               sendAdminNotification($sub_adm, $message_admin, $content, getMemberId());
+       } elseif (isAdminNotificationEnabled()) {
+               // Cannot send mails to admin!
+               $content['message'] = '{--CANNOT_SEND_ADMIN_MAILS--}';
+       } else {
+               // No mail to admin
+               $content['message'] = '<span class="message">{--MEMBER_MYDATA_MAIL_SENT--}</span>';
+       }
 
        // Load template
-       loadTemplate('admin_settings_saved', false, $content);
+       loadTemplate('admin_settings_saved', false, $content['message']);
 }
 
 // Generates a 'selection box' from given array
@@ -1576,12 +1575,12 @@ function generateSelectionBoxFromArray ($options, $name, $optionValue, $optionCo
 
        // Walk through all options
        foreach ($options as $option) {
-               // Add the <option> entry
+               // Add the <option> entry from ...
                if (empty($optionContent)) {
-                       // ... from template
+                       // ... template
                        $OUT .= loadTemplate('select_' . $name . $extraName . '_option', true, $option);
                } else {
-                       // Direct HTML code
+                       // ... direct HTML code
                        $OUT .= '<option value="' . $option[$optionValue] . '">' . $option[$optionContent] . '</option>';
                }
        } // END - foreach
@@ -1676,8 +1675,14 @@ function fixNullEmptyToDashes ($str, $num) {
        return $return;
 }
 
+// Translates the "pool type" into human-readable
+function translatePoolType ($type) {
+       // Return "translation"
+       return sprintf("{--POOL_TYPE_%s--}", strtoupper($type));
+}
+
 //-----------------------------------------------------------------------------
-//                       Template Helper Functions
+//                      Template helper functions for EL
 //-----------------------------------------------------------------------------
 
 // Color-switch helper function
@@ -1690,7 +1695,7 @@ function doTemplateColorSwitch ($template, $clear = false, $return = true) {
                // Switch color if called from loadTemplate()
                //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'SWITCH:' . $template);
                $GLOBALS['color_switch'][$template] = 3 - $GLOBALS['color_switch'][$template];
-       } // END - if
+       }
 
        // Return CSS class name
        if ($return === true) {