enhancePostContent/src/EpcRecord.php

250 lines
7.7 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 dcCore;
2023-04-23 09:27:35 +00:00
use Dotclear\Database\{
Cursor,
MetaRecord
};
use Exception;
2023-04-21 22:21:30 +00:00
/**
* Filter records.
*/
2023-04-09 08:17:36 +00:00
class EpcRecord
{
2023-04-21 22:21:30 +00:00
/**
* Get records.
*
* @param array $params The query params
* @param bool $count_only Count only
*
2023-04-23 09:27:35 +00:00
* @return MetaRecord The records instance
2023-04-21 22:21:30 +00:00
*/
2023-04-23 09:27:35 +00:00
public static function getRecords(array $params, bool $count_only = false): MetaRecord
{
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-21 00:10:54 +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-23 09:27:35 +00:00
return new MetaRecord(dcCore::app()->con->select($strReq));
}
2023-04-21 22:21:30 +00:00
/**
* Add record.
*
2023-04-23 09:27:35 +00:00
* @param Cursor $cur The Cursor
2023-04-21 22:21:30 +00:00
*
* @return int The record ID
*/
2023-04-23 09:27:35 +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());
2023-04-21 00:10:54 +00:00
$cur->setField('blog_id', (string) dcCore::app()->blog?->id);
2023-04-09 08:17:36 +00:00
$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-21 22:21:30 +00:00
dcCore::app()->blog?->triggerBlog();
2023-04-23 09:27:35 +00:00
# --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-21 22:21:30 +00:00
/**
* Update a record.
*
* @param int $id The record ID
2023-04-23 09:27:35 +00:00
* @param Cursor $cur The Cursor
2023-04-21 22:21:30 +00:00
*/
2023-04-23 09:27:35 +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-21 00:10:54 +00:00
$cur->update('WHERE epc_id = ' . $id . " AND blog_id = '" . dcCore::app()->con->escapeStr((string) dcCore::app()->blog?->id) . "' ");
2023-04-21 22:21:30 +00:00
dcCore::app()->blog?->triggerBlog();
2023-04-23 09:27:35 +00:00
# --BEHAVIOR-- enhancePostContentAfterUpdRecord : Cursor, int
2022-11-13 20:30:48 +00:00
dcCore::app()->callBehavior('enhancePostContentAfterUpdRecord', $cur, $id);
}
2023-04-21 22:21:30 +00:00
/**
* Check if a record exists.
*
* @param null|string $filter The filter ID
* @param null|string $key The record key
* @param null|int $not_id Exclude an id
*
* @return bool True if it exists
*/
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-21 22:21:30 +00:00
/**
* Delete a record.
*
* @param int $id The record ID
*/
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-21 00:10:54 +00:00
"AND blog_id = '" . dcCore::app()->con->escapeStr((string) dcCore::app()->blog?->id) . "' "
);
2023-04-21 22:21:30 +00:00
dcCore::app()->blog?->triggerBlog();
}
2023-04-21 22:21:30 +00:00
/**
* Get next record ID.
*
* @return int The next record ID
*/
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-21 22:21:30 +00:00
/**
2023-04-23 09:27:35 +00:00
* Open filter Cursor.
2023-04-21 22:21:30 +00:00
*
2023-04-23 09:27:35 +00:00
* @return Cursor The Cursor
2023-04-21 22:21:30 +00:00
*/
2023-04-23 09:27:35 +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-21 22:21:30 +00:00
/**
2023-04-23 09:27:35 +00:00
* Clean up a Cursor.
2023-04-21 22:21:30 +00:00
*
2023-04-23 09:27:35 +00:00
* @param Cursor $cur The Cursor
2023-04-21 22:21:30 +00:00
*/
2023-04-23 09:27:35 +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'));
}
}
2021-11-01 09:33:43 +00:00
}