We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 26260
    • 12 Posts
    Title: os_DoxInfo
    Type: Snippet
    State: Alpha

    I have written below snippet, which might be described as: "AuthorTimestamp" on steroids.
    I would really appreciate if you would help me test it... especially the Ditto-option (I don’t use ditto on my setup).
    Besides the regular testing, I would appreciate any optimization ideas you might have. I’m pretty new to php, so please spell it out for dummies smiley

    Here it goes:
    <?php
    /* Title: os_DoxInfo | Version: 0.8.0 alpha, released july 16th 2008
     * Written by: Ole Sauffaus, [email protected] | Inspired by: "AuthorTimestamp"-snippet by OncleBen31
     * 
     * Description: Configurable snippet that gathers and outputs a MODx document's creation-, publishing-, editing-
     * and deletion status/time, as well as name, email, phone numbers and foto of the user responsible for each action.
     * osDoxInfo can be configured to output different datasets for front-end and logged-in users, and should be usable
     * with Ditto(which hasn't been tested tho!).
     * Output format is semantic XHTML 1.1 strict (read next sentence).
     * Study the included Example css for styling ideas, but please delete it from this file, and replace it with your
     * own css in a linked file (embedded css inside the document body, is not allowed in XHTML).
     * 
     * USAGE:
     * Example snippetcall with all possible options:
     * [[os_DoxInfo? &os_front=`[create,edit,publish],(photo,name,role,email,phone,mobile),server` &os_back=`[create,edit,publish,delete],(photo,name,role,email,phone,mobile),server` &os_id=`[+id+]`]]
     *
     * "$os_front" should contain those options you want to show to everybody (not logged in).
     * "$os_back" should contain those options you want to show to logged-in managers.
     * Options inside [] are "document-info".
     * Options inside () are "manager-user-info" associated with all "document-info".
     * The "server"-option outputs some server-info (query-number, db-time, parse-time etc.)
     * Any options you don't want to show, you just leave out...
     * 
     * Example: [[os_DoxInfo? &os_front=`[create,edit],(name)`]] will output:
     * "time of document creation" + "name of responsible manager" + "time of last edit" + "name of responsible manager" (but only in the front-end of the site)
     * 
     * Example of typical snippetcall (this is also the fallback settings if nothing at all is set):
     * [[os_DoxInfo? &os_front=`[create,edit],(photo,name,email)` &os_back=`[create,edit,publish,delete],(name,role,email,mobile),server`]]
     * 
     * The &os_id=`[+id+]` is intended for a Ditto template, to output info for the document returned by Ditto (NOT TESTED).
     */
    
    
    
    // Please edit this url, so that it points to your default image-file:
    define(OS_DEFAULT_PHOTO,'assets/images/default_admin.png',0);//defining a constant isn't cool... but how else to make it available to the function?
    
    // Please edit this date-format, to suit your particular needs (help for parameters: http://www.php.net/manual/en/function.strftime.php):
    define(OS_DATEFORMAT,'%A - %B %d, %Y - %H:%M',0);//defining a constant isn't cool... but how else to make it available to the function?
    
    //Set the headers to whatever you want. If you don't want headers, just make them empty strings
    $os_creation_header = "Document Creation";
    $os_editing_header = "Last Editing";
    $os_publishing_header = "Document Publishing";
    $os_deletion_header = "";
    $os_server_header = "";
    
    // Example css. Delete this and write your own css (preferably in a seperate linked css-file, to keep the xhtml output nice and tidy)
    echo "\n<style type=\"text/css\">
    div.os_DoxInfo { position:relative; border:1px solid #000; padding:0; margin:5px 0; overflow:hidden; }
    div#os_Creation { background:red; }
    div#os_Editing { background:green; }
    div#os_Publishing { background:yellow; }
    div#os_Deletion{ background:purple; }
    div#os_Server{ background:orange; }
    div.os_DoxInfo h6 { text-align:center; margin:0; padding:1px 0; border:1px solid #000; border-width:0 0 1px 0; letter-spacing:2px; }
    div.os_DoxInfo img { float:left; width:80px;height:80px; margin:5px 5px 5px 5px; border:1px solid #000; }
    div.os_DoxInfo p { color:#000; font-size:10px; line-height:11px; padding:0; margin:3px 0 3px 5px; }
    div.os_DoxInfo p dfn { display:block; color:#555; font-size:9px; }
    </style>\n";
    
    
    
    /*----- Don't edit below this line, unless you can :) -----
     * For those who can, there will be plenty of room for improvement and optimizing i think
    */
    
    function get_user_data($user_id, $output_mask) {
    	global $modx;
    	$os_query = $modx->db->select('photo,comment,fullname,internalKey,role,email,phone,mobilephone', $modx->getFullTableName('user_attributes'), 'id = '.$user_id);//get user detailes
    	$os_result = $modx->db->makeArray($os_query);
    	$os_result = $os_result[0];//there can be only one!
    
    	if(strpos($output_mask,'role') != FALSE) {//Do we want the admin-role?
    		$os_query = $modx->db->select('name,description', $modx->getFullTableName('user_roles'), 'id = '.$os_result['role']);//get role detailes
    		$os_result2 = $modx->db->makeArray($os_query);
    		$os_result2 = $os_result2[0];//there can be only one!
    		$os_result['role'] = 'Role: '.$os_result2['name'].' <dfn>'.$os_result2['description'].'</dfn>';//format and merge results into original array as 'role'
    		unset($os_result2);//not needed anymore, so flush
    	} else { unset($os_result['role']); }//not wanted, so flush
    
    	if(strpos($output_mask,'photo') != FALSE) {//Do we want the photo?
    		if(!empty($os_result['photo'])) {//Check if a photo is actually present... format and replace white space in url in any case
    			$os_result['photo'] = '<img src="'.str_replace(' ','%20',$os_result['photo']).'" alt="'.$os_result['fullname'].'" title="'.$os_result['fullname'].'. '.$os_result['comment'].'" />';//database image url
    		} else {
    			$os_result['photo'] = '<img src="'.str_replace(' ','%20',OS_DEFAULT_PHOTO).'" alt="'.$os_result['fullname'].'" title="'.$os_result['fullname'].'. '.$os_result['comment'].'" />';//default image url
    		}
    	} else {
    		unset($os_result['photo']);//not wanted, so flush
    	}
    	unset($os_result['comment']);//not needed anymore, so flush
    
    	if(strpos($output_mask,'name') != FALSE) {//Do we want the name?
    		if(empty($os_result['fullname'])) {//Check if a fullname is actually present... format it
    			$os_result['fullname'] = $os_result['internalKey'];//no user-name available in account, so insert user "internalKey" as fallback
    		}
    	} else {
    		unset($os_result['fullname']);//not wanted, so flush
    	}
    	unset($os_result['internalKey']);//not needed anymore, so flush
    
    	if(strpos($output_mask,'email') != FALSE) {//Do we want the email?
    		if(strpos($output_mask,'name') != FALSE) {//if user-name will be outputted, then use that as e-mail-link-text
    			$os_result['fullname'] = '<a href="mailto:'.$os_result['email'].'" title="Send me an e-mail">'.$os_result['fullname'].'</a>';
    			unset($os_result['email']);//not needed anymore, so flush
    		}
    		if(isset($os_result['email'])) {//is email still not formatted?
    			$os_result['email'] = '<a href="mailto:'.$os_result['email'].'" title="Send e-mail to the responsible administrator">'.$os_result['email'].'</a>';
    		}
    	} else { unset($os_result['email']); }//not wanted, so flush
    
    	if(strpos($output_mask,'phone') != FALSE) {//Do we want the phone number?
    		if(!empty($os_result['phone'])) {//Check if a phone number is actually present... format it
    			$os_result['phone'] = 'Phone: '.$os_result['phone'];//database user-phone
    		} else {
    			$os_result['phone'] = 'Phone: -';//no user-phone available in account, so insert human readable alternative
    		}
    	} else { unset($os_result['phone']); }//not wanted, so flush
    	
    	if(strpos($output_mask,'mobile') != FALSE) {//Do we want the mobile number?
    		if(!empty($os_result['mobilephone'])) {//Check if a mobile number is actually present... format it
    			$os_result['mobilephone'] = 'Mobile: '.$os_result['mobilephone'];//database user-mobile
    		} else {
    			$os_result['mobilephone'] = 'Mobile: -';//no user-mobile available in account, so insert human readable alternative
    		}
    	} else { unset($os_result['mobilephone']); }//not wanted, so flush
    	
    	return $os_result;
    }
    
    // Import parameters from snippet-call, or set them to defaults
    if(IN_MANAGER_MODE) {
    	$os_output_mask = isset($os_back) ? $os_back : '[create,edit,publish,delete],(photo,name,role,email,mobile),server';//set to default if not present in call
    } else {
    	$os_output_mask = isset($os_front) ? $os_front : '[create,edit],(photo,name,email)';//set to default if not present in call
    }
    $os_id = isset($os_id) ? $os_id : $modx->documentObject['id'];//Document to work with (this is the untested Ditto-thingy), defaults to current document
    
    $os_document = $modx->getPageInfo($os_id,1,'createdby,createdon,editedby,editedon,published,publishedby,pub_date,unpub_date,deleted,deletedby,deletedon');//Get document info
    
    //Creator array [+ time]
    if(strpos($os_output_mask,'create') != FALSE) {//Do we want creation-data?
    	$os_creator = get_user_data($os_document['createdby'], $os_output_mask);//Query for creator details
    
    	$os_output_array[time] = strftime(OS_DATEFORMAT,$os_document['createdon']);//New array for ordered output
    	$os_output_array = array_merge($os_output_array,$os_creator);//append creator-data to output
    	if(isset($os_creator['photo'])) {//Does data contain a photo?
    		unset($os_output_array['photo']);//Remove photo from output array
    		$os_output_photo = $os_creator['photo']."\n\t";//Make seperate photo variable
    	}
    	if(!empty($os_creation_header)) {//Format header according to configuration
    		$os_output_header = "\n\t<h6>".$os_creation_header."</h6>\n\t";
    	} else { $os_output_header = "\n\t"; }
    	
    	echo "\n<div class=\"os_DoxInfo\" id=\"os_Creation\">".$os_output_header.$os_output_photo."<p>".implode("</p>\n\t<p>",$os_output_array)."</p>\n</div>\n";
    	unset($os_output_header);//flush, so it won't be falsly incorporated in next output
    	unset($os_output_photo);//flush, so it won't be falsly incorporated in next output
    	unset($os_output_array);//not needed anymore, so flush
    } else { unset($os_result['createdby']); }//not wanted, so flush
    
    //Editor array : [+ time]
    if(strpos($os_output_mask,'edit') != FALSE) {//Do we want editing-data?
    	if($os_document['editedby'] == $os_document['createdby']) {
    		$os_editor = $os_creator;//inherit data... don't bother your sql
    	} else {
    		$os_editor = get_user_data($os_document['editedby'], $os_output_mask);//Query for editor details
    	}
    
    	$os_output_array[time] = strftime(OS_DATEFORMAT,$os_document['editedon']);//New array for ordered output
    	$os_output_array = array_merge($os_output_array, $os_editor);
    	if(isset($os_editor['photo'])) {
    		unset($os_output_array['photo']);//remove the photo from output
    		$os_output_photo = $os_editor['photo']."\n\t";//Make seperate photo variable
    	}
    	if(!empty($os_editing_header)) {//Format header according to configuration
    		$os_output_header = "\n\t<h6>".$os_editing_header."</h6>\n\t";
    	} else { $os_output_header = "\n\t"; }
    	
    	echo "\n<div class=\"os_DoxInfo\" id=\"os_Editing\">".$os_output_header.$os_output_photo."<p>".implode("</p>\n\t<p>",$os_output_array)."</p>\n</div>\n";
    	unset($os_output_header);//flush, so it won't be falsly incorporated in next output
    	unset($os_output_photo);//flush, so it won't be falsly incorporated in next output
    	unset($os_output_array);//not needed anymore, so flush
    } else { unset($os_result['editedby']); }//not wanted, so flush
    
    //Publisher array : [+ status,times]
    if(strpos($os_output_mask,'publish') != FALSE) {//Do we want publishing-data?
    	if($os_document['publishedby'] == $os_document['createdby']) {
    		$os_publisher = $os_creator;//inherit data... don't bother your sql
    	} else if($os_document['publishedby'] == $os_document['editedby']) {
    		$os_publisher = $os_editor;//inherit data... don't bother your sql
    	} else {
    		$os_publisher = get_user_data($os_document['publishedby'], $os_output_mask);//Query for publisher details
    	}
    	
    	$os_output_array = $os_publisher;//New array for ordered output
    	if(!empty($os_document['pub_date'])) {//add publish-time
    		array_unshift($os_output_array, ('In: '.strftime(OS_DATEFORMAT,$os_document['pub_date'])));
    	} else {
    		array_unshift($os_output_array, ('In: No date set'));
    	}
    	if(!empty($os_document['unpub_date'])) {//add unpublish-time
    		$os_output_array[0] = $os_output_array[0].' / Out: '.strftime(OS_DATEFORMAT,$os_document['unpub_date']);
    	} else {
    		$os_output_array[0] = $os_output_array[0].' / Out: No date set';
    	}
    	if($os_document['published']) {//add document's current status, human readable
    		array_unshift($os_output_array, ('Publishing status: Published'));
    	} else {
    		array_unshift($os_output_array, ('Publishing status: Unpublished'));
    	}
    	if(isset($os_publisher['photo'])) {
    		unset($os_output_array['photo']);//remove the photo from output
    		$os_output_photo = $os_publisher['photo']."\n\t";//Make seperate photo variable
    	}
    	if(!empty($os_publishing_header)) {//Format header according to configuration
    		$os_output_header = "\n\t<h6>".$os_publishing_header."</h6>\n\t";
    	} else { $os_output_header = "\n\t"; }
    	
    	echo "\n<div class=\"os_DoxInfo\" id=\"os_Publishing\">".$os_output_header.$os_output_photo."<p>".implode("</p>\n\t<p>",$os_output_array)."</p>\n</div>\n";
    	unset($os_output_header);//flush, so it won't be falsly incorporated in next output
    	unset($os_output_photo);//flush, so it won't be falsly incorporated in next output
    	unset($os_output_array);//not needed anymore, so flush
    } else { unset($os_result['publishedby']); }//not wanted, so flush
    
    //Deletor array : [+ status,time]
    if(strpos($os_output_mask,'delete') != FALSE) {//Do we want deletion-data?
    	if($os_document['deleted']) {
    		if($os_document['deletedby'] == $os_document['createdby']) {
    			$os_deletor = $os_creator;//inherit data... don't bother your sql
    		} else if($os_document['deletedby'] == $os_document['editedby']) {
    			$os_deletor = $os_editor;//inherit data... don't bother your sql
    		} else if($os_document['deletedby'] == $os_document['publishedby']) {
    			$os_deletor = $os_publisher;//inherit data... don't bother your sql
    		} else {
    			$os_deletor = get_user_data($os_document['deletedby'], $os_output_mask);//Query for deletor details
    		}
    		array_unshift($os_deletor, ('Deletion status: Deleted - '.strftime(OS_DATEFORMAT,$os_document['deletedon'])));
    	} else {
    		$os_deletor[0] = "Deletion status: Not deleted";//insert or add human readable description
    	}
    	
    	$os_output_array = $os_deletor;//New array for ordered output
    	if(isset($os_deletor['photo'])) {
    		unset($os_output_array['photo']);//remove the photo from output
    		$os_output_photo = $os_deletor['photo']."\n\t";//Make seperate photo variable
    	}
    	if(!empty($os_deletion_header)) {//Format header according to configuration
    		$os_output_header = "\n\t<h6>".$os_deletion_header."</h6>\n\t";
    	} else { $os_output_header = "\n\t"; }
    	
    	echo "\n<div class=\"os_DoxInfo\" id=\"os_Deletion\">".$os_output_header.$os_output_photo."<p>".implode("</p>\n\t<p>",$os_output_array)."</p>\n</div>\n";
    	unset($os_output_header);//flush, so it won't be falsly incorporated in next output
    	unset($os_output_photo);//flush, so it won't be falsly incorporated in next output
    	unset($os_output_array);//not needed anymore, so flush
    } else { unset($os_result['deletedby']); }//not wanted, so flush
    
    //Server output
    if(strpos($os_output_mask,'server') != FALSE) {//Do we want server-data?
    	if(!empty($os_server_header)) {//Format header according to configuration
    		$os_output_header = "\n\t<h6>".$os_server_header."</h6>\n\t";
    	} else { $os_output_header = "\n\t"; }
    
    	echo "\n<div class=\"os_DoxInfo\" id=\"os_Server\">".$os_output_header."<p>Served from <em>[^s^]</em> in <em>[^t^]</em> <dfn>[^q^] Queries / [^qt^], Parsing for [^p^]</dfn></p>\n</div>\n";
    	unset($os_output_header);//flush, so it won't be falsly incorporated in next output
    }
    ?>

      Creativity is 1% inspiration and 99% perspiration.

    This discussion is closed to further replies. Keep calm and carry on.