Skip to content
Snippets Groups Projects
class.page.php 10.6 KiB
Newer Older
  • Learn to ignore specific revisions
  • Peter Rotich's avatar
    Peter Rotich committed
    <?php
    /*********************************************************************
        class.page.php
    
        Page class
    
        Copyright (c)  2006-2013 osTicket
        http://www.osticket.com
    
        Released under the GNU General Public License WITHOUT ANY WARRANTY.
        See LICENSE.TXT for details.
    
        vim: expandtab sw=4 ts=4 sts=4:
    **********************************************************************/
    
    
    class Page extends VerySimpleModel {
    
        static $meta = array(
            'table' => PAGE_TABLE,
            'pk' => array('id'),
            'ordering' => array('name'),
            'defer' => array('body'),
            'joins' => array(
                'topics' => array(
                    'reverse' => 'Topic.page',
                ),
    
                'attachments' => array(
                    'constraint' => array(
                        "'P'" => 'Attachment.type',
                        'id' => 'Attachment.object_id',
                    ),
                    'list' => true,
                    'null' => true,
                    'broker' => 'GenericAttachments',
                ),
    
        var $_local;
    
    Peter Rotich's avatar
    Peter Rotich committed
    
        function getId() {
            return $this->id;
        }
    
        function getHashtable() {
    
            $base = $this->ht;
            unset($base['topics']);
    
            unset($base['attachments']);
    
            return $base;
    
    Peter Rotich's avatar
    Peter Rotich committed
        }
    
        function getType() {
    
            return $this->type;
    
    Peter Rotich's avatar
    Peter Rotich committed
        }
    
        function getName() {
    
            return $this->name;
    
    Peter Rotich's avatar
    Peter Rotich committed
        }
    
        function getLocalName($lang=false) {
    
            return $this->getLocal('name', $lang);
    
        function getNameAsSlug() {
            return urlencode(Format::slugify($this->name));
        }
    
    Peter Rotich's avatar
    Peter Rotich committed
    
        function getBody() {
    
            return $this->body;
    
    Peter Rotich's avatar
    Peter Rotich committed
        }
    
        function getLocalBody($lang=false) {
            return $this->_getLocal('body', $lang);
    
        function getBodyWithImages() {
    
            return Format::viewableImages($this->getLocalBody());
    
        function _getLocal($what, $lang=false) {
    
                $lang = Internationalization::getCurrentLanguage();
    
            }
            $translations = $this->getAllTranslations();
            foreach ($translations as $t) {
    
                if ($lang == $t->lang) {
    
                    $data = $t->getComplex();
                    if (isset($data[$what]))
                        return $data[$what];
    
            return $this->ht[$what];
        }
    
    
        function getAllTranslations() {
            if (!isset($this->_local)) {
                $tag = $this->getTranslateTag('name:body');
                $this->_local = CustomDataTranslation::allTranslations($tag, 'article');
            }
            return $this->_local;
        }
    
    
    Peter Rotich's avatar
    Peter Rotich committed
        function getNotes() {
    
            return $this->notes;
    
    Peter Rotich's avatar
    Peter Rotich committed
        }
    
        function isActive() {
    
            return ($this->isactive);
    
    Peter Rotich's avatar
    Peter Rotich committed
        }
    
        function isInUse() {
            global $cfg;
    
            return  ($this->getNumTopics()
                        || in_array($this->getId(), $cfg->getDefaultPages()));
        }
    
    
        function getCreateDate() {
    
            return $this->created;
    
    Peter Rotich's avatar
    Peter Rotich committed
        }
    
        function getUpdateDate() {
    
            return $this->updated;
    
    Peter Rotich's avatar
    Peter Rotich committed
        }
    
        function getNumTopics() {
    
            return $this->topics->count();
    
        function getTranslateTag($subtag) {
    
            return _H(sprintf('page.%s.%s', $subtag, $this->getId()));
    
        }
        function getLocal($subtag) {
            $tag = $this->getTranslateTag($subtag);
            $T = CustomDataTranslation::translate($tag);
            return $T != $tag ? $T : $this->ht[$subtag];
        }
    
    
    Peter Rotich's avatar
    Peter Rotich committed
        function disable() {
    
            if(!$this->isActive())
                return true;
    
            if($this->isInUse())
                return false;
    
    
    
            $this->isactive = 0;
            return $this->save();
    
    Peter Rotich's avatar
    Peter Rotich committed
        }
    
        function delete() {
    
    
            if ($this->isInUse())
    
    Peter Rotich's avatar
    Peter Rotich committed
                return false;
    
    
            if (!parent::delete())
    
    Peter Rotich's avatar
    Peter Rotich committed
                return false;
    
    
            return Topic::objects()
                ->filter(array('page_id'=>$this->getId()))
                ->update(array('page_id'=>0));
        }
    
        function save($refetch=false) {
            if ($this->dirty)
                $this->updated = SqlFunction::NOW();
            return parent::save($refetch || $this->dirty);
    
    Peter Rotich's avatar
    Peter Rotich committed
        }
    
        /* ------------------> Static methods <--------------------- */
    
    
        static function create($vars=false) {
    
            $page = new static($vars);
    
            $page->created = SqlFunction::NOW();
            return $page;
        }
    
    
    Peter Rotich's avatar
    Peter Rotich committed
        function add($vars, &$errors) {
            if(!($id=self::create($vars, $errors)))
                return false;
    
            return self::lookup($id);
        }
    
    
        static function getPages($criteria=array()) {
            $pages = self::objects();
    
    Peter Rotich's avatar
    Peter Rotich committed
            if(isset($criteria['active']))
    
                $pages = $pages->filter(array('isactive'=>$criteria['active']));
    
    Peter Rotich's avatar
    Peter Rotich committed
            if(isset($criteria['type']))
    
                $pages = $pages->filter(array('type'=>$criteria['type']));
    
            return $pages;
    
        static function getActivePages($criteria=array()) {
    
    Peter Rotich's avatar
    Peter Rotich committed
    
            $criteria = array_merge($criteria, array('active'=>true));
    
            return self::getPages($criteria);
        }
    
    
        static function getActiveThankYouPages() {
    
    Peter Rotich's avatar
    Peter Rotich committed
            return self::getActivePages(array('type' => 'thank-you'));
        }
    
    
        static function lookup($id, $lang=false) {
            try {
    
                $qs = self::objects()->filter(is_array($id) ? $id : array('id'=>$id));
    
                if ($lang)
                    $qs = $qs->filter(array('lang'=>$lang));
                return $qs->one();
            }
            catch (DoesNotExist $ex) {
                return null;
            }
        }
    
    
        static function getIdByName($name, $lang=false) {
            try {
                $qs = self::objects()->filter(array('name'=>$name))
                    ->values_flat('id');
                list($id) = $qs->one();
                return $id;
            }
            catch (DoesNotExist $ex) {
                return null;
            }
    
            catch (InconsistentModelException $ex) {
                // This largely happens on upgrades, and may specifically cause
                // the staff login box to crash
                return null;
            }
    
        static function lookupByType($type, $lang=false) {
    
                return self::objects()->filter(array('type'=>$type))->one();
    
            }
            catch (DoesNotExist $ex) {
                return null;
            }
    
    Peter Rotich's avatar
    Peter Rotich committed
            catch (InconsistentModelException $ex) {
                return null;
            }
    
        function update($vars, &$errors, $allowempty=false) {
    
    Peter Rotich's avatar
    Peter Rotich committed
    
            //Cleanup.
            $vars['name']=Format::striptags(trim($vars['name']));
    
            //validate
    
            if (isset($this->id) && !$vars['isactive'] && $this->isInUse()) {
                $errors['err'] = __('A page currently in-use CANNOT be disabled!');
                $errors['isactive'] = __('Page is in-use!');
            }
    
            if (isset($this->id) && $this->getId() != $vars['id'])
    
                $errors['err'] = sprintf('%s - %s', __('Internal error occurred'), __('Please try again!'));
    
    Peter Rotich's avatar
    Peter Rotich committed
    
            if(!$vars['type'])
    
                $errors['type'] = __('Type is required');
    
    Peter Rotich's avatar
    Peter Rotich committed
    
            if(!$vars['name'])
    
                $errors['name'] = __('Name is required');
    
            elseif(($pid=self::getIdByName($vars['name'])) && $pid!=$this->getId())
    
                $errors['name'] = __('Name already exists');
    
            if(!$vars['body'] && !$allowempty)
    
                $errors['body'] = __('Page body is required');
    
    Peter Rotich's avatar
    Peter Rotich committed
    
            if($errors) return false;
    
    
            $this->type = $vars['type'];
            $this->name = $vars['name'];
            $this->body = Format::sanitize($vars['body']);
            $this->isactive = (bool) $vars['isactive'];
            $this->notes = Format::sanitize($vars['notes']);
    
    
            $isnew = !isset($this->id);
            $rv = $this->save();
            if (!$isnew)
    
                $rv = $this->saveTranslations($vars, $errors);
    
            // Attach inline attachments from the editor
    
            $keepers = Draft::getAttachmentIds($vars['body']);
            $keepers = array_map(function($i) { return $i['id']; }, $keepers);
            $this->attachments->keepOnlyFileIds($keepers, true);
    
            if ($rv)
                return $rv;
    
            $errors['err']=sprintf(__('Unable to update %s.'), __('this site page'));
    
    Peter Rotich's avatar
    Peter Rotich committed
            return false;
        }
    
    
        function saveTranslations($vars, &$errors) {
            global $thisstaff;
    
    
            $tag = $this->getTranslateTag('name:body');
    
            $translations = CustomDataTranslation::allTranslations($tag, 'article');
    
            foreach ($translations as $t) {
    
                $title = @$vars['trans'][$t->lang]['title'];
                $body = @$vars['trans'][$t->lang]['body'];
                if (!$title && !$body)
    
                // Content is not new and shouldn't be added below
    
                unset($vars['trans'][$t->lang]['title']);
                unset($vars['trans'][$t->lang]['body']);
    
                $content = array('name' => $title, 'body' => Format::sanitize($body));
    
                // Don't update content which wasn't updated
                if ($content == $t->getComplex())
    
                $t->text = $content;
                $t->agent_id = $thisstaff->getId();
    
                $t->updated = SqlFunction::NOW();
    
                if (!$t->save())
                    return false;
            }
            // New translations (?)
    
    Peter Rotich's avatar
    Peter Rotich committed
            foreach ($vars['trans'] ?: array() as $lang=>$parts) {
    
                $content = array('name' => @$parts['title'], 'body' => Format::sanitize(@$parts['body']));
                if (!array_filter($content))
    
                    continue;
                $t = CustomDataTranslation::create(array(
                    'type'      => 'article',
                    'object_hash' => $tag,
                    'lang'      => $lang,
                    'text'      => $content,
                    'revision'  => 1,
                    'agent_id'  => $thisstaff->getId(),
                    'updated'   => SqlFunction::NOW(),
                ));
                if (!$t->save())
                    return false;
    
    
        static function getContext($type) {
            $context = array(
            'thank-you' => array('ticket'),
            'registration-staff' => array(
                // 'token' => __('Special authentication token'),
                'staff' => array('class' => 'Staff', 'desc' => __('Message recipient')),
                'recipient' => array('class' => 'Staff', 'desc' => __('Message recipient')),
                'link',
            ),
            'pwreset-staff' => array(
                'staff' => array('class' => 'Staff', 'desc' => __('Message recipient')),
                'recipient' => array('class' => 'Staff', 'desc' => __('Message recipient')),
                'link',
            ),
            'registration-client' => array(
                // 'token' => __('Special authentication token'),
                'recipient' => array('class' => 'User', 'desc' => __('Message recipient')),
                'link', 'user',
            ),
            'pwreset-client' => array(
                'recipient' => array('class' => 'User', 'desc' => __('Message recipient')),
                'link', 'user',
            ),
            'access-link' => array('ticket', 'recipient'),
            );
    
            return $context[$type];
        }