Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

logging to iblocks and show notify for admin #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions src/Agents/Iblock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?
namespace Bex\Monolog\Agents;

class Iblock
{
/**
* Deleting all elements from iblock, except the first $countElements elements
* @param $countElements
* @param $iblockID
* @return string
*/
public static function AgentDeleteOldElementsByCount($countElements, $iblockID)
{
if (\CModule::IncludeModule("iblock")) {
$arSelect = Array("ID");
$arFilter = Array("IBLOCK_ID" => $iblockID);
$rsResult = \CIBlockElement::GetList(Array("timestamp_x" => "DESC"), $arFilter, false, false, $arSelect);
$intCounter = 1;
while ($arItem = $rsResult->GetNext()) {
if ($intCounter > $countElements) {
\CIBlockElement::Delete($arItem["ID"]);
}
$intCounter++;
}
}

return "\Bex\Monolog\Agents\Iblock::AgentDeleteOldElementsByCount(" . $countElements . ", " . $iblockID . ");";
}

/**
* Deleting elements from iblock, who are older than $countDays days
* @param $countDays
* @param $iblockID
* @return string
*/
public static function AgentDeleteOldElementsByLastChangeTime($countDays, $iblockID)
{
if (\CModule::IncludeModule("iblock")) {
$arSelect = Array("ID");
$arFilter = Array(
"IBLOCK_ID" => $iblockID,
">DATE_ACTIVE_FROM" => (new \DateTime())->modify('-' . $countDays . ' days')->format('d.m.Y')
);
$rsResult = \CIBlockElement::GetList(Array(), $arFilter, false, false, $arSelect);
while ($arItem = $rsResult->GetNext()) {
\CIBlockElement::Delete($arItem["ID"]);
}
}

return "\Bex\Monolog\Agents\Iblock::AgentDeleteOldElementsByLastChangeTime(" . $countDays . ", " . $iblockID . ");";
}
}
48 changes: 48 additions & 0 deletions src/Formatter/BitrixIblockFormatter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
/**
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*/

namespace Bex\Monolog\Formatter;

use Bitrix\Main\Type\DateTime;

/**
* Record formatter for iblock of Bitrix.
*
* Context of record will also be written to iblock of Bitrix.
*
* @author GSU <gsu1234@mail.ru>
*/
class BitrixIblockFormatter extends BitrixFormatter
{
/**
* {@inheritdoc}
*/
public function format(array $record)
{
$record['level'] = parent::toBitrixLevel($record['level']);

$objDateTime = new DateTime();
$record['title'] = $record["channel"] . ": ";
$record['title'] .= "[" . $record['level'] . "] ";
$record['title'] .= $record['message'];
$record['title'] .= " - " . $objDateTime->toString();

if (!empty($record['context']))
{
$arData = [];
foreach ($record['context'] as $field => $value) {
if (is_array($value)) {
$value = var_export($value, true);
}
$arData[] = $field . "\n\n" . $value;
}
$record['data'] = implode("\n\n\n\n", $arData);
}

return $record;
}

}
127 changes: 127 additions & 0 deletions src/Handler/BitrixIblockHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<?php
/**
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*/

namespace Bex\Monolog\Handler;

use Monolog\Handler\AbstractProcessingHandler;
use Monolog\Logger;
use Bex\Monolog\Formatter\BitrixIblockFormatter;
use Bitrix\Main\Loader;
/**
* Monolog handler for the iblock of Bitrix CMS.
*
* @author GSU <gsu1234@mail.ru>
*/
class BitrixIblockHandler extends AbstractProcessingHandler
{
private $iblockID;
private $notifyToAdmin;

/**
* BitrixIblockHandler constructor.
* @param int $iblock_id
* @param bool|int $level The minimum logging level at which this handler will be triggered.
* @param int $rotating_count_elements Deleting by agents all elements from iblock, except the first
* $rotating_count_elements elements. If $rotating_count_elements == 0, then the elements will not be deleted.
* @param int $rotating_count_days Deleting elements from iblock, who are older than $countDays days.
* @param bool $notify_to_admin
* @param bool $bubble Whether the messages that are handled can bubble up the stack or not.
*/
public function __construct($iblock_id, $level = Logger::DEBUG, $rotating_count_elements = 0, $rotating_count_days = 0, $notify_to_admin = false, $bubble = true)
{
parent::__construct($level, $bubble);
$this->setIblockID($iblock_id);
$this->setNotifyToAdmin($notify_to_admin);

if($rotating_count_elements > 0) {
$result = \CAgent::GetList(Array("ID" => "DESC"), array("NAME" => "\Bex\Monolog\Agents\Iblock::AgentDeleteOldElementsByCount(" . $rotating_count_elements . ", " . $iblock_id . ");"));
if(!$result->NavNext(true)){
\CAgent::AddAgent(
"\Bex\Monolog\Agents\Iblock::AgentDeleteOldElementsByCount(" . $rotating_count_elements . ", " . $iblock_id . ");"
);
}
}

if($rotating_count_days > 0) {
$result = \CAgent::GetList(Array("ID" => "DESC"), array("NAME" => "\Bex\Monolog\Agents\Iblock::AgentDeleteOldElementsByLastChangeTime(" . $rotating_count_days . ", " . $iblock_id . ");"));
if(!$result->NavNext(true)){
\CAgent::AddAgent(
"\Bex\Monolog\Agents\Iblock::AgentDeleteOldElementsByLastChangeTime(" . $rotating_count_days . ", " . $iblock_id . ");"
);
}
}
}

/**
* {@inheritdoc}
*/
protected function write(array $record)
{
global $USER;
Loader::includeModule("iblock");
$el = new \CIBlockElement;
$arLoadElementArray = Array(
"MODIFIED_BY" => $USER->GetID(),
"IBLOCK_SECTION_ID" => false,
"IBLOCK_ID" => $this->getIblockID(),
"NAME" => $record['formatted']['title'],
"ACTIVE" => "Y",
"PREVIEW_TEXT" => $record['formatted']['data']
);
$logRecordID = $el->Add($arLoadElementArray);

if($this->getNotifyToAdmin()) {
Loader::includeModule("main");
\CAdminNotify::Add(array(
'MESSAGE' => $record['formatted']['title'],
'TAG' => $record["channel"] . "_" . $logRecordID,
'MODULE_ID' => '0',
'ENABLE_CLOSE' => 'Y'
)
);

}

}

/**
* {@inheritdoc}
*/
protected function getDefaultFormatter()
{
return new BitrixIblockFormatter();
}

/**
* @param $iblockID
*/
public function setIblockID($iblockID){
$this->iblockID = $iblockID;
}

/**
* @return mixed
*/
public function getIblockID(){
return $this->iblockID;
}

/**
* @param $notifyToAdmin
*/
public function setNotifyToAdmin($notifyToAdmin){
$this->notifyToAdmin = $notifyToAdmin;
}

/**
* @return mixed
*/
public function getNotifyToAdmin(){
return $this->notifyToAdmin;
}


}