protected function _AddEdit($Sender, $PocketID = FALSE) { $Form = new Gdn_Form(); $PocketModel = new Gdn_Model('Pocket'); $Form->SetModel($PocketModel); $Sender->ConditionModule = new ConditionModule($Sender); $Sender->Form = $Form; if ($Form->AuthenticatedPostBack()) { // Save the pocket. if ($PocketID !== FALSE) { $Form->SetFormValue('PocketID', $PocketID); } // Convert the form data into a format digestable by the database. $Repeat = $Form->GetFormValue('RepeatType'); switch ($Repeat) { case Pocket::REPEAT_EVERY: $PocketModel->Validation->ApplyRule('EveryFrequency', 'Integer'); $PocketModel->Validation->ApplyRule('EveryBegin', 'Integer'); $Frequency = $Form->GetFormValue('EveryFrequency', 1); if (!$Frequency || !ValidateInteger($Frequency) || $Frequency < 1) { $Frequency = 1; } $Repeat .= ' ' . $Frequency; if ($Form->GetFormValue('EveryBegin', 1) > 1) { $Repeat .= ',' . $Form->GetFormValue('EveryBegin'); } break; case Pocket::REPEAT_INDEX: $PocketModel->Validation->AddRule('IntegerArray', 'function:ValidateIntegerArray'); $PocketModel->Validation->ApplyRule('Indexes', 'IntegerArray'); $Indexes = explode(',', $Form->GetFormValue('Indexes', '')); $Indexes = array_map('trim', $Indexes); $Repeat .= ' ' . implode(',', $Indexes); break; default: break; } $Form->SetFormValue('Repeat', $Repeat); $Form->SetFormValue('Sort', 0); $Form->SetFormValue('Format', 'Raw'); $Condition = Gdn_Condition::ToString($Sender->ConditionModule->Conditions(TRUE)); $Form->SetFormValue('Condition', $Condition); if ($Form->GetFormValue('Ad', 0)) { $Form->SetFormValue('Type', Pocket::TYPE_AD); } else { $Form->SetFormValue('Type', Pocket::TYPE_DEFAULT); } $Saved = $Form->Save(); if ($Saved) { $Sender->StatusMessage = T('Your changes have been saved.'); $Sender->RedirectUrl = Url('settings/pockets'); } } else { if ($PocketID !== FALSE) { // Load the pocket. $Pocket = $PocketModel->GetWhere(array('PocketID' => $PocketID))->FirstRow(DATASET_TYPE_ARRAY); if (!$Pocket) { return Gdn::Dispatcher()->Dispatch('Default404'); } // Convert some of the pocket data into a format digestable by the form. list($RepeatType, $RepeatFrequency) = Pocket::ParseRepeat($Pocket['Repeat']); $Pocket['RepeatType'] = $RepeatType; $Pocket['EveryFrequency'] = GetValue(0, $RepeatFrequency, 1); $Pocket['EveryBegin'] = GetValue(1, $RepeatFrequency, 1); $Pocket['Indexes'] = implode(',', $RepeatFrequency); $Pocket['Ad'] = $Pocket['Type'] == Pocket::TYPE_AD; $Sender->ConditionModule->Conditions(Gdn_Condition::FromString($Pocket['Condition'])); $Form->SetData($Pocket); } else { // Default the repeat. $Form->SetFormValue('RepeatType', Pocket::REPEAT_ONCE); } } $Sender->Form = $Form; $Sender->SetData('Locations', $this->Locations); $Sender->SetData('LocationsArray', $this->GetLocationsArray()); $Sender->SetData('Pages', array('' => '(' . T('All') . ')', 'activity' => 'activity', 'comments' => 'comments', 'dashboard' => 'dashboard', 'discussions' => 'discussions', 'inbox' => 'inbox', 'profile' => 'profile')); return $Sender->Render('AddEdit', '', 'plugins/Pockets'); }
/** Load the pocket's data from an array. * * @param array $Data */ public function Load($Data) { $this->Body = $Data['Body']; $this->Disabled = $Data['Disabled']; $this->Format = $Data['Format']; $this->Location = $Data['Location']; $this->Name = $Data['Name']; $this->Page = $Data['Page']; $this->MobileOnly = $Data['MobileOnly']; $this->MobileNever = $Data['MobileNever']; $this->Type = val('Type', $Data, Pocket::TYPE_DEFAULT); $this->EmbeddedNever = GetValue('EmbeddedNever', $Data); $this->ShowInDashboard = GetValue('ShowInDashboard', $Data); // parse the frequency. $Repeat = $Data['Repeat']; list($this->RepeatType, $this->RepeatFrequency) = Pocket::ParseRepeat($Repeat); }