/** * Get uploader code * * @param string $fieldKey uploadFieldKey * @param string $fieldName uploadFieldName * @param string $uploadCallBack upload callback function * @param boolean $allowImageOnly allow only images files * * @return string uploaderCode * @throws \Exception */ protected function getUploaderCode($fieldKey, $fieldName, $uploadCallBack = "uploadFinished", $allowImageOnly = true) { \cmsSession::getInstance(); $cx = \Cx\Core\Core\Controller\Cx::instanciate(); try { $uploader = new \Cx\Core_Modules\Uploader\Model\Entity\Uploader(); $uploaderId = $uploader->getId(); $uploadOptions = array('id' => 'calendarUploader_' . $uploaderId, 'style' => 'display: none'); if ($allowImageOnly) { $uploadOptions['allowed-extensions'] = array('gif', 'jpg', 'png', 'jpeg'); } $uploader->setCallback($fieldName . 'JsCallback'); $uploader->setUploadLimit(1); $uploader->setOptions($uploadOptions); $uploader->setFinishedCallback(array($cx->getCodeBaseModulePath() . '/Calendar/Controller/Calendar.class.php', '\\Cx\\Modules\\Calendar\\Controller\\Calendar', $uploadCallBack)); $folderWidget = new \Cx\Core_Modules\MediaBrowser\Model\Entity\FolderWidget($_SESSION->getTempPath() . '/' . $uploaderId); $this->_objTpl->setVariable(array(strtoupper($fieldName) . '_WIDGET_CODE' => $folderWidget->getXHtml(), "{$this->moduleLangVar}_" . strtoupper($fieldKey) => $uploaderId)); $strJs = <<<JAVASCRIPT {$uploader->getXHtml()} <script type="text/javascript"> cx.ready(function() { //called if user clicks on the field jQuery('#{$fieldName}').bind('click', function() { jQuery('#calendarUploader_{$uploaderId}').trigger('click'); return false; }); }); //uploader javascript callback function function {$fieldName}JsCallback(callback) { angular.element('#mediaBrowserfolderWidget_{$folderWidget->getId()}').scope().refreshBrowser(); } </script> JAVASCRIPT; return $strJs; } catch (\Exception $e) { \DBG::msg('<!-- failed initializing uploader -->'); throw new \Exception("failed initializing uploader"); } }
/** * Inits the uploader when displaying a contact form. */ protected function initUploader($fieldId, $restrictUpload2SingleFile = true) { try { $cx = \Cx\Core\Core\Controller\Cx::instanciate(); $sessionObj = $cx->getComponent('Session')->getSession(); $uploader = new \Cx\Core_Modules\Uploader\Model\Entity\Uploader(); //set instance name so we are able to catch the instance with js $uploader->setCallback('contactFormUploader_' . $fieldId); $cx = \Cx\Core\Core\Controller\Cx::instanciate(); // specifies the function to call when upload is finished. must be a static function $uploader->setFinishedCallback(array($cx->getCodeBaseCoreModulePath() . '/Contact/Controller/Contact.class.php', '\\Cx\\Core_Modules\\Contact\\Controller\\Contact', 'uploadFinished')); if ($restrictUpload2SingleFile) { $uploader->setData(array('singleFile' => $restrictUpload2SingleFile)); $uploader->setUploadLimit(1); } $uploaderId = $uploader->getId(); $uploader->setOptions(array('id' => 'contactUploader_' . $uploaderId, 'style' => 'display: none')); //initialize the widget displaying the folder contents $folderWidget = new \Cx\Core_Modules\MediaBrowser\Model\Entity\FolderWidget($_SESSION->getTempPath() . '/' . $uploaderId); $this->objTemplate->setVariable(array('CONTACT_UPLOADER_FOLDER_WIDGET_' . $fieldId => $folderWidget->getXhtml(), 'CONTACT_UPLOADER_ID_' . $fieldId => $uploaderId)); $folderWidgetId = $folderWidget->getId(); $strInputfield = $uploader->getXHtml(); $strInputfield .= <<<CODE <script type="text/javascript"> cx.ready(function() { jQuery('#contactFormFieldId_{$fieldId}').bind('click', function() { jQuery('#contactUploader_{$uploaderId}').trigger('click'); return false; }).removeAttr('disabled'); }); //uploader javascript callback function function contactFormUploader_{$fieldId}(callback) { angular.element('#mediaBrowserfolderWidget_{$folderWidgetId}').scope().refreshBrowser(); } </script> CODE; return $strInputfield; } catch (Exception $e) { return '<!-- failed initializing uploader, exception ' . get_class($e) . ' with message "' . $e->getMessage() . '" -->'; } }