summaryrefslogtreecommitdiff
path: root/app/frontend/controls/CalendarScaffold.php
diff options
context:
space:
mode:
Diffstat (limited to 'app/frontend/controls/CalendarScaffold.php')
-rw-r--r--app/frontend/controls/CalendarScaffold.php142
1 files changed, 142 insertions, 0 deletions
diff --git a/app/frontend/controls/CalendarScaffold.php b/app/frontend/controls/CalendarScaffold.php
new file mode 100644
index 0000000..7f15a32
--- /dev/null
+++ b/app/frontend/controls/CalendarScaffold.php
@@ -0,0 +1,142 @@
+<?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(bool $refresh = FALSE) {
+ $this->_rebindCategoryList(
+ $this->_getCategories()
+ );
+ $this->_rebindCalendars(
+ $this->_getCalendars($refresh)
+ );
+ }
+
+ private function _getCalendars(bool $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(
+ Prado::localize('Calendar not found')
+ );
+ }
+ $this->Calendars->EditItemIndex = -1;
+ $this->_rebindData(TRUE);
+ }
+
+ public function cancelRowEdit($sender, $param) {
+ $this->Calendars->EditItemIndex = -1;
+ $this->_rebindData();
+ }
+
+ public function toggleDefaultState($sender, $param) {
+ $calendar = $this->getFacade()->get($sender->CustomData);
+ if ($calendar) {
+ $calendar[0]->Visible = $sender->Checked;
+ $calendar[0]->save();
+ $this->_rebindData(TRUE);
+ }
+ }
+
+ 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(
+ Prado::localize('Calendar not found')
+ );
+ }
+ } else {
+ throw new TInvalidDataTypeException(
+ Prado::localize('Invalid file type')
+ );
+ }
+ }
+
+ protected function getPradoScriptDependencies() {
+ return ['jquery'];
+ }
+
+}
+
+?>