Overriding functionality in backend module

The following example shows how to implement sending notifications to the Administrator mailbox when you save the news.

Open the file modules/news/admin/news.admin.php and add to the array $variables a new element with the type "none".

Example:

'main' => array (
    
'sendmail' => array(
        
'type' => 'none',
    ),
...

"None" type makes it possible not to display the field in the administrative part, but at the same time to perform the necessary code when saving and editing the element, in this case the news (for details on the types of fields you can see here).

Later in the same file add a method:

Example:

public function save_variable_sendmail()
{
    
//...
}

He will be executed every time you save the news.

We now turn to the implementation of the required functionality.

To send a mail DIAFAN.CMS function is used send_mail(). To do it, you need to connect the file includes/mail.php. Detailed description of the functions can be found in the relevant section.

The file includes/mail.php connect through the function inc() in the class Custom, so you can use this method of customized themes. Read more in the "Customization" section.

Example:

Custom::inc("includes/mail.php");

Mandatory function are three parameters – the recipient's address, message subject and text of the letter.

The address of the recipient in this case will be the e-mail addresses of our administrators. User data is stored in the table {users}. All site administrators in the field role_id have a value of 3 (if predefined user data types were not changed). You also need to add the condition "is active on the site" and "not be removed in the trash". Knowing this, we can choose the needed mailboxes.

Example:

$mails = DB::query_fetch_value("SELECT DISTINCT(mail) FROM {users} WHERE role_id=3 AND act='1' AND trash='0'", "mail");

The theme of the letter can specify an arbitrary, like this:

Example:

$subject = 'Changes on the website '.TIT1;

TIT1 – is a constant containing the name of the site referred to in the section "Site Settings" administrative part.

The text of the letter will be sent a link to store news.

Method $this->diafan->_route->link() is used to form the links. A detailed method is described in the section "Router".

To use the method, we need the value of the page ID, which is attached to the module, the module name and the ID of the current news.

ID of the page get by get_site_id().

News ID is stored in variable $this->diafan->id.

The module, in this case, has name "news".

As a result, we get the following code:

Example:

$site_id = $this->diafan->get_site_id();
$link = $this->diafan->_route->link($site_id, $this->diafan->id, "news");

It now remains to cycle cycle through all the mailboxes administrators and send them a notification. The result will look like this:

Example:

public function save_variable_sendmail()
{
    
Custom::inc("includes/mail.php");

    
//letter subject
    
$subject = 'Changes on the website '.TIT1;

    
//news link
    
$site_id = $this->diafan->get_site_id();
    
$link = BASE_PATH.$this->diafan->_route->link($site_id, $this->diafan->id, "news");

    
//message
    
$message = '<a href="'.$link.'">'.(! empty($_POST['name']) ? $_POST['name'] : $this->diafan->id).'</a>';

    
//choose values mailbox administrators
    
$mails = DB::query_fetch_value("SELECT DISTINCT(mail) FROM {users} WHERE role_id=3 AND act='1' AND trash='0'", "mail");

    
//send emails
    
foreach($mails as $mail)
    {
        
send_mail($mail, $subject, $message);
    }
}