Collections functions
General functions
Node functions
Render functions
Theme permission functions
User functions
Resource functions

message_get()

Description

Simple class to use for user notifications

@internal

class ResourceSpaceUserNotification
{

@var array $message_parts
Array of message text components and optional find/replace arrays for language strings

private $message_parts = [];


@var array $subject
Array of subject parts

private $subject = [];


@var string $url

public $url = "";


@var array $template

public $template;


@var array $templatevars

public $templatevars;


@var array $user_preference Optional array of (boolean only) user preferences, with required and default values to check for when sending notification e.g.
["user_pref_resource_access_notifications"=>["requiredvalue"=>true,"default"=>$admin_resource_access_notifications],"actions_resource_requests" =>["requiredvalue"=>false,"default"=>true]]
or
["user_pref_system_management_notifications" => true]

All preferences must be set to the required values for the notification to be sent


public $user_preference;


@var array $eventdata Optional array for linking the system message to a specific activity e.g. resource or account request so that it can be deleted once the request has been processed.
["type"] e.g. MANAGED_REQUEST
["ref"]

public $eventdata = [];


Set the notification message


public function set_text($text,$find=[], $replace=[])
{
$this->message_parts = [[$text, $find, $replace]];
}


Append text to the notification message

public function append_text($text,$find=[], $replace=[])
{
$this->message_parts[] = [$text, $find, $replace];
}


Append multiple text elements to the notification message

public function append_text_multi($textarr)
{
$this->message_parts = array_merge( $this->message_parts,$textarr);
}


Prepend text component to the notification message

public function prepend_text($text,$find=[], $replace=[])
{
array_unshift($this->message_parts,[$text, $find, $replace]);
}


Prepend multiple text elements to the notification message

public function prepend_text_multi($textarr)
{
// Loop in reverse order so that the parts get ordered correctly at start
for($n=count($textarr);$n--;$n>=0)
{
array_unshift($this->message_parts,$textarr[$n]);
}
}


Set the notification subject


public function set_subject($text,$find=[], $replace=[])
{
$this->subject = [[$text, $find, $replace]];
}


Append text to the notification subject

public function append_subject($text,$find=[], $replace=[])
{
$this->subject[] = [$text, $find, $replace];
}


Get the message text, by default this is resolved into single string with text translated and with the find/replace completed
Note that if not returning raw data the correct $lang must be set by before this is called

public function get_text($unresolved=false)
{
global $lang;
if($unresolved)
{
return $this->message_parts;
}
$messagetext = "";
foreach($this->message_parts as $message_part)
{
$text = $message_part[0];
if(substr($text,0,5) == "lang_")
{
$langkey = substr($text,5);
$text = str_replace('%applicationname%', $GLOBALS['applicationname'], $lang[$langkey]);
}
if(substr($text,0,5) == "i18n_")
{
$i18n_string = substr($text,5);
$text = i18n_get_translated($i18n_string);
}
if(isset($message_part[1]) && isset($message_part[2]) && count($message_part[1]) == count($message_part[2]))
{
$text = str_replace($message_part[1],$message_part[2],$text);
}
$messagetext .= $text;
}
return $messagetext;
}

public function get_subject()
{
global $lang;
$fullsubject = "";
foreach($this->subject as $subjectpart)
{
$text = $subjectpart[0];
if(substr($text,0,5) == "lang_")
{
$langkey = substr($text,5);
$text = $lang[$langkey];
}

if(isset($subjectpart[1]) && isset($subjectpart[2]))
{
$text = str_replace($subjectpart[1],$subjectpart[2],$text);
}
$fullsubject .= $text;
}
return $fullsubject;
}
}


Gets messages for a given user (returns true if there are messages, false if not)
Note that messages are passed by reference.

Parameters

ColumnTypeDefaultDescription
&$messages
$user int User ID
$get_all bool false Retrieve all messages? Setting to TRUE will include all seen and expired messages
$sort bool "ASC" Sort by message ID in ascending or descending order
$order_by string "ref" Order of messages returned
$text string Text or $lang string using the 'lang_' prefix
$find array Array of find strings to use for str_replace() in $lang strings
$replace array Array of replace strings to use for str_replace()
$messages array Array that will be populated by messages. Passed by reference
$unresolved bool Return the raw message parts to use in another message object. False by default

Return

void */
void */
void */
void */
void */
void */
void */
string|array */
bool Flag to indicate if any messages exist

Location

include/message_functions.php lines 219 to 261

Definition

 
function message_get(&$messages,$user,$get_all=false,$sort="ASC",$order_by="ref")
    {
    switch (
$order_by)
        {
        case 
"ref":
            
$sql_order_by "user_message.ref";
            break;
        case 
"created":
            
$sql_order_by "message.created";
            break;
        case 
"from":
            
$sql_order_by "owner";
            break;
        case 
"fullname":
            
$sql_order_by "user.fullname";
            break;
        case 
"message":
            
$sql_order_by "message.message";
            break;
        case 
"expires":
            
$sql_order_by "message.expires";
            break;
        case 
"seen":
            
$sql_order_by "user_message.seen";
            break;  
        }

    
// Check sort value is valid
    
if (!in_array(strtolower($sort), array("asc""desc")))
    {
    
$sort "ASC";
    }

    
$messages=ps_query("SELECT user_message.ref, message.ref AS 'message_id', user.username AS owner, user_message.seen, message.created, message.expires, message.message, message.url, message.owner as ownerid, message.type " .
        
"FROM `user_message`
        INNER JOIN `message` ON user_message.message=message.ref " 
.
        
"LEFT OUTER JOIN `user` ON message.owner=user.ref " .
        
"WHERE user_message.user = ?" .
        (
$get_all " " " AND message.expires > NOW()") .
        (
$get_all " " " AND user_message.seen='0'") .
        
" ORDER BY " $sql_order_by " " $sort, array("i",$user));
    return 
count($messages) > 0;
    }

This article was last updated 20th April 2024 03:35 Europe/London time based on the source file dated 15th March 2024 09:00 Europe/London time.