PocketsPlugin::_addEdit PHP Method

_addEdit() protected method

protected _addEdit ( $Sender, boolean | false $PocketID = false ) : mixed
$Sender
$PocketID boolean | false
return mixed
    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');
    }