Jump to content

Widget top private notes threads


Recommended Posts

I wanted a way to show the top tickets with private notes. I found myself getting lost with notes flying all over the place. This is just a little something I threw together so please feel free to use and rebuild it if you like. I wanted to add tabs but just no time. Just a note, I have no issues with criticism so if you see a better way please let me know as I am new to whmcs widgets but love to learn.

 


 

<?php
//Courtesy of Mark Fitzgerald
//May, 6 2017
//version 1.0.0
//Tested with WHMCS 7.1+
//Database Access ref: https://laravel.com/docs/5.2/queries#ordering-grouping-limit-and-offset
//
/*
This little widget will simply pull the top notes from the private notes thread and display them. It will include the 4 prior private post as
well so you can follow along. Is very handy to keep up with an at-a-glance of what is going on during the day. Have fun and you are more than
welcome to modify this to what every needs you wish. 

Note: would love to modify it a bit more to use group by for the tickets but did not have the time to learn everything about Laravel today.
*/



use Illuminate\Database\Capsule\Manager as Capsule;
add_hook('AdminHomeWidgets', 1, function() {
   return new DIDXPrivateNotesWidget();
});

/**
* Private Notes last posted To you.
*/
class DIDXPrivateNotesWidget extends \WHMCS\Module\AbstractWidget
{

   protected $title = 'Top 10 Private Ticket Post  <a href="supporttickets.php?view=Open" class="btn btn-link btn-xs">View All »</a>';
   protected $description = '';
   protected $weight = 150;
   protected $columns = 2;
   protected $cache = false;
   protected $cacheExpiry = 120;
   protected $requiredPermission = '';

	//Will just format the date stamp in a nice way is all. Sorta like WHMCS human function.
	//I found this function a while back but cant' remember where though I have made a number of changes to it.
	public function format_stamp($datetime, $show_full = false) 
	{

   $aTime_line = array('y' => 'year','m' => 'month','w' => 'week','d' => 'day','h' => 'hour','i' => 'minute','s' => 'second');

   $long_ago = new DateTime($datetime);
   $date_now = new DateTime;
   $diff_in_time = $date_now->diff($long_ago);

   $diff_in_time->w = floor($diff_in_time->d / 7);
   $diff_in_time->d -= $diff_in_time->w * 7;

   foreach ($aTime_line as $key => &$value) 
   {
       if ($diff_in_time->$key)
         $value = $diff_in_time->$key . ' ' . $value . ($diff_in_time->$key > 1 ? 's' : '');
       else
         unset($aTime_line[$key]);
   }

   if (!$show_full) $aTime_line = array_slice($aTime_line, 0, 1);
   $return_value = $aTime_line ? implode(', ', $aTime_line) . ' ago' : 'just now';
   return $return_value;
	}


   public function getData()
   {
       return array();
   }

   public function generateOutput($data)
   {
   		$the_div_output = "";
   		$notes_same_group = 0;
			$idlist = "";

			//Get a list of private tickes order by date. Would like to later use a group by to do this so I can do away
			//with the if statement below and then lower the take to just 10...
       foreach (Capsule::table('tblticketnotes')->orderBy('date', 'desc')->take(20)->get() as $privatenotes) 
       {
       		//If statement, bad code to only show ticket once, Should be a groupby but did not have time to learn laravel's system completely today.
       		if (strstr($idlist,",".$privatenotes->ticketid.","))
       			$notes_same_group++;//Might use this later on.
       		else
       		{
        		$idlist .= ",".$privatenotes->ticketid.",";

            $tid = $privatenotes->ticketid;
            $noteid = $privatenotes->id;
            $notedate = $privatenotes->date;
            $postedby = $privatenotes->admin;
           	$notemsg = $privatenotes->message;

           	//You can modify this to truncate the string if you don't wish to show it all. I had it at 100 but team did not like it.
            if (strlen($notemsg) > 1000)
            {
            	$notemsg = substr($notemsg,0,1000)."...";
            }
            $ticketlink = "supporttickets.php?action=view&id=$tid";

            //Find the top x replies per ticket. Wish they had a flag to show which ones were private, would be nice.
            $reply_str = '';
						//Look up the ticket id and date <= to it. this will give you the from to...
						foreach (Capsule::table('tblticketnotes')->orderBy('date', 'desc')->take(3) ->where([
				    ['ticketid', '=', $tid],['id', '<', $noteid]])->get()as $ticketreplies)
				    {
		           	$replymsg = $ticketreplies->message;
		            if (strlen($replymsg) > 30)
	  	          {
	    	        	$replymsg = substr($replymsg,0,1000)."...";
	      	      }


				    	$reply_str.= "<Br/>";
				    	$reply_str.= "<div><span class='data color-black'>".$ticketreplies->admin." (".$this->format_stamp($ticketreplies->date).") - </span>  <span class='data color-orange'>"".$replymsg.""<span></div><Br/>";
				    }


            //Get ticket so I can get the users id. Odd they don't keep this in the notes table.
            $userid = '';
            foreach(Capsule::table('tbltickets')->WHERE ('id', '=', $tid)->take(1)->get() as $ticketinfo)
            {
                $userid = $ticketinfo->userid;
            }

            //Check user id and if we have one, build a url so we can link to it.
						if (!empty($userid))
						{
            $userlink = "clientssummary.php?userid=".$userid;

	            //Get this users infomaton.
	            foreach(Capsule::table('tblclients')->WHERE ('id', '=', $userid)->take(1)->get() as $whoami)
	            {
	                $fname = $whoami->firstname;
	                $lname = $whoami->lastname;
	                $cname = $whoami->companyname;
	                $theuser = "$fname $lname";
	            }
          	}
          	else
          	{
          		$userid = "No User";
          	}

						//Add to the output string that you will ref below.
            $the_div_output  .= "
            <div class='feed-element'>
                      <div><small class='text-muted'><a href='".$userlink."'>Client: " . $theuser . "</a></small><small class='pull-right text-navy'><a href='".$ticketlink."'>Ticket #".$tid."</a></small></div>
                      <div><a href='".$ticketlink."'>".$postedby."</a><span class='data color-black'> (".$this->format_stamp($notedate).") - <span  class='data color-orange'>".$notemsg."
                      </span></span></div>" . $reply_str . "

            </div><hr width='100%'>";
          }
       }

if (!empty($the_div_output)) {
return <<<EOF
<div class="widget-content-padded">
       $the_div_output
</div>
EOF;
}
} //public function generateOutput($data)
} //class

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated