Source of file AsmselectField.php
Size: 3,874 Bytes - Last Modified: 2021-12-24T06:34:40+00:00
/var/www/docs.ssmods.com/process/src/code/AsmselectField.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 | <?php /** * AsmselectField is a SilverStripe implementation of jquery-asmselect * * http://code.google.com/p/jquery-asmselect/ * * @author Jeremy Shipman http://www.burnbright.co.nz * * Example usage: * * $members = DataObject::get('ID','Title'); * $asmfield = new AsmselectField('SpecialMembers','Special Members',$members); * */ class AsmselectField extends DropdownField{ protected $rollbacksize = 10; //size of multiple select filed if js is broken protected $usejavascript = true; public $dontEscape = true; protected $reserveNL = false; function __construct($name, $title = null, $source = array(), $values = array(), $form = null) { parent::__construct($name, $title, $source, $values , $form, null); } function Field($properties = array()) { if($this->usejavascript){ Requirements::css(ASMSELECTFIELD_DIR.'/css/jquery.asmselect.basic.css'); //can be included seperately if desired Requirements::javascript(THIRDPARTY_DIR.'/jquery/jquery.js'); Requirements::javascript(THIRDPARTY_DIR.'/jquery-livequery/jquery.livequery.js'); Requirements::javascript(ASMSELECTFIELD_DIR.'/javascript/jquery.asmselect.js'); $id = $this->id(); $removelabel = 'remove'; //TODO: allow to be custom //TODO: provide more customisation options Requirements::javascript(ASMSELECTFIELD_DIR.'/javascript/asmselectfield.js'); } $options = ''; $source = $this->getSource(); $values = $this->value; // Get values from the join, if available if(is_object($this->form)) { $record = $this->form->getRecord(); if(!$values && $record && $record->hasMethod($this->name)) { $funcName = $this->name; $join = $record->$funcName(); if($join) { foreach($join as $joinItem) { $values[] = $joinItem->ID; } } } } if($source) { // For SQLMap sources, the empty string needs to be added specially if(is_object($source) && $this->emptyString) { $options .= $this->createTag('option', array('value' => ''), $this->emptyString); } foreach($source as $value => $title) { // Blank value of field and source (e.g. "" => "(Any)") if($value === '' && ($values === '' || $values === null)) { $selected = 'selected'; } else { // Normal value from the source $selected = null; if(count($values) > 0){ $selected = (in_array($value,$values)) ? 'selected' : null; $this->isSelected = ($selected) ? true : false; } } $options .= $this->createTag( 'option', array( 'selected' => $selected, 'value' => $value ), $title ); } } $attributes = array( 'class' => ($this->extraClass() ? $this->extraClass()." asmselectfield" : 'asmselectfield'), 'id' => $this->id(), 'name' => $this->name."[]", 'tabindex' => $this->getAttribute('tabindex'), 'multiple' => 'multiple', 'size' => $this->rollbacksize ); if($this->disabled) $attributes['disabled'] = 'disabled'; return $this->createTag('select', $attributes, $options); } function saveInto(DataObjectInterface $record) { $fieldName = $this->name; $saveDest = $record->$fieldName(); if(! $saveDest) user_error("AsmselectField::saveInto() Field '$fieldName' not found on $record->class.$record->ID", E_USER_ERROR); if(is_array($this->value)){ //hack to make the field work in the CMS (likely cms 2.4 js related) if(Director::is_ajax() && Controller::curr() instanceof LeftAndMain) $this->value = explode(",",$this->value[0]); $saveDest->setByIDList($this->value); } } function setListSize($size){ $this->rollbacksize = $size; } /** * If you don't want to use the Asmselect functionality */ function disableJavascript(){ $this->usejavascript = false; } } |