Source of file SiteTreeListboxField.php
Size: 4,272 Bytes - Last Modified: 2021-12-23T10:46:37+00:00
/var/www/docs.ssmods.com/process/src/code/forms/SiteTreeListboxField.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 | <?php class SiteTreeListboxField extends ListboxField { /** * * @var Int */ protected $siteTreeParentID = 0; /** * * @var Array */ protected $arrayOfAllowedIDs = array(); /** * * @var array */ protected $classNamesForItems = array("SiteTree"); /** * * @var Array */ protected $siteTreeParentAllChildren = array(); /** * Creates a new dropdown field. * * @param string $name The field name * @param string $title The field title * @param array $source An map of the dropdown items * @param string|array $value You can pass an array of values or a single value like a drop down to be selected * @param int $size Optional size of the select element * @param form The parent form */ public function __construct($name, $title = '', $source = array(), $value = '', $size = null) { parent::__construct($name, $title, $source, $value, $size, true); } /** * * @param Int $id */ public function setSiteTreeParentID($id) { $this->siteTreeParentID = $id; } /** * * @param Int $id * @param Int | string $array */ public function setSiteTreeParentAndChildClassNames($id, $array) { $this->siteTreeParentID = $id; $this->setClassNamesForItems($array); } /** * * @param Array | str $array */ public function setClassNamesForItems($array) { unset($this->classNamesForItems); if (is_array($array)) { $this->classNamesForItems = $array; } else { $this->classNamesForItems = array($array); } } /** * * @param String $str */ public function addClassNameForItems($str) { $this->classNamesForItems[] = $str; } /** * * @param Array | SS_Map */ public function setSource($source) { if ($source instanceof SS_Map) { $source = $source->toArray(); } if ($source) { $hasCommas = array_filter( array_keys($source), create_function('$key', 'return strpos($key, ",") !== FALSE;') ); if ($hasCommas) { throw new InvalidArgumentException('No commas allowed in $source keys'); } } parent::setSource($source); return $this; } /** * * @return Array */ public function getSource() { $source = parent::getSource(); //debug::log("original source count ".implode($this->classNamesForItems)." ".$this->siteTreeParentID.": ".count($source)); if ($this->siteTreeParentID) { $arrayItems = array(); $allChildrenForSiteTreeParent = $this->getAllChildrenForSiteTreeParent($this->siteTreeParentID); //debug::log("new source count: ".count($allChildrenForSiteTreeParent)); $finalSource = array(); foreach ($source as $sourceKey => $sourceValue) { if (isset($allChildrenForSiteTreeParent[$sourceKey])) { $finalSource[$sourceKey] = $sourceValue; } } } else { $finalSource = $source; } //debug::log("final source count: ".count($finalSource)); return $finalSource; } /** * @param Int * @return Array */ private function getAllChildrenForSiteTreeParent($parentID) { $children = SiteTree::get()->filter(array("ParentID" => $parentID)); if ($children && $children->count()) { foreach ($children as $child) { foreach ($this->classNamesForItems as $matchingCLassName) { //debug::log("has child"); if ($child instanceof $matchingCLassName) { //debug::log("we now have ".count($this->siteTreeParentAllChildren)." children"); $this->siteTreeParentAllChildren[$child->ID] = $child->ID; } } $this->getAllChildrenForSiteTreeParent($child->ID); } } return $this->siteTreeParentAllChildren; } } |