Source of file WikiPage.php
Size: 27,466 Bytes - Last Modified: 2021-12-23T10:07:55+00:00
/var/www/docs.ssmods.com/process/src/code/pages/WikiPage.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011 | <?php /** * A page type that provides wiki like editing functionality. The * initial goals are * * - automatically create child pages based on [[page name]] style syntax * - be able to edit content via a wysiwyg mechanism * - add links by picking pages from the tree selection control * - add images picking images from the tree selection control * * @author Marcus Nyeholt <marcus@silverstripe.com.au> * @license BSD License (http://silverstripe.org/BSD-License) */ class WikiPage extends Page { private static $db = array( 'EditorType' => "Varchar(32)", // Who was the last editor of the page? 'WikiLastEditor' => 'Varchar(64)', 'WikiLockExpiry' => 'SS_Datetime', ); /** * lock pages for 1 minute at a time by default * * This value is in seconds * * @var int */ private static $lock_time = 60; /** * Set this to true in your mysite/_config.php file to force publishing * as soon as you hit save. Removes the potentially awkward step of * save/done/publish making pages seem to 'disappear'. Consider the * situation where a user creates a new page link (while editing a page in stage), * then publishes the edited page, which takes them to the newly published * page; the page they created hasn't been published, so there's now a * broken link. * * By setting this to true, you more closely mirror the functionality of * other wikis that have save -> live * * @var boolean */ public static $auto_publish = true; /** * Whether or not to allow public users to see the 'edit' button. If not * set, the user must manually know to hit the 'edit' URL, or the assumption * is that there is a separate module managing the login of users on the * frontend of the website. * * @var boolean */ public static $show_edit_button = true; /** * Whether to run the content through HTMLPurify before we display it to users * * @var boolean */ public static $purify_output = false; /** * An array of plugins that allows developers to provide thirdparty field types * * @var array */ protected static $registered_formatters; /** * Register a formatter * * @param SimpleWikiFormatter $formatter * The formatter to register */ public static function register_formatter(SimpleWikiFormatter $formatter) { self::$registered_formatters[$formatter->getFormatterName()] = $formatter; } /** * Before writing, convert any page links to appropriate * new, non-published, pages * * @see sapphire/core/model/SiteTree#onBeforeWrite() */ protected function onBeforeWrite() { parent::onBeforeWrite(); // Changes in 2.4 mean that $this->Content can now become polluted with UTF-8 HTML entitised garbage // we'll leave this legacy conversion in for now? $this->Content = str_replace(' ', '', $this->Content); $formatter = $this->getFormatter(); $formatter->analyseSavedContent($this); // set a lock expiry in the past if there's not one already set if (!$this->WikiLockExpiry) { $this->WikiLockExpiry = date('Y-m-d H:i:s'); } // Make sure to set the last editor to the current user if (Member::currentUser()) { $this->WikiLastEditor = Member::currentUser()->Email; } } /** * Returns whether or not the current user can edit this page * * If the */ public function canEdit($member=null) { $can = parent::canEdit($member); if (!$can) { // see if they can via the wiki permission explicitly $can = Permission::check(EDIT_WIKI); } return $can; } /** * Get the CMS fields * @see sapphire/core/model/SiteTree#getCMSFields() */ public function getCMSFields() { $fields = parent::getCMSFields(); $options = $this->getEditorTypeOptions(); $fields->addFieldToTab('Root.Behaviour', new OptionsetField('EditorType', _t('WikiPage.EDITORTYPE', 'Editor Type'), $options)); // if we're not using the HTML editor type, we should just use a textarea edit field $formatter = $this->getFormatter(); if ($formatter) { $formatter->updateCMSFields($fields); } return $fields; } public function getEditorTypeOptions() { $options = array(); foreach (self::$registered_formatters as $fieldType) { $options[$fieldType->getFormatterName()] = $fieldType->getFormatterName(); } return $options; } /** * Return the editor type to use for this item. Will interrogate * parents if needbe * * @return String */ public function getActualEditorType() { if ($this->EditorType && $this->EditorType != 'Inherit') { return $this->EditorType; } $parent = $this->getParent(); $editorType = 'Wiki'; while ($parent != null && $parent instanceof WikiPage) { if ($parent->EditorType && $parent->EditorType != 'Inherit') { return $parent->EditorType; } $parent = $parent->getParent(); } return 'Wiki'; } /** * Gets the formatter for a given type. If none specified, gets the current formatter * * @return SimpleWikiFormatter */ public function getFormatter($formatter=null) { if (!$formatter) { $formatter = $this->getActualEditorType(); } if (!isset(self::$registered_formatters[$formatter])) { throw new Exception("Formatter $formatter does not exist"); } return self::$registered_formatters[$formatter]; } /** * Retrieves the page's content, passed through any necessary parsing * eg Wiki based content * * @return String */ public function ParsedContent() { $formatter = $this->getFormatter(); $content = $formatter->formatContent($this); // purify the output - we don't want people breaking pages if we set purify=true if (self::$purify_output) { include_once SIMPLEWIKI_DIR . '/thirdparty/htmlpurifier-4.0.0-lite/library/HTMLPurifier.auto.php'; $purifier = new HTMLPurifier(); $content = $purifier->purify($content); $content = preg_replace_callback('/\%5B(.*?)\%5D/', array($this, 'reformatShortcodes'), $content); } return $content; } /** * Reformats shortcodes after being run through htmlpurifier * * @param array $matches */ public function reformatShortcodes($matches) { $val = urldecode($matches[1]); return '[' . $val . ']'; } /** * Get the root of the wiki that this wiki page exists in * * @return WikiPage */ public function getWikiRoot() { $current = $this; $parent = $current->Parent(); while ($parent instanceof WikiPage) { $current = $parent; $parent = $current->Parent(); } return $current; } /** * Lock the page for the current user * * @param Member $member * The user to lock the page for */ public function lock($member = null) { if (!$member) { $member = Member::currentUser(); } // set the updated lock expiry based on now + lock timeout $this->WikiLastEditor = $member->Email; $this->WikiLockExpiry = date('Y-m-d H:i:s', time() + $this->config()->get('lock_time')); // save it with us as the editor $this->write(); } /* * Form for the insert link dialog box */ public function LinkPickerForm() { $fields = FieldList::create( new OptionsetField( $name = "Type", $title = "Link to a", $source = array( "page" => "Page on this site", "file" => "File or image on this site", "external" => "External URL" ), $value = "page" ), new TextField('Link', 'Search by page title'), new TextField('Title', 'Title') ); $actions = FieldList::create( //new FormAction('Submit', 'Submit') ); return new Form($this, "LinkPickerForm", $fields, $actions); } /* * Form for the insert image dialog box */ public function ImagePickerForm() { $fields = FieldList::create( new OptionsetField( $name = "Type", $title = "Image source", $source = array( "new" => "Upload from your computer", "existing" => "Existing image in the file system" ), $value = "new" ), $ff = new FileField('NewImage', 'Upload image'), new TextField('ExistingImage', 'Search by filename'), new TextField('Title', 'Title'), new LiteralField('UploadingIcon', '<div id="uploadingIcon" style="display:none"><img src="simplewiki/images/loading.gif" /></div>') ); $ff->getValidator()->setAllowedMaxFileSize(3145728); // 3mb $actions = FieldList::create( //new FormAction('Submit', 'Submit') ); return new Form($this, "ImagePickerForm", $fields, $actions); } } class WikiPage_Controller extends Page_Controller implements PermissionProvider { private static $allowed_actions = array( 'linkselector', 'edit', 'StatusForm', 'save', 'done', 'publish', 'cancel', 'revert', 'startediting', 'EditForm', 'LinkSelectForm', 'objectdetails', 'CreatePageForm', 'delete', 'addpage', 'updatelock', 'livepreview', 'imagepicker', 'linkpicker', 'linklist', 'imageupload' ); public function init() { parent::init(); Requirements::javascript(THIRDPARTY_DIR . '/jquery/jquery.js'); Requirements::javascript(THIRDPARTY_DIR . '/jquery-entwine/dist/jquery.entwine-dist.js'); Requirements::javascript('simplewiki/javascript/simplewiki.js'); Requirements::javascript('simplewiki/javascript/jquery-autogrow-textarea.js'); Requirements::css('simplewiki/css/simplewiki.css'); } /** * Define some permissions used for editing wiki pages * * @return array */ public function providePermissions() { return array( EDIT_WIKI => array( 'name' => _t('WikiPage.PERM_EDIT', 'Edit Wiki Pages'), 'category' => _t('WikiPage.WIKI_CATEGORY', 'Wiki'), 'sort' => -100, 'help' => _t('WikiPage.PERM_EDIT_HELP', 'Allows users to edit wiki pages') ), MANAGE_WIKI_PAGES => array( 'name' => _t('WikiPage.MANAGE_PAGES', 'Manage Wiki pages'), 'category' => _t('WikiPage.WIKI_CATEGORY', 'Wiki'), 'sort' => -100, 'help' => _t('WikiPage.CREATE_PAGES_HELP', 'Display controls that allow users to create and delete aribtrary pages from the Wiki editing UI') ), ); } /** * The form we're editing with * * @var Form */ protected $form; /** * Action handler for editing this wiki page * * Creates a form that's used for editing the page's content, * as well as adding in a couple of additional toolbar actions * for adding a simple link and a simple image */ public function edit() { HtmlEditorField::include_js(); // Requirements::javascript('simplewiki/javascript/sslinks/editor_plugin_src.js'); $existing = $this->getEditingLocks($this->data(), true); // oops, we've somehow got here even though we shouldn't have if ($existing && $existing['user'] != Member::currentUser()->Email) { return $this->redirect($this->data()->Link()); } if (!$this->data()->canEdit()) { return Security::permissionFailure($this); } $this->form = $this->EditForm(); // check who's editing and whether or not we should bail out return $this->renderWith(array('WikiPage', 'Page')); } /** * Creates the form used for editing the page's content * * @return Form */ public function EditForm() { // make sure to load fresh from db $record = DataObject::get_by_id('WikiPage', $this->data()->ID); $formatter = $record->getFormatter(); $editorField = $formatter->getEditingField($record); $helpLink = $formatter->getHelpUrl(); $fields = FieldList::create( new LiteralField('Preview', '<div data-url="'.$this->Link('livepreview').'" id="editorPreview"></div>'), new LiteralField('DialogContent', '<div id="dialogContent" style="display:none;"></div>'), $editorField, new DropdownField('EditorType', _t('WikiPage.EDITORTYPE', 'Editor Type'), $this->data()->getEditorTypeOptions()), new HiddenField('LockUpdate', '', $this->data()->Link('updatelock')), new HiddenField('LockLength', '', $this->config()->get('lock_time') - 10) ); if ($helpLink) { $fields->push(new LiteralField('HelpLink', '<a target="_blank" href="' . $helpLink . '">' . _t('WikiPage.EDITOR_HELP_LINK', 'Editor Help') . '</a>')); } $actions = null; if (!WikiPage::$auto_publish) { $actions = FieldList::create( new FormAction('save', _t('WikiPage.SAVE', 'Save')), new FormAction('done', _t('WikiPage.DONE', 'Done (Draft)')), new FormAction('publish', _t('WikiPage.PUBLISH', 'Publish')) ); } else { $actions = FieldList::create( new FormAction('save', _t('WikiPage.SAVE', 'Save')), new FormAction('publish', _t('WikiPage.FINISHED', 'Finished')) ); } $actions->push(new FormAction('cancel', _t('WikiPage.CANCEL_EDIT', 'Cancel'))); $actions->push(new FormAction('revert', _t('WikiPage.REVERT_EDIT', 'Revert'))); if (Permission::check(MANAGE_WIKI_PAGES)) { $actions->push(new FormAction('addpage_t', _t('WikiPage.ADD_PAGE', 'New Page'))); $actions->push(new FormAction('delete', _t('WikiPage.DELETE_PAGE', 'Delete Page'))); } $form = new Form($this, "EditForm", $fields, $actions); $form->loadDataFrom($record); $this->extend('updateWikiEditForm', $form); return $form; } /** * Returns the form used to create new pages. If the current form is not set * (ie the user is NOT currently editing), then we just return null. * * @return Form */ public function CreatePageForm() { $createOptions = array( 'child' => 'As a child of the selected page', 'sibling' => 'As a sibling of the selected page', ); $pageTree = new TreeDropdownField('CreateContext', _t('WikiPage.CREATE_CONTEXT', 'Select an existing page'), 'WikiPage'); $pageTree->setValue($this->ID); $pageTree->setTreeBaseID($this->data()->getWikiRoot()->ID); $fields = FieldList::create( new TextField('NewPageName', _t('WikiPage.NEW_PAGE_NAME', 'New Page Name')), $pageTree, new OptionsetField('CreateType', _t('WikiPage.CREATE_OPTIONS', 'and create the new page '), $createOptions, 'child') ); $actions = FieldList::create(new FormAction('addpage', _t('WikiPage.ADD_PAGE', 'Create'))); return new Form($this, 'CreatePageForm', $fields, $actions); } /** * basic action that the user can use to just quit editing * */ public function cancel() { return $this->redirect($this->data()->Link() . '?stage=Stage'); } /** * Option for the user to revert the changes made since it was last published */ public function revert() { if ($this->data()->IsModifiedOnStage) { $this->data()->doRevertToLive(); } return $this->redirect($this->data()->Link() . '?stage=Live'); } /** * Deletes the current page and returns the user to the parent * of the now deleted page. * */ public function delete() { $page = $this->data(); /* @var $page Page */ if ($page) { $parent = $page->Parent(); $ID = $page->ID; $page->deleteFromStage('Live'); // only fully delete if we're autopublishing stuff.. a bit counter // intuitive, but works pretty well if (WikiPage::$auto_publish) { $page->ID = $ID; $page->deleteFromStage('Stage'); } return $this->redirect($parent->Link()); return; } throw new Exception("Invalid request"); } /** * Creates an entirely new page as a child of the current page, or * 'after' a selected page. */ public function addpage($args) { if (!Permission::check(MANAGE_WIKI_PAGES)) { return Security::permissionFailure($this); } $pageName = trim($args['NewPageName']); $createType = $args['CreateType'] ? $args['CreateType'] : 'child'; if (!strlen($pageName)) { throw new Exception("Invalid page name"); } $createContext = $this->data(); if ($args['CreateContext']) { $createContext = DataObject::get_by_id('WikiPage', $args['CreateContext']); } if (!$createContext instanceof WikiPage) { throw new Exception("You must select an existing wiki page."); } // now see whether to add the new page above, below or as a child $page = new WikiPage(); $page->Title = $pageName; $page->MenuTitle = $pageName; switch ($createType) { case 'sibling': { $page->ParentID = $createContext->ParentID; break; } case 'child': default: { $page->ParentID = $createContext->ID; break; } } $page->writeToStage('Stage'); // publish if we're on autopublish if (WikiPage::$auto_publish) { $page->doPublish(); } return $this->redirect($page->Link('edit') . '?stage=Stage'); } /** * * @param WikiPage $page * @param array $data * @return WikiPage */ protected function savePage($page, $form = null, $stage = 'Stage') { // save stuff then reuse the edit action if ($form) { $form->saveInto($page); } $page->Status = ($page->Status == "New page" || $page->Status == "Saved (new)") ? "Saved (new)" : "Saved (update)"; $page->writeToStage($stage); } /** * Save the submitted data * * @return */ public function save($data, $form) { if (!$this->data()->canEdit()) { return Security::permissionFailure($this); } $existing = $this->getEditingLocks($this->data(), true); // oops, we've somehow got here even though we shouldn't have if ($existing && $existing['user'] != Member::currentUser()->Email) { return "Someone somehow locked it while you were gone, this shouldn't happen like this :("; } $this->savePage($this->data(), $form); if (WikiPage::$auto_publish) { // do publish $this->data()->doPublish(); } return $this->redirect($this->data()->Link('edit') . '?stage=Stage'); } /** * Complete editing and publish the data * * @param mixed $data * @param Form $form */ public function done($data, $form) { if (!$this->data()->canEdit()) { return Security::permissionFailure($this); } // save stuff then reuse the edit action $this->savePage($this->data(), $form); return $this->redirect($this->data()->Link() . '?stage=Stage'); } /** * Complete editing and publish the data * * @param mixed $data * @param Form $form */ public function publish($data, $form) { if (!$this->data()->canEdit()) { return Security::permissionFailure($this); } // save stuff then reuse the edit action $this->savePage($this->data(), $form); $this->data()->doPublish(); // Make sure we're on the live content now Versioned::reading_stage('Live'); // and go return $this->redirect($this->data()->Link() . '?stage=Live'); } /** * We only want to output content if we're not in edit mode * at all * * @return String */ public function Content() { if ($this->form) { return ''; } return $this->data()->ParsedContent(); //XML_val('Content'); } /** * Return the form to the user if it exists, otherwise some information * about who is currently editing * * @return Form */ public function Form() { // The editing form hasn't been put in place by the 'edit' action // so lets just show the status form $append = ''; if (!$this->form) { if (WikiPage::$show_edit_button || $this->data()->canEdit()) { // create the information form $this->form = $this->StatusForm(); } } else { // if we have got an editing form, then we'll add a New Page // form if we have permissions to do so if (Permission::check(MANAGE_WIKI_PAGES)) { $append = $this->CreatePageForm()->forTemplate(); } } return $this->form->forTemplate() . $append; } /** * Gets the status form that is used by users to trigger the editing mode * if they have the relevant access to it. * * @return Form */ public function StatusForm() { $existing = $this->getEditingLocks($this->data()); if ($existing && $existing['user'] != Member::currentUser()->Email) { $fields = FieldList::create( new ReadonlyField('ExistingEditor', '', _t('WikiPage.EXISTINGEDITOR', 'This page is currently locked for editing by ' . $existing['user'] . ' until ' . $existing['expires'])) ); $actions = FieldList::create(); } else { $fields = FieldList::create(); $actions = FieldList::create( new FormAction('startediting', _t('WikiPage.STARTEDIT', 'Edit Page')) ); } return new Form($this, 'StatusForm', $fields, $actions); } /** * Updates the lock timeout for the given object * * @param <type> $data */ public function updatelock($data) { if ($this->data()->ID && $this->data()->canEdit()) { $lock = $this->getEditingLocks($this->data(), true); $response = new stdClass(); $response->status = 1; if ($lock != null && $lock['user'] != Member::currentUser()->Email) { // someone else has stolen it ! $response->status = 0; $response->message = _t('WikiPage.LOCK_STOLEN', "Another user (" . $lock['user'] . ") has forcefully taken this lock"); } return Convert::raw2json($response); } } /** * Lock the page for editing * * @param SiteTree $page * The page being edited * @param boolean $doLock * Whether to actually lock the page for ourselves * @return array * The names of any existing editors */ protected function getEditingLocks($page, $doLock=false) { $currentStage = Versioned::current_stage(); Versioned::reading_stage('Stage'); $filter = array( 'WikiPage.ID' => $page->ID, 'WikiLockExpiry' => date('Y-m-d H:i:s'), ); $user = Member::currentUser(); $currentLock = WikiPage::get()->filter($filter)->first(); $lock = null; if ($currentLock && $currentLock->ID) { // if there's a current lock in place, lets return that value $lock = array( 'user' => $currentLock->WikiLastEditor, 'expires' => $currentLock->WikiLockExpiry, ); } // If we're trying to take the lock, make sure that a) there's no existing // lock or b) we currently hold the lock if ($doLock && ($currentLock == null || !$currentLock->ID || $currentLock->WikiLastEditor == $user->Email)) { $page->lock(); } Versioned::reading_stage($currentStage); return $lock; } /** * Called to start editing this page * */ public function startediting() { return $this->redirect($this->data()->Link('edit') . '?stage=Stage'); } /** * Show the link selector * * @return String */ public function linkselector() { return $this->renderWith(array('LinkSelectDialog')); } /** * What kind of linking is the link selection form doing * @return unknown_type */ public function LinkingType() { return isset($_GET['type']) ? $_GET['type'] : 'href'; } /** * * @return Form */ public function LinkSelectForm() { $type = isset($_GET['type']) ? $_GET['type'] : 'href'; $fields = FieldList::create( new TreeDropdownField('TargetPage', _t('WikiPage.TARGETPAGE', 'Select Page'), 'SiteTree'), new TreeDropdownField('TargetFile', _t('WikiPage.TARGETIMAGE', 'Select Image'), 'File') ); $actions = FieldList::create( new FormAction('insert', _t('WikiPage.INSERTLINK', 'Insert')) ); return Form::create($this, 'LinkSelectForm', $fields, $actions); } /** * Retrieves information about a selected image for the frontend * image insertion tool - hacky for now, ideally need to pull through the * backend ImageForm * * @return string */ public function objectdetails() { $response = new stdClass; if (isset($_GET['ID'])) { $type = null; if (isset($_GET['type'])) { $type = $_GET['type'] == 'href' ? 'SiteTree' : 'File'; } else { $type = 'SiteTree'; } $object = DataObject::get_by_id($type, $_GET['ID']); $response->Title = $object->Title; $response->Link = $object->Link(); if ($object instanceof Image) { $response->Name = $object->Name; $response->Filename = $object->Filename; $response->width = $object->getWidth(); $response->height = $object->getHeight(); } $response->error = 0; } else { $response->error = 1; $response->message = "Invalid image ID"; } echo json_encode($response); } /* * returns a formatted version of the users content field for preview */ public function livepreview(){ $content = $_POST['content']; if($formatter = $this->data()->getFormatter()){ $content = $formatter->formatRaw($content); } return $content; } /* * returns the image picker form in template for dialog window */ public function imagepicker(){ return $this->renderWith('ImagePickerDialog'); } /* * returns the link picker form in template for dialog window */ public function linkpicker(){ return $this->renderWith(array('LinkPickerDialog')); } /* * gets a list of files or pages for the dialogs autocomplete field */ public function linklist(){ $term = trim(Convert::raw2sql($this->request->getVar('term'))); $type = Convert::raw2sql($this->request->getVar('type')); if($type == 'file' || $type == 'image'){ $filter ="Title LIKE '%$term%'"; if($type == 'image'){ $filter .= " AND ClassName = 'Image'"; } if($files = DataObject::get('File', $filter, $sort='Title DESC', $join='', $limit='')){ //die($files->Count()) $this->response->addHeader('Content-type', 'application/json'); $return = array(); foreach ($files as $file){ if($file->ClassName == 'Image'){ if($file->CroppedImage(20,20)){ $label = $file->CroppedImage(20,20)->forTemplate() . " " . $file->Title; }else{ $label = "<img src='{$file->Link()}' height='20' width = '20'/> " . $file->Title; } }else{ $label = "<img src='{$file->Icon()}' height='20' width = '20'/> " . $file->Title; } $return [] = array( 'ID' => $file->ID, 'Title' => $file->Title, 'Label' => $label, 'Link' => $file->Link() ); } return Convert::raw2json($return); } }elseif($type == 'page'){ if($pages = DataObject::get('SiteTree', $filter ="Title LIKE '%$term%'", $sort='Title DESC', $join='', $limit='')){ $this->response->addHeader('Content-type', 'application/json'); $return = array(); foreach ($pages as $page){ $return [] = array( 'ID' => $page->ID, 'Label' => $page->Title, 'Title' => $page->Title, 'Link' => $page->Link() ); } return Convert::raw2json($return); } } } /* * handles the upload of an image via ajax in the insert image dialog */ public function imageupload(){ if (!Member::currentUserID()) { $return = array( 'error' => 1, 'text' => "Cannot upload there" ); return Convert::raw2json($return); } if(isset($_FILES['NewImage']) && $tempfile = $_FILES['NewImage']){ // validate // $allowed = array('jpg', 'jpeg', 'gif', 'png', 'ico'); $nameBits = explode('.', $tempfile['name']); $ext = end($nameBits); if(!in_array(strtolower($ext), $allowed)){ $return = array( 'error' => 1, 'text' => "Your image must be in jpg, gif or png format" ); return Convert::raw2json($return); } $maxsize = $_POST['MAX_FILE_SIZE']; if($tempfile['size'] > $maxsize){ $size = number_format($maxsize / 1024 / 1024, 2) . 'MB'; $return = array( 'error' => 1, 'text' => "Your image must be smaller than $size" ); return Convert::raw2json($return); } // upload // $upload = new Upload; $file = new Image(); $upload->loadIntoFile($tempfile, $file); if($upload->isError()) return false; $file = $upload->getFile(); $return = array( 'link' => $file->Link() ); return Convert::raw2json($return); }else{ // no file to upload return false; } } } ?> |