enhancePostContent/src/EpcRecord.php

200 lines
6.6 KiB
PHP
Raw Normal View History

2021-08-23 23:55:52 +00:00
<?php
2021-09-02 18:35:25 +00:00
/**
* @brief enhancePostContent, a plugin for Dotclear 2
2021-11-01 09:33:43 +00:00
*
2021-09-02 18:35:25 +00:00
* @package Dotclear
* @subpackage Plugin
2021-11-01 09:33:43 +00:00
*
2021-09-02 18:35:25 +00:00
* @author Jean-Christian Denis and Contributors
2021-11-01 09:33:43 +00:00
*
2021-09-02 18:35:25 +00:00
* @copyright Jean-Christian Denis
* @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html
*/
2023-04-09 08:17:36 +00:00
declare(strict_types=1);
2023-04-09 08:17:36 +00:00
namespace Dotclear\Plugin\enhancePostContent;
use cursor;
use dcCore;
use dcRecord;
use Exception;
2023-04-09 08:17:36 +00:00
class EpcRecord
{
public static function getRecords(array $params, bool $count_only = false): dcRecord
{
if ($count_only) {
$strReq = 'SELECT count(E.epc_id) ';
} else {
$content_req = '';
if (!empty($params['columns']) && is_array($params['columns'])) {
$content_req .= implode(', ', $params['columns']) . ', ';
}
2021-11-01 09:33:43 +00:00
$strReq = 'SELECT E.epc_id, E.blog_id, E.epc_type, E.epc_upddt, ' .
$content_req .
'E.epc_filter, E.epc_key, E.epc_value ';
}
2023-04-09 08:17:36 +00:00
$strReq .= 'FROM ' . dcCore::app()->prefix . My::TABLE_NAME . ' E ';
if (!empty($params['from'])) {
$strReq .= $params['from'] . ' ';
}
2023-04-09 08:17:36 +00:00
$strReq .= "WHERE E.blog_id = '" . dcCore::app()->con->escapeStr((string) dcCore::app()->blog->id) . "' ";
if (isset($params['epc_type'])) {
if (is_array($params['epc_type']) && !empty($params['epc_type'])) {
2023-04-09 08:17:36 +00:00
$strReq .= 'AND E.epc_type ' . dcCore::app()->con->in($params['epc_type']);
} elseif ($params['epc_type'] != '') {
2023-04-09 08:17:36 +00:00
$strReq .= "AND E.epc_type = '" . dcCore::app()->con->escapeStr((string) $params['epc_type']) . "' ";
}
} else {
$strReq .= "AND E.epc_type = 'epc' ";
}
if (isset($params['epc_filter'])) {
if (is_array($params['epc_filter']) && !empty($params['epc_filter'])) {
2023-04-09 08:17:36 +00:00
$strReq .= 'AND E.epc_filter ' . dcCore::app()->con->in($params['epc_filter']);
} elseif ($params['epc_filter'] != '') {
2023-04-09 08:17:36 +00:00
$strReq .= "AND E.epc_filter = '" . dcCore::app()->con->escapeStr((string) $params['epc_filter']) . "' ";
}
}
if (!empty($params['epc_id'])) {
if (is_array($params['epc_id'])) {
2021-11-06 13:46:37 +00:00
array_walk($params['epc_id'], function (&$v, $k) { if ($v !== null) { $v = (int) $v; }});
} else {
2021-11-06 13:46:37 +00:00
$params['epc_id'] = [(int) $params['epc_id']];
}
2023-04-09 08:17:36 +00:00
$strReq .= 'AND E.epc_id ' . dcCore::app()->con->in($params['epc_id']);
} elseif (isset($params['not_id']) && is_numeric($params['not_id'])) {
$strReq .= "AND NOT E.epc_id = '" . $params['not_id'] . "' ";
}
if (isset($params['epc_key'])) {
if (is_array($params['epc_key']) && !empty($params['epc_key'])) {
2023-04-09 08:17:36 +00:00
$strReq .= 'AND E.epc_key ' . dcCore::app()->con->in($params['epc_key']);
} elseif ($params['epc_key'] != '') {
2023-04-09 08:17:36 +00:00
$strReq .= "AND E.epc_key = '" . dcCore::app()->con->escapeStr((string) $params['epc_key']) . "' ";
}
}
if (!empty($params['sql'])) {
$strReq .= $params['sql'] . ' ';
}
if (!$count_only) {
if (!empty($params['order'])) {
2023-04-09 08:17:36 +00:00
$strReq .= 'ORDER BY ' . dcCore::app()->con->escapeStr((string) $params['order']) . ' ';
} else {
$strReq .= 'ORDER BY E.epc_key ASC ';
}
}
if (!$count_only && !empty($params['limit'])) {
2023-04-09 08:17:36 +00:00
$strReq .= dcCore::app()->con->limit($params['limit']);
}
2023-04-09 08:17:36 +00:00
return new dcRecord(dcCore::app()->con->select($strReq));
}
2023-04-09 08:17:36 +00:00
public static function addRecord(cursor $cur): int
{
2023-04-09 08:17:36 +00:00
dcCore::app()->con->writeLock(dcCore::app()->prefix . My::TABLE_NAME);
try {
2023-04-09 08:17:36 +00:00
$cur->setField('epc_id', self::getNextId());
$cur->setField('blog_id', dcCore::app()->blog->id);
$cur->setField('epc_upddt', date('Y-m-d H:i:s'));
2021-09-02 18:35:25 +00:00
2023-04-09 08:17:36 +00:00
self::getCursor($cur);
2021-09-02 18:35:25 +00:00
$cur->insert();
2023-04-09 08:17:36 +00:00
dcCore::app()->con->unlock();
} catch (Exception $e) {
2023-04-09 08:17:36 +00:00
dcCore::app()->con->unlock();
2021-11-01 09:33:43 +00:00
throw $e;
}
2023-04-09 08:17:36 +00:00
self::trigger();
# --BEHAVIOR-- enhancePostContentAfterAddRecord : cursor
2022-11-13 20:30:48 +00:00
dcCore::app()->callBehavior('enhancePostContentAfterAddRecord', $cur);
2023-04-09 08:17:36 +00:00
return (int) $cur->getField('epc_id');
}
2021-09-02 18:35:25 +00:00
2023-04-09 08:17:36 +00:00
public static function updRecord(int $id, cursor $cur): void
{
if (empty($id)) {
throw new Exception(__('No such record ID'));
}
2023-04-09 08:17:36 +00:00
$cur->setField('epc_upddt', date('Y-m-d H:i:s'));
2023-04-09 08:17:36 +00:00
$cur->update('WHERE epc_id = ' . $id . " AND blog_id = '" . dcCore::app()->con->escapeStr((string) dcCore::app()->blog->id) . "' ");
self::trigger();
# --BEHAVIOR-- enhancePostContentAfterUpdRecord : cursor, int
2022-11-13 20:30:48 +00:00
dcCore::app()->callBehavior('enhancePostContentAfterUpdRecord', $cur, $id);
}
public static function isRecord(?string $filter, ?string $key, ?int $not_id = null): bool
{
2023-04-09 08:17:36 +00:00
return 0 < self::getRecords([
2021-11-01 09:33:43 +00:00
'epc_filter' => $filter,
'epc_key' => $key,
2022-11-13 20:30:48 +00:00
'not_id' => $not_id,
2021-11-01 09:33:43 +00:00
], true)->f(0);
}
2023-04-09 08:17:36 +00:00
public static function delRecord(int $id): void
{
if (empty($id)) {
throw new Exception(__('No such record ID'));
}
# --BEHAVIOR-- enhancePostContentBeforeDelRecord, int
2022-11-13 20:30:48 +00:00
dcCore::app()->callBehavior('enhancePostContentbeforeDelRecord', $id);
2023-04-09 08:17:36 +00:00
dcCore::app()->con->execute(
'DELETE FROM ' . dcCore::app()->prefix . My::TABLE_NAME . ' ' .
2021-11-01 09:33:43 +00:00
'WHERE epc_id = ' . $id . ' ' .
2023-04-09 08:17:36 +00:00
"AND blog_id = '" . dcCore::app()->con->escapeStr((string) dcCore::app()->blog->id) . "' "
);
2023-04-09 08:17:36 +00:00
self::trigger();
}
2023-04-09 08:17:36 +00:00
private static function getNextId(): int
{
2023-04-09 08:17:36 +00:00
return (int) dcCore::app()->con->select(
'SELECT MAX(epc_id) FROM ' . dcCore::app()->prefix . My::TABLE_NAME . ' '
)->f(0) + 1;
}
2023-04-09 08:17:36 +00:00
public static function openCursor(): cursor
{
2023-04-09 08:17:36 +00:00
return dcCore::app()->con->openCursor(dcCore::app()->prefix . My::TABLE_NAME);
}
2023-04-09 08:17:36 +00:00
private static function getCursor(cursor $cur): void
{
2023-04-09 08:17:36 +00:00
if ($cur->getField('epc_key') == '') {
throw new Exception(__('No record key'));
}
2023-04-09 08:17:36 +00:00
if ($cur->getField('epc_value') == '') {
throw new Exception(__('No record value'));
}
2023-04-09 08:17:36 +00:00
if ($cur->getField('epc_filter') == '') {
throw new Exception(__('No record filter'));
}
}
2023-04-09 08:17:36 +00:00
private static function trigger(): void
{
2022-11-13 20:30:48 +00:00
dcCore::app()->blog->triggerBlog();
}
2021-11-01 09:33:43 +00:00
}