summaryrefslogtreecommitdiff
path: root/app/php/model/Calendar.php
blob: 9de49207b1118b3fcfd6ffafb9747b0d32edc1fc (plain)
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
<?php

Prado::using('Application.db.ActiveRecord');
Prado::using('Application.model.Entry');
Prado::using('Application.model.Category');

class Calendar extends ActiveRecord {

    const TABLE = 'calendars';

    public $UID;
    public $Url;
    public $Name;
    public $Website;
    public $Visible;
    public $CustomName;
    public $CustomImage;
    public $CustomUrl;
    public $LastUpdated;

    public $CategoryID;

    public static $COLUMN_MAPPING = [
        'uid' => 'UID',
        'url' => 'Url',
        'name' => 'Name',
        'website' => 'Website',
        'visible' => 'Visible',
        'last_updated' => 'LastUpdated',
        'custom_name' => 'CustomName',
        'custom_image' => 'CustomImage',
        'custom_url' => 'CustomUrl',
        '_category' => 'CategoryID'
    ];

    public static $RELATIONS = [
        'Entries' => [self::HAS_MANY, 'Entry', '_calendar'],
        'Category' => [self::BELONGS_TO, 'Category', '_category']
    ];

    public static function finder($className=__CLASS__) {
        return parent::finder($className);
    }

    const CUSTOM_IMAGE_PATH = '../../resources/images/calendars/';

    public function getCustomImageUrl() {
        if ($this->CustomImage) {
            if (!preg_match('#^//#', $this->CustomImage)) {
                return Prado::getApplication()->getAssetManager()->publishFilePath(
                    implode(
                        DIRECTORY_SEPARATOR,
                        [
                            Prado::getApplication()->getBasePath(),
                            self::CUSTOM_IMAGE_PATH,
                            $this->CustomImage
                        ]
                    ),
                    TRUE
                );
            }
            return $this->CustomImage;
        }
    }

    public function getCustomImagePath($forFile = NULL, $type = '') {
        $pathParts = [
            Prado::getApplication()->getBasePath(),
            self::CUSTOM_IMAGE_PATH
        ];
        if ($forFile) {
            $pathParts[] = $this->_getCustomImageHash($forFile, $type);
        }
        return implode(DIRECTORY_SEPARATOR, $pathParts);
    }

    private function _getCustomImageHash($file, $type) {
        $hash = md5($file . md5_file($file) . filemtime($file));
        if ($type) {
            $hash .= '.' . preg_replace('#^image/#', '', $type);
        }
        return $hash;
    }

    public function saveData($data) {
        $this->copyFrom($data);
        return $this->save();
    }

}

?>