RM - Source Code View

Source Code Viewer

This page lets you view source code from my server. The program uses a brute force code formatter to color code elements. NOTE: I wrote this program while trying to learn the vim text editor. This is not my usual coding style.

Use this select box to select a file.

Hide Lines Numbers

View: msg.php Notification Object - Test

These are tests I perform on rmHead() to assure functions work as designed.

Formatted Code

Below is the code all formatted with bright colors. The program links to files opened with include() and expands those opened with require(). Clicking on the require line should change visibility.

msg.php Notification Object - Test

001 <!DOCTYPE html> 002 <html lang="en"> 003 <head> 004 <meta charset="UTF-8" /> 005 <meta name="viewport" content="width=device-width, initial-scale=1" /> 006 <meta name="Author" content="Kevin Delaney" /> 007 <meta name="keywords" content="msg object, notification object" /> 008 <meta name="description" content="The test page for the msg notfication object in msg.php" /> 009 <title>Test Page for msg.php</title> 010 <link rel="canonical" href="https://yintercept.com/resources/msgTest.php" /> 011 <link rel="stylesheet" href="rm.css" type="text/css"> 012 </head> 013 <body> 014 <div class="main"> 015 <h2>msg Test Page</h2> 016 <p>This is a test page for the functions in the msg.php file. The msg program is a notifcation object that conveys messages from backend processes to the programmer or users. I consider this object to be <a href="https://yintercept.com/resources/">a global resource</a> that should not be constrained by the class structure.</p> 017 <p>This test page loads msg.php; then runs it through tests. You can see the msg object and the test in the <a href="https://yintercept.com/resources/view.php?script=2#code">Source Code Viewer</a>. The results of the tests are below the dotted line</p> 018 <hr style="border: 2px dotted #000"> 019 <h3>Test Results</h3> 020 <p>These are tests for notifcation functions. The css for message block is in <a href="rm.css">rm.css</a>.</p> 021 <?php 022 /** 023 * msgTest.php is a test script for the msg.php object. 024 * Yes, even though it doesn't use a rigid class structure, it is an object. 025 * This link shows the test results: 026 * @see https://yintercept.com/resources/msgTest.php 027 * 028 * @copyright 2015 kd 029 */
require('/var/www/php/msg.php');
001 <?php 002 /** 003 * @package ResourceModel 004 * 005 * msgNote() functions create a poor man's notification object. 006 * 007 * msgNote() conveys general information and success messages to the user 008 * msgComment() leaves comments on the view source page for visitors and 009 * as [DEBUG] warnings for the programmer and tester. 010 * msgError() produces messages that show up in red. It sets isOkay to false 011 * You can view this code on the msg code Test page: 012 * @see https://yintercept.com/resources/view.php?script=9 013 * 014 * The function msgHTML() prints the messages and resets the buffer. 015 * @author Kevin Delaney 016 * @copyright 1999-2015 Kevin Delaney 017 * @license https://yintercept.com/resources/license.html 018 */ 019 const MSG_OK = 1; // denotes a happy message 020 const MSG_ERROR = 2; // denotes a sad message 021 const MSG_COMMENT = 3; // print as a comment or DEBUG notice. 022 // NOTE: As this "object" is a global, I will put its data in $GLOBALS 023 $GLOBALS['msg']['arr'] = array(); // holds the messages 024 $GLOBALS['msg']['isOkay'] = true; // warns that an error occurred 025 $GLOBALS['msg']['debugMode'] = false; // if true msgHTML() will display comments for programmer 026 /** 027 * msgNote() adds a neutral or success message to the buffer. 028 * @param string $txt The text of a godd message 029 */ 030 function msgNote($txt='') { 031 if($txt!='') $GLOBALS['msg']['arr'][] = ['type'=>MSG_OK, 'txt' => $txt]; 032 } 033 /** 034 * @param string $txt The text to give the user on error. 035 */ 036 function msgError($txt='') { 037 if ($txt != '') $GLOBALS['msg']['arr'][] = ['type'=>MSG_ERROR, 'txt' => $txt]; 038 $GLOBALS['msg']['isOkay'] = false; 039 } 040 /** 041 * @param string $txt Text of a comment for the programmer. 042 */ 043 function msgComment($txt='') { 044 if ($txt!='') $GLOBALS['msg']['arr'][] = ['type'=>MSG_COMMENT, 'txt' => $txt]; 045 } 046 /** 047 * msgToggle saves a success or failure message based on the toggle 048 * @PARAM boolean $test 049 * @PARAM array $msgArr This three part array holds the message. 050 * $msgArr[0] is a required success message. 051 * $msgArr[1] is a reuired failure message 052 * $msgArr[2] is an optional failure comment (with debug notes) 053 * $msgArr[3] is optional message if test is not boolean. 1=return error. * @RETURN function resturns a boolean. 054 */ 055 function msgToggle($test, $msgArr) { 056 $rv = $test; 057 if (is_integer($test)) $test = (bool) $test; 058 // some functions say they return boolean, then don't. 059 if (!(is_bool($test))) { 060 msgError($msgArr[1]); 061 msgComment('Test needs to be boolean'); 062 $rv = false; 063 } elseif ($test) { 064 // success message. 065 if (isset($msgArr[0])) msgNote($msgArr[0]); 066 } else { 067 if (isset($msgArr[1])) msgError($msgArr[1]); 068 if (isset($msgArr[2])) msgComment($msgArr[2]); 069 } 070 return $rv; 071 } 072 /** 073 * I used to call this function isOkay(); I changed name to msgOkay() 074 * to emphasize that this really is an object even though it's not in a class. 075 * @return boolean Returns true if no errors and false on error 076 */ 077 function msgOkay() { 078 return $GLOBALS['msg']['isOkay']; 079 } 080 /** 081 * @output function prints the array in HTML 082 */ 083 function msgHTML($indent=PHP_EOL.' ') { 084 // This control loops through and prints the messages. 085 static $blockId = 0; // give each message block an id 086 $cnt = count($GLOBALS['msg']['arr']); // count messages 087 088 if ($cnt > 0) { 089 $ulClass = ($GLOBALS['msg']['isOkay'])? 'ulOkay' : 'ulErrors'; 090 $blockStr = ($blockId++ == 0)? 'msg' : 'msg'.$blockId; 091 echo $indent.'<ul id="'.$blockStr.'" class="'.$ulClass.'">'; 092 093 for($i=0; $i<$cnt; $i++) { 094 $row=$GLOBALS['msg']['arr'][$i]; 095 switch ($row['type']) { 096 case MSG_OK: 097 echo PHP_EOL.' <li class="msgHappy">'.$row['txt'].'</li>'; 098 break; 099 case MSG_ERROR: 100 echo PHP_EOL.' <li class="msgSad">'.$row['txt'].'</li>'; 101 break; 102 case MSG_COMMENT: 103 if ($GLOBALS['msg']['debugMode']) { 104 // this is a message for the administrator. 105 echo PHP_EOL.' <li>[DEBUG]'.$row['txt'].'[/DEBUG]</li>'; 106 } else { 107 echo PHP_EOL.'<!--'.$row['txt'].'-->'; 108 } 109 break; 110 default: // should not happen. 111 } 112 } 113 echo PHP_EOL.'</ul>'; 114 $GLOBALS['msg']['arr'] = []; // empty the array. 115 } 116 } 117 /** 118 * msgLog() records information into the log database 119 * @param string logName is the name of a table in the log. 120 * @param arr is an array that should match the columns in the log. 121 */ 122 function msgLog($logName,$arr=[]) { 123 file_put_contents('/var/www/db/'.$logName.'.log',implode('|',$arr), FILE_APPEND | LOCK_EX); 124 } 125 ?>
// End Require
030 031 echo '<h3>Test a Success Message</h3>'; 032 msgNote('This is a test message'); 033 msgNote('Things are going swimmingly.'); 034 msgNote('Click "view source" in your browser to see the comment.'); 035 msgComment('This comment is embedded in the HTML code in the msg seection'); 036 msgHTML(); // echos comment to the screen. 037 echo '<p>The next line checks msgError. As there is no error. It should show "<em>Peform Important Function</em>."</p>'.PHP_EOL; 038 if (msgOkay()) { 039 echo '<p style="text-align: center"><em>Perform Important Function</em></p>'.PHP_EOL; 040 } else { 041 echo '<p style="text-align: center"><em>Errors occurred. Skip function.</em></p>'.PHP_EOL; 042 } 043 echo '<h3>Test a failure message</h3>'; 044 msgNote('Second round.'); 045 msgError('A serious error occurred'); 046 msgError('The comment is in HTML - View Source.'); 047 msgComment('It\'s not really that serious'); 048 msgHTML(); /// should be in an error block; 049 echo '<p>An error occurred. The program is now in a state of general panic; So we skip the important function. The line below should read "<em>Error occurred. Skip Function.</em>"</p>'.PHP_EOL; 050 if (msgOkay()) { 051 echo '<p style="text-align: center"><em>Perform Important Function</em></p>'.PHP_EOL; 052 } else { 053 echo '<p style="text-align: center"><em>Errors occurred. Skip function.</em></p>'.PHP_EOL; 054 } 055 echo '<h3>Test Comment in Debug Mode</h3> 056 <p>In these next tests, I switch the debugMode toggle. The comment jumps out of 057 the HTML and into the comment section.</p>'; 058 //msgLog('dbTrace',[1,'abc']); 059 $GLOBALS['msg']['debugMode'] = true; 060 msgError('Something terrible just happened.'); 061 msgComment('A comment about the terrible event.'); 062 msgNote('The debug comment should display above.'); 063 msgHTML(); 064 echo '<p>I added a function called msgToggle. You can wrap it around a function 065 that returns a boolean <i>true</i> or <i>false</i> and a message array. The first element [0] is a success message, the seond [1] is a failure message. The third [2] is a debug comment and fourth action to take if test is not boolean, set to 1. I will call msgToggl with true, false, -1 and "What\'s a Boolean?"</p>'; 066 $arr=['Success Message', 067 'Failure Message', 068 'debug comment', 069 'Test Needs to be a Boolean!']; 070 msgToggle(true,$arr); 071 msgToggle(false,$arr); 072 msgToggle(-1,$arr); 073 msgToggle('Whate\'s a Boolean?',$arr); 074 msgHTML(); 075 /** 076 * The sql.php object uses msg.php to report errors with the datbase connection 077 * The sqlTest page provides an example of how msg.php is used in practice. 078 * 079 * @see https://yintercept.com/resources/view.php?script=3 080 */ 081 ?> 082 <h3>More Tests</h3> 083 <p>The <a href="http://yinterceot.com/resources/sqlTest.php">PDO wrapper sql.php</a> uses the object msg.php to report errors and provides a good example of how msg.php is used.</p> 084 </div> 085 <p style="text-align: center"><a href="http://blog.yintercept.com" style="color: #ff8">blog</a> 086 ~ <a href="http://CommunityColor.com" style="color: #ff8">Community Color</a> 087 </p> 088 </body> 089 </html>

Use "view source" from your browser to grab the output. Feel free to link to this project and check out the Resource Model for information on PHP coding or my tumblr blog for picture of Arizona, Colorado or Utah.

File last modified at January 04 2016 21:10:53.. This page has been viewed 1361 Times.

Record of Revisions
RevbyDateDescription
0.2kd2016-01-06Added msgToggle() function.

blog ~ Resource Model ~ links