125 lines
3.9 KiB
PHP
125 lines
3.9 KiB
PHP
<?php
|
|
/**
|
|
* OnlyOfficeTemplates model: list templates by module, get from S3, merge, convert.
|
|
*/
|
|
|
|
class OnlyOfficeTemplates_Model
|
|
{
|
|
protected $db;
|
|
protected $config;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->db = PearDatabase::getInstance();
|
|
require_once dirname(__DIR__) . '/config.php';
|
|
$this->config = OnlyOfficeTemplates_getConfig();
|
|
}
|
|
|
|
/**
|
|
* List templates available for a module (and current user).
|
|
*
|
|
* @param string $module
|
|
* @return array [ ['id' =>, 'name' =>, 'file_name' =>, 'module' =>], ... ]
|
|
*/
|
|
public function getTemplatesByModule($module)
|
|
{
|
|
$userId = Users_Record_Model::getCurrentUserModel()->getId();
|
|
$sql = "SELECT id, name, module, file_name, s3_key, owner, created_at
|
|
FROM vtiger_oot_templates
|
|
WHERE module = ? AND (owner = ? OR owner IN (SELECT groupid FROM vtiger_users2group WHERE userid = ?))
|
|
ORDER BY name";
|
|
$res = $this->db->pquery($sql, [$module, $userId, $userId]);
|
|
$list = [];
|
|
while ($row = $this->db->fetchByAssoc($res)) {
|
|
$list[] = [
|
|
'id' => (int)$row['id'],
|
|
'name' => $row['name'],
|
|
'module' => $row['module'],
|
|
'file_name' => $row['file_name'],
|
|
's3_key' => $row['s3_key'],
|
|
'owner' => (int)$row['owner'],
|
|
'created_at' => $row['created_at'],
|
|
];
|
|
}
|
|
return $list;
|
|
}
|
|
|
|
/**
|
|
* Get one template by id (with permission check).
|
|
*
|
|
* @param int $templateId
|
|
* @return array|null
|
|
*/
|
|
public function getTemplateById($templateId)
|
|
{
|
|
$userId = Users_Record_Model::getCurrentUserModel()->getId();
|
|
$res = $this->db->pquery(
|
|
"SELECT id, name, module, s3_key, file_name, owner FROM vtiger_oot_templates WHERE id = ? AND (owner = ? OR owner IN (SELECT groupid FROM vtiger_users2group WHERE userid = ?))",
|
|
[$templateId, $userId, $userId]
|
|
);
|
|
if ($this->db->num_rows($res) === 0) {
|
|
return null;
|
|
}
|
|
return $this->db->fetchByAssoc($res);
|
|
}
|
|
|
|
/**
|
|
* Save template metadata and S3 key (after upload).
|
|
*
|
|
* @param string $name
|
|
* @param string $module
|
|
* @param string $s3Key
|
|
* @param string $fileName
|
|
* @param int $owner
|
|
* @return int new template id
|
|
*/
|
|
public function saveTemplate($name, $module, $s3Key, $fileName, $owner = null)
|
|
{
|
|
if ($owner === null) {
|
|
$owner = Users_Record_Model::getCurrentUserModel()->getId();
|
|
}
|
|
$this->db->pquery("INSERT INTO vtiger_oot_templates (name, module, s3_key, file_name, owner, created_at) VALUES (?,?,?,?,?,NOW())",
|
|
[$name, $module, $s3Key, $fileName, $owner]);
|
|
$id = $this->db->getLastInsertID();
|
|
return $id ? (int)$id : (int)$this->db->query_result($this->db->pquery("SELECT MAX(id) AS n FROM vtiger_oot_templates", []), 0, 'n');
|
|
}
|
|
|
|
/**
|
|
* Delete template record and optionally S3 object (caller can delete object).
|
|
*
|
|
* @param int $templateId
|
|
* @return bool
|
|
*/
|
|
public function deleteTemplate($templateId)
|
|
{
|
|
$t = $this->getTemplateById($templateId);
|
|
if (!$t) {
|
|
return false;
|
|
}
|
|
$this->db->pquery("DELETE FROM vtiger_oot_templates WHERE id = ?", [$templateId]);
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Get config (S3 prefix, bucket, OnlyOffice URL).
|
|
*
|
|
* @return array
|
|
*/
|
|
public function getConfig()
|
|
{
|
|
return $this->config;
|
|
}
|
|
|
|
/**
|
|
* Get next id for template (for S3 path).
|
|
*
|
|
* @return int
|
|
*/
|
|
public function getNextTemplateId()
|
|
{
|
|
$this->db->pquery("UPDATE vtiger_oot_templates_seq SET id = LAST_INSERT_ID(id + 1)", []);
|
|
$r = $this->db->pquery("SELECT LAST_INSERT_ID() AS n", []);
|
|
return (int)$this->db->query_result($r, 0, 'n');
|
|
}
|
|
}
|