summaryrefslogtreecommitdiff
path: root/app/php/controls/CalendarScaffold.php
blob: b12f6aa1c8efc8cde0be0283e8f6dff5cd5f42ca (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
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
126
127
<?php

Prado::using('Application.web.FacadeTemplateControl');

Prado::using('System.Web.UI.ActiveControls.TActiveDataGrid');
Prado::using('System.Web.UI.ActiveControls.TActiveTextBox');
Prado::using('Application.components.SafeActiveFileUpload');

Prado::using('Application.facades.CalendarFacade');

class CalendarScaffold extends FacadeTemplateControl {

    public function onPreRender($param) {
        parent::onPreRender($param);
        if (!$this->Page->IsPostBack && !$this->Page->IsCallBack) {
            $this->_rebindData();
        }
    }

    private function _rebindCalendars(array $calendars) {
        $this->Calendars->DataSource = $calendars;
        $this->Calendars->dataBind();
    }

    private function _rebindCategoryList(array $categories) {
        foreach ($this->Calendars->Columns as $column) {
            if ($column->ID === 'Category'
                && $column instanceof TActiveDropDownListColumn) {
                $column->ListDataSource = $categories;
            }
        }
    }

    private function _rebindData($refresh = FALSE) {
        $this->_rebindCategoryList(
            $this->_getCategories()
        );
        $this->_rebindCalendars(
            $this->_getCalendars($refresh)
        );
    }

    private function _getCalendars($refresh = FALSE) {
        if ($refresh) {
            $this->clearViewState('Calendars');
        }
        $calendars = $this->getViewState(
            'Calendars',
            $this->getFacade()->getAll()
        );
        $this->setViewState('Calendars', $calendars);
        return $calendars;
    }

    private function _getCategories() {
        $categories = $this->getViewState(
            'Categories',
            $this->getFacade()->getCategories()
        );
        $this->setViewState('Categories', $categories);
        return $categories;
    }

    public function editRow($sender, $param) {
        $this->Calendars->EditItemIndex = $param->Item->ItemIndex;
        $this->_rebindData();
    }

    private function _compileSaveData(TDataGridItem $item) {
        return [
            'CategoryID' => $item->Category->DropDownList->SelectedValue,
            'Visible' => $item->Visible->CheckBox->Checked,
            'CustomName' => $item->CustomName->TextBox->SafeText,
            'CustomUrl' => $item->CustomUrl->TextBox->SafeText,
            'CustomImage' => $item->CustomImage->Value->SafeText
        ];
    }

    public function saveRow($sender, $param) {
        $calendar = $this->getFacade()->get(
            $sender->DataKeys[$param->Item->ItemIndex]
        );
        if ($calendar) {
            foreach ($calendar as $c) {
                $c->saveData($this->_compileSaveData($param->Item));
            }
        } else {
            throw new TInvalidDataValueException('Calendar not found');
        }
        $this->Calendars->EditItemIndex = -1;
        $this->_rebindData(TRUE);
    }

    public function cancelRowEdit($sender, $param) {
        $this->Calendars->EditItemIndex = -1;
        $this->_rebindData();
    }

    public function uploadRowFile($sender, $param) {
        $fileType = $sender->getFileType();
        if (preg_match('/^image\//', $fileType)) {
            $calendar = $this->getFacade()->get($sender->CustomData);
            if ($calendar) {
                $targetFile = $calendar[0]->getCustomImagePath(
                    $sender->getLocalName(),
                    $fileType
                );
                if ($sender->saveAs($targetFile)) {
                    $sender->NamingContainer->CustomImage->Value->Text = basename(
                        $targetFile
                    );
                }
            } else {
                throw new TInvalidDataValueException('Calendar not found');
            }
        } else {
            throw new TInvalidDataTypeException('Invalid file type');
        }
    }

    protected function getPradoScriptDependencies() {
        return ['jquery'];
    }

}

?>