/**
  * Standard SS method
  * @return FieldSet
  */
 function getCMSFields()
 {
     $fields = parent::getCMSFields();
     $fields->addFieldToTab("Root.Images", new CheckboxField("HasImages", _t("ProductQuestion.HAS_IMAGES", "This question makes use of images")));
     if (!$this->HasImages) {
         $fields->addFieldToTab("Root.DefaultFormField", new OptionSetField("DefaultFormField", _t("ProductQuestion.DEFAULTFORMFIELD", "Field type to use"), $this->Options ? $this->Config()->get("available_form_fields_list") : $this->Config()->get("available_form_fields_free")));
     } else {
         $fields->removeFieldFromTab("Root.Main", "DefaultFormField");
     }
     $productFieldTitle = _t("ProductQuestion.PRODUCTS", "Products showing this question");
     if ($this->ApplyToAllProducts) {
         $productField = new HiddenField("ProductsToIgnore");
     } elseif (Product::get()->count() < $this->Config()->get("max_products_for_checkbox_set_field")) {
         $productField = new CheckboxSetField("Products", $productFieldTitle, Product::get()->map("ID", "FullName")->toArray());
     } else {
         $productField = new GridField('Products', _t("ProductQuestion.PRODUCTS", "Products showing this question"), $this->Products(), GridFieldEditOriginalPageConfigWithDelete::create());
     }
     $fields->addFieldToTab("Root.Products", $productField);
     if (!$this->ApplyToAllProducts) {
         foreach ($this->Products() as $product) {
             $fields->addFieldToTab("Root.Products", new LiteralField("Product" . $product->ID, "<h5><a href=\"" . $product->CMSEditLink() . "\">" . _t("ProductQuestion.BACK_TO", "Edit ") . " " . $product->Title . "</a></h5>"));
         }
     }
     if ($this->HasImages) {
         $folders = Folder::get();
         if ($folders->count()) {
             $folderMap = $folders->map("ID", "Title")->toArray();
             $folders = null;
             $fields->removeFieldFromTab("Root.Main", "Folder");
             $fields->addFieldToTab("Root.Images", $treeDropdownField = new TreeDropdownField("FolderID", _t("ProductQuestion.FOLDER", "Folder"), "Folder"));
             $treeDropdownFieldRightTitle = _t("ProductQuestion.FOLDER_ID", "Select the folder in which the images live.\n\t\t\t\t\t<br />\n\t\t\t\t\t<strong>\n\t\t\t\t\t\tThe images need to have the exact same file name as the options listed.\n\t\t\t\t\t\tFor example, if one of your options is 'red' then there should be a file in your folder called 'red.png' or 'red.jpg' or 'red.gif',\n\t\t\t\t\t\tthe following filenames would not work: 'Red.png', 'red1.jpg', 'RED.gif', etc...\n\t\t\t\t\t</strong>");
             $folder = Folder::get()->byID($this->FolderID);
             if ($folder) {
                 $treeDropdownFieldRightTitle .= "\n\t\t\t\t\t\t<br /><a href=\"admin/assets/show/" . $folder->ID . "/\">" . _t("ProductQuestion.OPEN", "Open") . " " . $folder->Title . "</a>\n\t\t\t\t\t\t<br /><a href=\"admin/assets/add/?ID" . $folder->ID . "/\">" . _t("ProductQuestion.ADD_IMAGES_TO", "Add images to") . " " . $folder->Title . "</a>";
             }
             $treeDropdownField->setRightTitle($treeDropdownFieldRightTitle);
         }
         if ($this->FolderID) {
             $imagesInFolder = Image::get()->filter(array("ParentID" => $this->FolderID));
             if ($imagesInFolder->count()) {
                 $imagesInFolderArray = $imagesInFolder->map("ID", "Name")->toArray();
                 $options = explode(",", $this->Options);
                 $imagesInFolderField = new ReadonlyField("ImagesInFolder", _t("ProductQuestion.NO_IMAGES", "Images in folder"), implode("<br />", $imagesInFolderArray));
                 $imagesInFolderField->dontEscape = true;
                 $fields->addFieldToTab("Root.Images", $imagesInFolderField);
                 //matches
                 if ($this->exists()) {
                     $matchesInFolderArray = array();
                     $nonMatchesInFolderArray = array();
                     $options = explode(",", $this->Options);
                     if (count($options)) {
                         foreach ($options as $option) {
                             $fileNameArray = self::create_file_array_from_option($option);
                             foreach ($fileNameArray as $fileName) {
                                 if (in_array($fileName, $imagesInFolderArray)) {
                                     $matchesInFolderArray[$option] = "<strong>" . $option . "</strong>: " . $fileName;
                                 }
                             }
                             if (!isset($matchesInFolderArray[$option])) {
                                 $nonMatchesInFolderArray[$option] = "<strong>" . $option . " - add one these files: </strong>" . implode(",", $fileNameArray);
                             }
                         }
                     }
                     $matchesInFolderField = new ReadonlyField("MatchesInFolder", _t("ProductQuestion.MATCHES_IN_FOLDER", "Matches in folder"), implode("<br />", $matchesInFolderArray));
                     $matchesInFolderField->dontEscape = true;
                     $fields->addFieldToTab("Root.Images", $matchesInFolderField);
                     $nonMatchesInFolderField = new ReadonlyField("NonMatchesInFolder", _t("ProductQuestion.NON_MATCHES_IN_FOLDER", "NON Matches in folder"), implode("<br />", $nonMatchesInFolderArray));
                     $nonMatchesInFolderField->dontEscape = true;
                     $fields->addFieldToTab("Root.Images", $nonMatchesInFolderField);
                 }
             } else {
                 $imagesInFolderField = new ReadonlyField("ImagesInFolder", "Images in folder", _t("ProductQuestion.NO_IMAGES", "There are no images in this folder"));
                 $fields->addFieldToTab("Root.Main", $imagesInFolderField);
             }
         }
     } else {
         $fields->removeByName("Folder");
     }
     if ($this->Products()->count()) {
         $randomProduct = $this->Products()->First();
         $fields->addFieldToTab("Root.Example", $this->getFieldForProduct($randomProduct));
     }
     $folderExplanation = _t("ProductQuestion.FOLDER_EXPLANATION", "Tick the box to link each option to an image (e.g. useful if you have colour swatches).\n\t\t\t\tOnce ticked and the question is saved you will be able to select a folder from where to select the images.");
     $field = $fields->dataFieldByName("HasImages");
     if ($field) {
         $field->setDescription($folderExplanation);
     }
     $this->extend('updateCMSFields', $fields);
     //custom field labels
     $internalCodeField = $fields->dataFieldByName("InternalCode");
     $internalCodeField->setRightTitle(_t("ProductQuestion.INTERNALCODE", "Code used to identify question (not shown to customers)"));
     $questionField = $fields->dataFieldByName("Question");
     $questionField->setRightTitle(_t("ProductQuestion.QUESTION", "Question (e.g. what configuration do you prefer?)"));
     $labelField = $fields->dataFieldByName("Label");
     $labelField->setRightTitle(_t("ProductQuestion.LABEL", "Label (e.g. Your selected configuration)"));
     $defaultAnswerField = $fields->dataFieldByName("DefaultAnswer");
     $defaultAnswerField->setRightTitle(_t("ProductQuestion.DEFAULT_ANSWER", "Default Answer if no Answer has been provided.  Can be blank or, for example, tba."));
     $optionsField = $fields->dataFieldByName("Options");
     $optionsField->setRightTitle(_t("ProductQuestion.OPTIONS", "Predefined Options (leave blank for any option).  These must be comma separated (e.g. red, blue, yellow, orange)"));
     return $fields;
 }
 public static function assetsFolderField($obj, $dirExists)
 {
     $field = null;
     $msg = null;
     if ($dirExists) {
         //Message
         $defaultMsg = '<em>Files uploaded via the content area will be uploaded to</em>' . '<br /> <strong>' . Upload::config()->uploads_folder . '</strong>';
         if (method_exists($obj, 'getMessageUploadDirectory')) {
             $msg = $obj->getMessageUploadDirectory();
         }
         if (!$msg) {
             $msg = $defaultMsg;
         }
         //TODO these could also be global settings
         $manageAble = true;
         $editable = true;
         //As this is happening from the subsites administration, when editing a subsite
         //you'd probably be on another site, and hence can't access the site's files anyway
         if ($obj->ClassName == 'Subsite') {
             $manageAble = false;
             $editable = false;
         }
         if ($editable) {
             //Asset folder is editable
             $field1 = new TreeDropdownField('AssetsFolderID', 'Change Directory:', 'Folder');
             $field1->setRightTitle('Directory changes take place after saving.');
             //Dropdown field style adjustments
             //TODO move this to an external stylesheet as these styles don't kick in on AJAX loads
             Requirements::customCSS('
                 #Form_EditForm_UploadDirRulesNotes .ui-accordion-content {
                     overflow: visible;
                 }
                 #TreeDropdownField_Form_EditForm_AssetsFolderID {
                     min-width: 260px;
                 }
                 .UploadDirectoryFields .fieldgroup label {
                     padding: 0 0 4px;
                 }
             ');
             $dir = $obj->AssetsFolder();
             $filescount = File::get()->filter(array('ParentID' => $dir->ID))->count();
             $manageButton = null;
             if ($manageAble) {
                 $manageButton = "<a href='/admin/assets/show/" . $dir->ID . "' class='ss-ui-button ss-ui-button-small ui-button'>\n                        Manage Files (" . $filescount . ')</a>';
             }
             $field2 = new LiteralField('UploadDirRulesNote', "<div style='margin-bottom:10px;margin-right:16px;'>{$msg}</div>" . $manageButton);
             $field = new FieldGroup(array($field2, $field1));
             $field->setTitle('Upload Directory');
             $field->addExtraClass('UploadDirectoryFields');
             $field->setName('UploadDirectoryFields');
         } else {
             //Asset folder is not editable
             $field = new LiteralField('UploadDirRulesNote', '
                 <div class="field text" id="UploadDirRulesNote">
                     <label class="left">Upload Directory</label>
                     <div class="middleColumn">
                         <p style="margin-bottom: 0; padding-top: 0px;">
                             ' . $msg . '
                             <br />
                             <em>If you need to edit or change this folder, please contact your administrator.</em>
                         </p>
                     </div>
                 </div>
                 ');
         }
     } else {
         //Message
         $defaultMsg = 'Please <strong>choose a name and save</strong> for adding content.';
         if (method_exists($obj, 'getMessageSaveFirst')) {
             $msg = $obj->getMessageSaveFirst();
         }
         if (!$msg) {
             $msg = $defaultMsg;
         }
         //preview calculated assets folder
         //$msg = $msg . ' (' . $this->owner->getCalcAssetsFolderDirectory() . ')';
         $field = new LiteralField('UploadDirRulesNote', '
             <p class="message notice" >' . $msg . '</p>
         ');
     }
     return $field;
 }
 public function updateCMSFields(FieldList $fields)
 {
     if ($this->classHasAdvertisements($this->owner->ClassName)) {
         $tabName = $this->MyTabName();
         //advertisements shown...
         $where = '1 = 1';
         if ($this->owner->AdvertisementsFolderID) {
             $images = Image::get()->filter("ParentID", $this->owner->AdvertisementsFolderID);
             if ($images->count()) {
                 $where = "\"AdvertisementImageID\" IN (" . implode(",", $images->column("ID")) . ")";
             } else {
                 $where = " 1 = 2";
             }
         }
         //$advertisementsCount = DB::query("SELECT COUNT(ID) FROM \"Advertisement\" $whereDB ;")->value();
         $advertisements = $this->owner->Advertisements()->where($where);
         $txt = sprintf(_t("AdvertisementDecorator.ACTUAL", 'Current %1$s Shown'), Config::inst()->get("Advertisement", "plural_name"));
         $fields->addFieldToTab($tabName, $this->MyHeaderField($txt));
         $txt = sprintf(_t("AdvertisementDecorator.SELECT", 'Select %1$s to show ... '), Config::inst()->get("Advertisement", "plural_name"));
         $advertisementsGridField = new GridField('Advertisements', $txt, $this->owner->Advertisements(), GridFieldConfig_RelationEditor::create());
         $fields->addFieldToTab($tabName, $advertisementsGridField);
         if (Config::inst()->get("Advertisement", "resize_images") == 'no') {
             $totalSize = 0;
             foreach ($this->owner->Advertisements() as $advertisement) {
                 $totalSize += $advertisement->AdvertisementImage()->getAbsoluteSize();
             }
             $seconds = round(($totalSize + 1) / 524288);
             $fields->addFieldToTab($tabName, new LiteralField("TotalSize", '<p><em>Total download size: ' . File::format_size($totalSize) . ', good reception 3G network download time less than ~' . ($seconds + 1) . ' seconds.</em></p>'));
         }
         if (class_exists("DataObjectSorterController")) {
             $shownAdvertisements = $this->owner->getManyManyComponents('Advertisements');
             if ($shownAdvertisements) {
                 $array = $shownAdvertisements->column("ID");
                 $idString = implode(",", $array);
                 $link = DataObjectSorterController::popup_link("Advertisement", $filterField = "ID", $filterValue = $idString, $linkText = "sort " . Config::inst()->get("Advertisement", "plural_name"), $titleField = "FullTitle");
                 $fields->addFieldToTab($tabName, new LiteralField("AdvertisementsSorter", $link));
             }
         }
         if ($advertisements->count()) {
         } else {
             $txt = sprintf(_t("AdvertisementDecorator.CREATE", '<p>Please <a href="admin/%1$s/">create %2$s</a> on the <a href="admin/%1$s/">%3$s tab</a> first, or see below on how to create %2$s from a folder.</p>'), Config::inst()->get("AdvertisementAdmin", "url_segment"), Config::inst()->get("Advertisement", "plural_name"), Config::inst()->get("AdvertisementAdmin", "menu_title"));
             $fields->addFieldToTab($tabName, new LiteralField("AdvertisementsHowToCreate", $txt));
         }
         if ($parent = $this->advertisementParent()) {
             $txt = sprintf(_t("AdvertisementDecorator.ORUSE", 'OR  ... use %1$s from  <i>%2$s</i>.'), Config::inst()->get("Advertisement", "plural_name"), $parent->Title);
             $fields->addFieldToTab($tabName, new CheckboxField("UseParentAdvertisements", $txt));
         }
         //create new advertisements
         $txt = sprintf(_t("AdvertisementDecorator.CREATE", 'Create new %1$s'), Config::inst()->get("Advertisement", "plural_name"));
         $fields->addFieldToTab($tabName, $this->MyHeaderField($txt));
         $txt = sprintf(_t("AdvertisementDecorator.CREATENEWFROMFOLDER_EXPLANATION", 'Create New %1$s from images in the folder selected - each image in the folder will be used to create a %3$s. %2$s'), Config::inst()->get("Advertisement", "plural_name"), Advertisement::recommended_image_size_statement(), Config::inst()->get("Advertisement", "singular_name"));
         if (Folder::get()->count()) {
             $fields->addFieldToTab($tabName, $treeDropdownField = new TreeDropdownField('AdvertisementsFolderID', _t("AdvertisementDecorator.CREATENEWFROMFOLDER", "Create from folder"), 'Folder'));
             $treeDropdownField->setRightTitle($txt);
         }
         $styles = AdvertisementStyle::get();
         if ($styles->count()) {
             $fields->addFieldToTab($tabName, $this->MyHeaderField("Style"));
             $list = $styles->map("ID", "Title", $emptyString = _t("AdvertisementDecorator.SELECTSTYLE", "--select style--"), $sortByTitle = true);
             $fields->addFieldToTab($tabName, $selectStyleField = new DropdownField("AdvertisementStyleID", _t("AdvertisementDecorator.STYLECREATED", "Select style"), $list));
             $selectStyleField->setRightTitle(_t("AdvertisementDecorator.STYLECREATED_EXPLANATION", "Styles are created by your developer"));
         }
         $txt = sprintf(_t("AdvertisementDecorator.EDIT", 'Edit %1$s'), Config::inst()->get("Advertisement", "plural_name"));
         $fields->addFieldToTab($tabName, $this->MyHeaderField($txt));
         $txt = sprintf(_t("AdvertisementDecorator.PLEASEMANAGEEXISTING", '<p>Please manage existing %1$s on the <a href="admin/%2$s/">%3$s tab</a>.</p>'), Config::inst()->get("Advertisement", "plural_name"), Config::inst()->get("AdvertisementAdmin", "url_segment"), Config::inst()->get("AdvertisementAdmin", "menu_title"));
         $fields->addFieldToTab($tabName, new LiteralField("ManageAdvertisements", $txt));
         $txt = sprintf(_t("AdvertisementDecorator.DELETE", 'Delete %1$s'), Config::inst()->get("Advertisement", "plural_name"));
         $fields->addFieldToTab($tabName, $this->MyHeaderField($txt));
         $page = SiteTree::get()->byID($this->owner->ID);
         $txtRemove = sprintf(_t("AdvertisementDecorator.REMOVE", 'Remove all %1$s from this page (%1$s will not be deleted but are not longer associated with this page)'), Config::inst()->get("Advertisement", "plural_name"));
         $txtConfirmRemove = sprintf(_t("AdvertisementDecorator.CONFIRMREMOVE", 'Are you sure you want to remove all %1$s from this page?'), Config::inst()->get("Advertisement", "plural_name"));
         $removeallLink = 'advertisements/removealladvertisements/' . $this->owner->ID . '/';
         $jquery = 'if(confirm(\'' . $txtConfirmRemove . '\')) {jQuery(\'#removealladvertisements\').load(\'' . $removeallLink . '\');} return false;';
         $fields->addFieldToTab($tabName, new LiteralField("removealladvertisements", '<p class="message warning"><a href="' . $removeallLink . '" onclick="' . $jquery . '"  id="removealladvertisements"  class="ss-ui-button">' . $txtRemove . '</a></p>'));
         $txtDelete = sprintf(_t("AdvertisementDecorator.DELETE", 'Delete all %1$s from this website (but not the images associated with them)'), Config::inst()->get("Advertisement", "plural_name"));
         $txtConfirmDelete = sprintf(_t("AdvertisementDecorator.CONFIRMDELETE", 'Are you sure you want to delete all %1$s - there is no UNDO?'), Config::inst()->get("Advertisement", "plural_name"));
         $deleteallLink = 'advertisements/deletealladvertisements/' . $this->owner->ID . '/';
         $jquery = 'if(confirm(\'' . $txtConfirmDelete . '\')) {jQuery(\'#deletealladvertisements\').load(\'' . $deleteallLink . '\');} return false;';
         $fields->addFieldToTab($tabName, new LiteralField("deletealladvertisements", '<p class="message bad"><a href="' . $deleteallLink . '" onclick="' . $jquery . '"  id="deletealladvertisements" class="ss-ui-button">' . $txtDelete . '</a></p>'));
     }
     return $fields;
 }