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);
|
2021-08-24 20:05:23 +00:00
|
|
|
|
2023-04-09 08:17:36 +00:00
|
|
|
namespace Dotclear\Plugin\enhancePostContent;
|
|
|
|
|
|
|
|
use cursor;
|
|
|
|
use dcCore;
|
|
|
|
use dcRecord;
|
2023-04-09 21:52:07 +00:00
|
|
|
use Exception;
|
2021-08-24 20:05:23 +00:00
|
|
|
|
2023-04-09 08:17:36 +00:00
|
|
|
class EpcRecord
|
|
|
|
{
|
|
|
|
public static function getRecords(array $params, bool $count_only = false): dcRecord
|
2021-08-24 20:05:23 +00:00
|
|
|
{
|
|
|
|
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, ' .
|
2021-08-24 20:05:23 +00:00
|
|
|
$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 ';
|
2021-08-24 20:05:23 +00:00
|
|
|
|
|
|
|
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) . "' ";
|
2021-08-24 20:05:23 +00:00
|
|
|
|
|
|
|
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']);
|
2021-08-24 20:05:23 +00:00
|
|
|
} 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']) . "' ";
|
2021-08-24 20:05:23 +00:00
|
|
|
}
|
|
|
|
} 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']);
|
2021-08-24 20:05:23 +00:00
|
|
|
} 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']) . "' ";
|
2021-08-24 20:05:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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; }});
|
2021-08-24 20:05:23 +00:00
|
|
|
} else {
|
2021-11-06 13:46:37 +00:00
|
|
|
$params['epc_id'] = [(int) $params['epc_id']];
|
2021-08-24 20:05:23 +00:00
|
|
|
}
|
2023-04-09 08:17:36 +00:00
|
|
|
$strReq .= 'AND E.epc_id ' . dcCore::app()->con->in($params['epc_id']);
|
2021-09-04 22:41:10 +00:00
|
|
|
} 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']);
|
2021-09-04 22:41:10 +00:00
|
|
|
} 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']) . "' ";
|
2021-09-04 22:41:10 +00:00
|
|
|
}
|
2021-08-24 20:05:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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']) . ' ';
|
2021-08-24 20:05:23 +00:00
|
|
|
} 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']);
|
2021-08-24 20:05:23 +00:00
|
|
|
}
|
|
|
|
|
2023-04-09 08:17:36 +00:00
|
|
|
return new dcRecord(dcCore::app()->con->select($strReq));
|
2021-08-24 20:05:23 +00:00
|
|
|
}
|
|
|
|
|
2023-04-09 08:17:36 +00:00
|
|
|
public static function addRecord(cursor $cur): int
|
2021-08-24 20:05:23 +00:00
|
|
|
{
|
2023-04-09 08:17:36 +00:00
|
|
|
dcCore::app()->con->writeLock(dcCore::app()->prefix . My::TABLE_NAME);
|
2021-08-24 20:05:23 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
2021-08-24 20:05:23 +00:00
|
|
|
$cur->insert();
|
2023-04-09 08:17:36 +00:00
|
|
|
dcCore::app()->con->unlock();
|
2021-08-24 20:05:23 +00:00
|
|
|
} catch (Exception $e) {
|
2023-04-09 08:17:36 +00:00
|
|
|
dcCore::app()->con->unlock();
|
2021-11-01 09:33:43 +00:00
|
|
|
|
2021-08-24 20:05:23 +00:00
|
|
|
throw $e;
|
|
|
|
}
|
2023-04-09 08:17:36 +00:00
|
|
|
self::trigger();
|
2021-08-24 20:05:23 +00:00
|
|
|
|
2023-04-09 21:52:07 +00:00
|
|
|
# --BEHAVIOR-- enhancePostContentAfterAddRecord : cursor
|
2022-11-13 20:30:48 +00:00
|
|
|
dcCore::app()->callBehavior('enhancePostContentAfterAddRecord', $cur);
|
2021-08-24 20:05:23 +00:00
|
|
|
|
2023-04-09 08:17:36 +00:00
|
|
|
return (int) $cur->getField('epc_id');
|
2021-08-24 20:05:23 +00:00
|
|
|
}
|
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
|
2021-08-24 20:05:23 +00:00
|
|
|
{
|
|
|
|
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'));
|
2021-08-24 20:05:23 +00:00
|
|
|
|
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();
|
2021-08-24 20:05:23 +00:00
|
|
|
|
2023-04-09 21:52:07 +00:00
|
|
|
# --BEHAVIOR-- enhancePostContentAfterUpdRecord : cursor, int
|
2022-11-13 20:30:48 +00:00
|
|
|
dcCore::app()->callBehavior('enhancePostContentAfterUpdRecord', $cur, $id);
|
2021-08-24 20:05:23 +00:00
|
|
|
}
|
|
|
|
|
2023-04-09 21:52:07 +00:00
|
|
|
public static function isRecord(?string $filter, ?string $key, ?int $not_id = null): bool
|
2021-09-04 22:41:10 +00:00
|
|
|
{
|
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);
|
2021-09-04 22:41:10 +00:00
|
|
|
}
|
|
|
|
|
2023-04-09 08:17:36 +00:00
|
|
|
public static function delRecord(int $id): void
|
2021-08-24 20:05:23 +00:00
|
|
|
{
|
|
|
|
if (empty($id)) {
|
|
|
|
throw new Exception(__('No such record ID'));
|
|
|
|
}
|
|
|
|
|
2023-04-09 21:52:07 +00:00
|
|
|
# --BEHAVIOR-- enhancePostContentBeforeDelRecord, int
|
2022-11-13 20:30:48 +00:00
|
|
|
dcCore::app()->callBehavior('enhancePostContentbeforeDelRecord', $id);
|
2021-08-24 20:05:23 +00:00
|
|
|
|
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) . "' "
|
2021-08-24 20:05:23 +00:00
|
|
|
);
|
|
|
|
|
2023-04-09 08:17:36 +00:00
|
|
|
self::trigger();
|
2021-08-24 20:05:23 +00:00
|
|
|
}
|
|
|
|
|
2023-04-09 08:17:36 +00:00
|
|
|
private static function getNextId(): int
|
2021-08-24 20:05:23 +00:00
|
|
|
{
|
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 . ' '
|
2021-08-24 20:05:23 +00:00
|
|
|
)->f(0) + 1;
|
|
|
|
}
|
|
|
|
|
2023-04-09 08:17:36 +00:00
|
|
|
public static function openCursor(): cursor
|
2021-08-24 20:05:23 +00:00
|
|
|
{
|
2023-04-09 08:17:36 +00:00
|
|
|
return dcCore::app()->con->openCursor(dcCore::app()->prefix . My::TABLE_NAME);
|
2021-08-24 20:05:23 +00:00
|
|
|
}
|
|
|
|
|
2023-04-09 08:17:36 +00:00
|
|
|
private static function getCursor(cursor $cur): void
|
2021-08-24 20:05:23 +00:00
|
|
|
{
|
2023-04-09 08:17:36 +00:00
|
|
|
if ($cur->getField('epc_key') == '') {
|
2021-08-24 20:05:23 +00:00
|
|
|
throw new Exception(__('No record key'));
|
|
|
|
}
|
2023-04-09 08:17:36 +00:00
|
|
|
if ($cur->getField('epc_value') == '') {
|
2021-08-24 20:05:23 +00:00
|
|
|
throw new Exception(__('No record value'));
|
|
|
|
}
|
2023-04-09 08:17:36 +00:00
|
|
|
if ($cur->getField('epc_filter') == '') {
|
2021-08-24 20:05:23 +00:00
|
|
|
throw new Exception(__('No record filter'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-09 08:17:36 +00:00
|
|
|
private static function trigger(): void
|
2021-08-24 20:05:23 +00:00
|
|
|
{
|
2022-11-13 20:30:48 +00:00
|
|
|
dcCore::app()->blog->triggerBlog();
|
2021-08-24 20:05:23 +00:00
|
|
|
}
|
2021-11-01 09:33:43 +00:00
|
|
|
}
|