public function getEffectiveVariant($options) { $variants = aTools::getVariantsForSlotType($this->type, $options); if (!isset($variants)) { // No variants, no class return ''; } // Treat null and an empty string the same $variant = $this->variant . ''; // If the variant is not defined (and the empty string will not be), // and there is at least one variant, return the first one as the default. // If there are no variants return an empty string if (!isset($variants[$variant])) { if (count($variants)) { // Return the first variant for the type, if any, when the variant is bogus $keys = array_keys($variants); return $keys[0]; } return ''; } // If the variant is valid, return it as the CSS class return $this->variant; }
<?php use_helper('I18N'); $options = $sf_user->getAttribute("slot-options-{$pageid}-{$name}-{$permid}", null, 'apostrophe'); $variants = aTools::getVariantsForSlotType($slot->type, $options); if (count($variants) > 1) { ?> <?php // You can't switch variants until you've saved something for architectural reasons, however ?> <?php // we do need this menu waiting in the wings so that we can turn it on on the first save of an edit view ?> <li class="a-controls-item variant" style="<?php echo $slot->isNew() ? "display:none" : ""; ?> " id="a-<?php echo "{$pageid}-{$name}-{$permid}-variant"; ?> "> <?php echo jq_link_to_function(__('Options', null, 'apostrophe'), '$("#a-' . $pageid . '-' . $name . '-' . $permid . '-variant").toggleClass("open").children("ul.a-variant-options").toggle()', array('class' => 'a-variant-options-toggle a-btn icon a-settings', 'id' => 'a-' . $pageid . '-' . $name . '-' . $permid . '-variant-options-toggle')); ?> <ul class="a-options a-variant-options dropshadow"> <?php foreach ($variants as $variant => $settings) { ?> <?php // These classes and ids are carefully set up so that _ajaxUpdateSlot can ?> <?php
protected function setup() { if (!isset($this->options)) { // Prevents numerous warnings and problems if there are no slot options present $this->options = array(); } $this->page = aTools::getCurrentPage(); $this->slug = $this->page->slug; // TODO: remove this workaround in 1.5. All uses of actual_slug and real-slug need to go away // in favor of actual_url, we just don't want to break any old overrides in client projects. $this->realSlug = aTools::getRealPage() ? aTools::getRealPage()->getSlug() : 'global'; $this->slot = $this->page->getSlot($this->name, $this->permid); if (!$this->slot || $this->slot->type !== $this->type) { $this->slot = $this->page->createSlot($this->type); } if ($this->getOption('edit')) { $this->editable = true; } else { if (aTools::getAllowSlotEditing()) { $this->editable = $this->page->userHasPrivilege('edit'); } else { $this->editable = false; } } if ($this->getOption('preview')) { $this->editable = false; } if ($this->editable) { $user = $this->getUser(); $id = $this->page->getId(); $name = $this->name; $permid = $this->permid; // Make sure the options passed to a_slot // can be found again at save time if (!$this->updating) { // Slot options can be influenced by variant switching, and that's fine, and the editor might // need to know about it to do the right thing, so it's appropriate to reset the slot // options in the attribute. However, we also need to know what the original, pristine // options from a_slot or a_area were in order to allow variants to be switched without // having side effects on each other's option sets $user->setAttribute("slot-original-options-{$id}-{$name}-{$permid}", $this->options, 'apostrophe'); // Refactored to get rid of duplicate logic $allowedVariants = array_keys(aTools::getVariantsForSlotType($this->type, $this->options)); $user->setAttribute("slot-allowed-variants-{$id}-{$name}-{$permid}", $allowedVariants, 'apostrophe'); } $user->setAttribute("slot-options-{$id}-{$name}-{$permid}", $this->options, 'apostrophe'); } // Calling getEffectiveVariant ensures we default to the behavior of the first one // defined, or the first one allowed if there is an allowed_variants option $variant = $this->slot->getEffectiveVariant($this->options); if ($variant) { // Allow slot variants to adjust slot options. This shouldn't be used to radically // change the slot, just as an adjunct to CSS, styling things in ways CSS can't $variants = aTools::getVariantsForSlotType($this->slot->type, $this->options); if (isset($variants[$variant]['options'])) { $options = $variants[$variant]['options']; $this->options = array_merge($this->options, $options); } } $this->pageid = $this->page->id; $this->id = $this->pageid . '-' . $this->name . '-' . $this->permid; // The basic slot types, and some custom slot types, are // simplified by having this field ready to go $this->value = $this->slot->value; // Not everyone wants the default 'double click the outline to // start editing' behavior $this->outlineEditable = $this->editable && $this->getOption('outline_editable', $this->slot->isOutlineEditable()); // Useful if you're reimplementing that via a button etc $id = $this->id; $this->showEditorJS = "\$('#content-{$id}').hide(); \$('#form-{$id}').fadeIn();"; if (isset($this->validationData['form'])) { // Make Symfony 1.2 form validation extra-convenient $this->form = $this->validationData['form']; } }