Mokuji /www/app/AdminModule/models/AdminPagesModel.php

Language PHP Lines 125
MD5 Hash 13093abd8d6e549c1ac8348b2a0c2778
Repository https://github.com/bazo/Mokuji.git View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
/**
 * Photon CMS
 *
 * @copyright  Copyright (c) 2009 Martin Bazik
 * @package    AdminModels
 */

/**
 * Admin Menu Model
 *
 * @author     Martin Bazik
 * @package    AdminModels
 */
class Admin_PagesModel extends Admin_BaseModel
{
    protected $table = 'site_pages';

    public function _getAll()
    {
        return db::select('*')->from('[:table:]')->fetchAll();
    }

    public function _getPairs($key = 'slug', $value = 'title')
    {
        return db::select(':table:.title `page_title`, :table:.slug `page_slug`, :categories:.title `category_title`, :categories:.slug `category_slug`')->from('[:table:]')->leftJoin(':categories:')->on(':table:.category = :categories:.id')->where(':table:.homepage = 0')->orderBy(':table:.category ASC')->fetchAssoc('category_slug|page_title');
    }

    public function _getById($id)
    {
        return db::select(':table:.*,UNIX_TIMESTAMP(:table:.added) `added`, :categories:.id `category_id`, :categories:.title `category_title`, :categories:.slug `category_slug`')->from('[:table:]')->leftJoin(':categories:')->on(':table:.category = :categories:.id')->where(':table:.id = %i', $id)->fetch();
    }

    public function _getByTitle($title)
    {
        return db::select('*')->from('[:table:]')->where('title = %s', $title)->fetch();
    }

    public function _getBySlug($slug)
    {
        return db::select('*')->from('[:table:]')->where('slug = %s', $slug)->and('content_type = %s', 'page')->fetch();
    }

    public function _getRecent($count)
    {
        return db::select(':table:.*,UNIX_TIMESTAMP(:table:.added) `added`, :categories:.title `category_title`, :categories:.slug `category_slug`')->from('[:table:]')->leftJoin(':categories:')->on(':table:.category = :categories:.id')->orderBy('added DESC')->limit('%i', $count)->fetchAll();
    }

    public function _save($values)
    {
    $values['slug'] = String::webalize($values['title']);
    if (isset($values['link']))
    {
        if($values['link'] > 0)
        {
                $link = array();
                $slug = $values['slug'];
                if($values['category'] > 0)
                {
                        $cat_model = new Admin_CategoriesModel();
                        $cat = $cat_model->getById((int)$values['category']);
                        $slug = $cat->slug.'/'.$slug;
                }
                $link['title'] = $values['title'];
                $link['url'] = $slug;
                $link['parent'] = 0;
                $link['level'] = 1;
                $link['menu_id'] = (int)$values['link'];
                $link_model = new Admin_MenuItemsModel();
                $link_model->create($link);
                //refresh table alias
                $this->__after_startup();
        }
        unset($values['link']);
    }
    $values['content_type'] = 'page';
    if(!isset($values['publish_time'])) $values['publish_time'] = time();
    $values['publish_time'] = db::datetime($values['publish_time']);
    if(isset($values['homepage']))
        if($values['homepage'] == 1)
        {
                $update = array('homepage' => 0);
                db::update(':table:', $update)->execute();
        }
    db::insert(':table:', $values)->execute();
    return db::getInsertId();
    }

    public function _update($values)
    {
    if(isset($values['publish_time'])) $values['publish_time'] = db::datetime($values['publish_time']);
    if(isset($values['homepage']) && $values['homepage'] == 1)
    {
            $update = array('homepage' => 0);
            db::update(':table:', $update)->execute();
    }
    db::update(':table:', $values)->where('id = %i', $values['id'])->execute();
    }

    public function _updateByTitle($values)
    {
    if(isset($values['publish_time'])) $values['publish_time'] = db::datetime($values['publish_time']);
    db::update(':table:', $values)->where('title = %s', $values['title'])->execute();
    }

    public function _delete($title)
    {
    db::delete(':table:')->where('title = %s', $title)->execute();
    }

    public function _deleteById($id)
    {
    return db::delete(':table:')->where('id = %i', $id)->execute();
    }
    
    /**
     * @cache update
     */
    public function _MakeHomepage($title)
    {
        db::update(':table:',array('homepage' => 0))->where('homepage = %i', 1)->execute();
        db::update(':table:',array('homepage' => 1))->where('title = %s', $title)->execute();
    }
}
?>
Back to Top