protected function processRecord($record, $columnMap, &$results, $preview = false) { $class = $this->objectClass; $title = trim($record['Title']); $item = trim($record['ListItem']); $existingList = DataList::get_data_list($title); if (!$existingList) { $existingList = new DataList(); $existingList->Title = $title; $existingList->write(); } // now add the item to that list $existingItem = DataObject::get_one('DataListItem', '"Title"=\'' . Convert::raw2sql($item) . '\' AND "ListID" = ' . (int) $existingList->ID); if (!$existingItem) { $existingItem = new DataListItem(); $existingItem->Title = $item; $existingItem->ListID = $existingList->ID; $existingItem->write(); } }
function getFormField() { $sourceList = $this->getSetting('SourceList') ? $this->getSetting('SourceList') : null; // first off lets go and output all the options we need $fields = $this->Parent()->Fields(); $source = null; foreach ($fields as $field) { if ($field->Name == $sourceList) { $source = $field; break; } } $optionLists = array(); if ($source) { // all our potential lists come from the source list's data list source, so we need to go load that // first, then iterate it and build all the additional required lists $sourceList = DataList::get_data_list($source->getSetting('ListTitle')); if ($sourceList) { $items = $sourceList->Items(); // now lets create a bunch of option fields foreach ($items as $sourceItem) { // now get the data list that is represented by this one $list = DataList::get_data_list($sourceItem->Title); if ($list) { // add its items to this lists' list of items $listItems = $list->Items()->map('Title', 'Title'); $optionLists[$sourceItem->Title] = $listItems; } } } $me = new DropdownField($this->Name, $this->Title, array()); if (count($optionLists)) { $me = new DependentDropdownField($this->Name, $this->Title, $optionLists, $source->Name); } return $me; } // return a new list return new LiteralField($this->Name); }
public function Field() { $dependScript = ''; // lets find out if we've got an existing selection in our dependon list if ($this->form) { $dependent = $this->form->Fields()->dataFieldByName($this->dependentOn); if ($dependent && $dependent->Value()) { $dependScript = "showList('" . Convert::raw2js($dependent->Value()) . "', '" . Convert::raw2js($this->value) . "');"; } } $dependentName = $this->dependentOn; if (strpos($dependentName, '.')) { $dependentName = substr($dependentName, strrpos($dependentName, '.') + 1); } $listItems = array(); if (is_string($this->dependentLists)) { $list = DataList::get_data_list($this->dependentLists); if ($list) { $this->dependentLists = $list->Items()->map('Title', 'Title'); } } foreach ($this->dependentLists as $listTitle) { $list = DataList::get_data_list($listTitle); if ($list) { $listItems[$listTitle] = $list->Items()->map('Title', 'Title'); } } $jsonStruct = Convert::raw2json($listItems); $jscript = <<<JSCRIPT (function (\$) { \t\$().ready(function () { \t\tvar listOptions = {$jsonStruct}; \t\tvar me = \$('select[name={$this->name}]'); \t\t/** \t\t * Shows the specified list when needed \t\t */ \t\tvar showList = function(name, value) { \t\t\t// need to create all the options \t\t\tif (listOptions[name]) { \t\t\t\tfor (var k in listOptions[name]) { \t\t\t\t\tvar sel = ''; \t\t\t\t\tif (k == value) { \t\t\t\t\t\tsel = ' selected="selected"'; \t\t\t\t\t} \t\t\t\t\tme.append('<option val="' + k + '"' + sel + '>' + k + '</option>'); \t\t\t\t} \t\t\t} \t\t} \t\t\$('select[name={$dependentName}]').change(function () { \t\t\t// when this list changes, make sure to update the contained list items \t\t\tvar _this = \$(this); \t\t\tme.empty(); \t\t\tif (_this.val()) { \t\t\t\tshowList(_this.val()); \t\t\t} \t\t}); \t\t{$dependScript} \t}); })(jQuery); JSCRIPT; Requirements::customScript($jscript, $this->name . 'dropdown'); return parent::Field(); }