/** * Build the filter from configuration * * @param array $cabinNamespaces * @param array $motifs * @return PreferencesFilter */ public static function fromConfig(array $cabinNamespaces = [], array $motifs = []) : self { $filterContainer = new PreferencesFilter(); foreach ($cabinNamespaces as $cabin) { $activeCabin = $motifs[$cabin]; $filterContainer->addFilter('prefs.motif.' . $cabin, (new StringFilter())->addCallback(function ($selected) use($cabin, $activeCabin) : string { foreach ($activeCabin as $cabinConfig) { if ($selected === $cabinConfig['path']) { return $selected; } } return ''; })); } return $filterContainer; }
/** * Allows users to select which Motif to use * * @route my/preferences */ public function preferences() { if (!$this->isLoggedIn()) { \Airship\redirect($this->airship_cabin_prefix); } $prefs = $this->acct->getUserPreferences($this->getActiveUserId()); $cabins = []; $motifs = []; foreach ($this->getCabinNamespaces() as $cabin) { $cabins[] = $cabin; $filename = ROOT . '/tmp/cache/' . $cabin . '.motifs.json'; if (\file_exists($filename)) { $motifs[$cabin] = \Airship\loadJSON($filename); } else { $motifs[$cabin] = []; } } $post = $this->post(PreferencesFilter::fromConfig($cabins, $motifs)); if (!empty($post)) { if ($this->savePreferences($post['prefs'], $cabins, $motifs)) { \Airship\redirect($this->airship_cabin_prefix . '/my/preferences'); } } $this->lens('preferences', ['prefs' => $prefs, 'motifs' => $motifs]); }