Source of file BulkUploader.php
Size: 9,847 Bytes - Last Modified: 2021-12-24T06:41:11+00:00
/var/www/docs.ssmods.com/process/src/src/BulkUploader/BulkUploader.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342 | <?php namespace Colymba\BulkUpload; use SilverStripe\ORM\DataObject; use SilverStripe\View\ArrayData; use SilverStripe\Dev\Deprecation; use SilverStripe\Forms\FormAction; use SilverStripe\View\Requirements; use SilverStripe\Control\Controller; use SilverStripe\Core\Config\Config; use Colymba\BulkUpload\BulkUploadField; use Colymba\BulkUpload\BulkUploadHandler; use SilverStripe\Forms\GridField\GridField_URLHandler; use SilverStripe\Forms\GridField\GridField_HTMLProvider; /** * GridField component for uploading images in bulk. * * @author colymba */ class BulkUploader implements GridField_HTMLProvider, GridField_URLHandler { /** * Component configuration. * * 'fileRelationName' => field name of the $has_one File/Image relation * 'recordClassName' => overrides the automatic DataObject class detection from gridfield->dataClass with a custom class name * * @var array */ protected $config = array( 'fileRelationName' => null, 'recordClassName' => null ); /** * If true, the component will Publish Versioned DataObject * if fasle they will be left as draft. * @var boolean */ protected $autoPublishDataObject = false; /** * UploadField setup function calls. * List of setup functions to call on {@link UploadField} with the value to pass. * * e.g. array('setFolderName' => 'bulkUpload') will result in: * $uploadField->setFolderName('bulkUpload') * * @var array */ protected $ufSetup = array( 'setFolderName' => 'bulkUpload', ); /** * Component constructor. * * @param string $fileRelationName * @param string $recordClassName */ public function __construct($fileRelationName = null, $recordClassName = null, $autoPublish = false) { if ($fileRelationName != null) { $this->setConfig('fileRelationName', $fileRelationName); } if ($recordClassName != null) { $this->setConfig('recordClassName', $recordClassName); } $this->setAutoPublishDataObject($autoPublish); } /* ********************************************************************** * Components settings and custom methodes * */ /** * Set a component configuration parameter. * * @param string $reference * @param mixed $value */ public function setConfig($reference, $value) { if (!array_key_exists($reference, $this->config)) { user_error("Unknown option reference: $reference", E_USER_ERROR); } $this->config[$reference] = $value; return $this; } /** * Set Versioned DataObject auto publish config * @param boolean $autoPublish True to auto publish versioned dataobjects */ public function setAutoPublishDataObject($autoPublish) { $this->autoPublishDataObject = $autoPublish; return $this; } /** * Get Versioned DataObject auto publish config * @return boolean auto publish config value */ public function getAutoPublishDataObject() { return $this->autoPublishDataObject; } /** * Set an UploadField setup function call. * * @param string $function * @param mixed $param */ public function setUfSetup($function, $param) { $this->ufSetup[$function] = $param; return $this; } /** * Returns one $config reference or the full $config. * * @param string $reference $congif parameter to return * * @return mixed */ public function getConfig($reference = false) { if ($reference) { return $this->config[$reference]; } else { return $this->config; } } /** * Returns one $ufSetup reference or the full config. * * @param string $reference $ufSetup parameter to return * * @return mixed */ public function getUfSetup($reference = false) { if ($reference) { return $this->ufSetup[$reference]; } else { return $this->ufSetup; } } /** * Returns the class name of container `DataObject` record. * Either as set in the component config or from the `Gridfield` `dataClass`. * * @return string */ public function getRecordClassName($gridField) { return $this->getConfig('recordClassName') ? $this->getConfig('recordClassName') : $gridField->list->dataClass; } /** * Get the first has_one Image/File relation from the GridField managed DataObject * i.e. 'MyImage' => 'Image' will return 'MyImage'. * * @param GridField $gridField * @return string Name of the $has_one relation */ public function getDefaultFileRelationName($gridField) { $recordClass = $this->getRecordClassName($gridField); $hasOneFields = Config::inst()->get($recordClass, 'has_one'); $imageField = null; foreach ($hasOneFields as $field => $type) { if ($type === 'SilverStripe\\Assets\\Image' || $type === 'SilverStripe\\Assets\\File' || is_subclass_of($type, 'SilverStripe\\Assets\\File') ) { $imageField = $field; break; } } return $imageField; } /** * Returns the name of the Image/File field name from the managed record * Either as set in the component config or the default one. * * @param GridField $gridField * @return string */ public function getFileRelationName($gridField) { $configFileRelationName = $this->getConfig('fileRelationName'); return $configFileRelationName ? $configFileRelationName : $this->getDefaultFileRelationName($gridField); } /** * Return the ClassName of the fileRelation * i.e. 'MyImage' => 'Image' will return 'Image' * i.e. 'MyImage' => 'File' will return 'File'. * * @param GridField $gridField * @return string file relation className */ public function getFileRelationClassName($gridField) { $recordClass = $this->getRecordClassName($gridField); $hasOneFields = Config::inst()->get($recordClass, 'has_one'); $fieldName = $this->getFileRelationName($gridField); if ($fieldName) { return $hasOneFields[$fieldName]; } else { return 'SilverStripe\\Assets\\File'; } } /** * Returned a configured UploadField instance * embedded in the gridfield heard. * * @param GridField $gridField Current GridField * * @return UploadField Configured UploadField instance */ public function bulkUploadField($gridField) { $fileRelationName = $this->getFileRelationName($gridField); $fieldName = $fileRelationName . '_' . $this->getRecordClassName($gridField) . '_BU'; $uploadField = BulkUploadField::create($gridField, $fieldName, '') ->setForm($gridField->getForm()) ->setRecord(DataObject::create()) // avoid UploadField to get auto-config from the Page (e.g fix allowedMaxFileNumber) ; //UploadField setup foreach ($this->ufSetup as $fn => $param) { $uploadField->{$fn}($param); } $schema['data']['createFileEndpoint'] = [ 'url' => $gridField->Link('bulkupload/upload'), 'method' => 'post', 'payloadFormat' => 'urlencoded', ]; $schema['data']['attachFileEndpoint'] = [ 'url' => $gridField->Link('bulkupload/attach'), 'method' => 'post' ]; $uploadField->setSchemaData($schema); return $uploadField; } /* ********************************************************************** * GridField_HTMLProvider * */ /** * HTML to be embedded into the GridField. * * @param GridField $gridField * * @return array */ public function getHTMLFragments($gridField) { // permission check if (!singleton($gridField->getModelClass())->canEdit()) { return array(); } // get uploadField $uploadField = $this->bulkUploadField($gridField); $data = ArrayData::create(array( 'Colspan' => (count($gridField->getColumns())), 'UploadField' => $uploadField->Field() // call ->Field() to get requirements in right order )); Requirements::javascript('colymba/gridfield-bulk-editing-tools:client/dist/js/main.js'); Requirements::css('colymba/gridfield-bulk-editing-tools:client/dist/styles/main.css'); Requirements::add_i18n_javascript('colymba/gridfield-bulk-editing-tools:client/lang'); return array( 'before' => $data->renderWith('Colymba\\BulkUpload\\BulkUploader'), ); } /* ********************************************************************** * GridField_URLHandler * */ /** * Component URL handlers. * * @param GridField $gridField * * @return array */ public function getURLHandlers($gridField) { return array( 'bulkupload' => 'handleBulkUpload' ); } /** * Pass control over to the RequestHandler. * * @param GridField $gridField * @param HTTPRequest $request * * @return mixed */ public function handleBulkUpload($gridField, $request) { $controller = Controller::curr(); $controller->pushCurrent(); $handler = new \Colymba\BulkUpload\BulkUploadHandler($gridField, $this); return $handler->handleRequest($request); } } |