Skip to content
Snippets Groups Projects
class.draft.php 6.25 KiB
<?php

/**
 * Class: Draft
 *
 * Defines a simple draft-saving mechanism for osTicket which supports draft
 * fetch and update via an ajax mechanism (include/ajax.draft.php).
 *
 * Fields:
 * id - (int:auto:pk) Draft ID number
 * body - (text) Body of the draft
 * namespace - (string) Identifier of draft grouping — useful for multiple
 *      drafts on the same document by different users
 * staff_id - (int:null) Staff owner of the draft
 * extra - (text:json) Extra attributes of the draft
 * created - (date) Date draft was initially created
 * updated - (date:null) Date draft was last updated
 */
class Draft extends VerySimpleModel {

    static $meta = array(
        'table' => DRAFT_TABLE,
        'pk' => array('id'),
        'joins' => array(
            'attachments' => array(
                'constraint' => array(
                    "'D'" => 'Attachment.type',
                    'id' => 'Attachment.object_id',
                ),
                'list' => true,
                'null' => true,
                'broker' => 'GenericAttachments',
            ),
        ),
    );

    function getId() { return $this->id; }
    function getBody() { return $this->body; }
    function getStaffId() { return $this->staff_id; }
    function getNamespace() { return $this->namespace; }

    static protected function getCurrentUserId() {
        global $thisstaff, $thisclient;
        
        $user = $thisstaff ?: $thisclient;
        if ($user)
            return $user->getId();

        return 1 << 31;
    }

    static function getDraftAndDataAttrs($namespace, $id=0, $original='') {
        $draft_body = null;
        $attrs = array(sprintf('data-draft-namespace="%s"', Format::htmlchars($namespace)));
        $criteria = array(
            'namespace' => $namespace,
            'staff_id' => self::getCurrentUserId(),
        );
        if ($id) {
            $attrs[] = sprintf('data-draft-object-id="%s"', Format::htmlchars($id));
            $criteria['namespace'] .= '.' . $id;
        }
        if ($draft = static::objects()->filter($criteria)->first()) {
            $attrs[] = sprintf('data-draft-id="%s"', $draft->getId());
            $draft_body = $draft->getBody();
        }
        $attrs[] = sprintf('data-draft-original="%s"',
            Format::htmlchars(Format::viewableImages($original)));

        return array(Format::htmlchars(Format::viewableImages($draft_body)),