Skocz do zawartości
Zaloguj się, aby obserwować  
Invisionize.eu

[Dokumentacja IPS4] Editor (*)

Polecane posty

To use the WYSIWG editor in your code, you use the \IPS\Helpers\Form\Editor class within the Form Helper.

Editors automatically have the ability to support attachments, and the administrator can customise the editor to make certain features available in some areas and not others - due to these concerns, using an Editor is slightly more complicated than most other form types.

 

The EditorLocations Extension

You are required to create an EditorLocations extension within your application which is mostly used to provide callbacks to locate attachments uploaded to that editor.

To get started, create an EditorLocations extension file through the developer center for your application. A skeleton file will be created in the applications/app/extensions/core/EditorLocations folder with example code. You will need to provide code for the attachmentPermissionCheck() and attachmentLookup() methods.

For example, if you are using this editor for an ACP setting, the value of which will be displayed on a certain page which is visible to all members who have access to the application, the code might look something like this:

<?php

namespace IPS\app\extensions\core\EditorLocations;

class _Key
{
	/**
	 * Permission check for attachments
	 *
	 * @param	\IPS\Member	$member	The member
	 * @param	int|null	$id1	Primary ID
	 * @param	int|null	$id2	Secondary ID
	 * @param	string|null	$id3	Arbitrary data
	 * @return	bool
	 */
	public function attachmentPermissionCheck( $member, $id1, $id2, $id3 )
	{
		return $member->canAccessModule( \IPS\Application\Module::get( 'app', 'module' ) );
	}
	
	/**
	 * Attachment lookup
	 *
	 * @param	int|null	$id1	Primary ID
	 * @param	int|null	$id2	Secondary ID
	 * @param	string|null	$id3	Arbitrary data
	 * @return	\IPS\Http\Url|\IPS\Content|\IPS\Node\Model
	 * @throws	\LogicException
	 */
	public function attachmentLookup( $id1, $id2, $id3 )
	{
		return \IPS\Http\Url::internal( 'app=app&module=module&controller=controller', 'front' );
	}
}

Or if you are using this editor for the description for Content Items that members create, the code might look something like:

<?php

namespace IPS\app\extensions\core\EditorLocations;

class _Key
{
	/**
	 * Permission check for attachments
	 *
	 * @param	\IPS\Member	$member	The member
	 * @param	int|null	$id1	Primary ID
	 * @param	int|null	$id2	Secondary ID
	 * @param	string|null	$id3	Arbitrary data
	 * @return	bool
	 */
	public function attachmentPermissionCheck( $member, $id1, $id2, $id3 )
	{
		try
		{
			return \IPS\app\Thing::load( $id1 )->canView( $member );
		}
		catch ( \OutOfRangeException $e )
		{
			return FALSE;
		}
	}
	
	/**
	 * Attachment lookup
	 *
	 * @param	int|null	$id1	Primary ID
	 * @param	int|null	$id2	Secondary ID
	 * @param	string|null	$id3	Arbitrary data
	 * @return	\IPS\Http\Url|\IPS\Content|\IPS\Node\Model
	 * @throws	\LogicException
	 */
	public function attachmentLookup( $id1, $id2, $id3 )
	{
		return \IPS\app\Thing::load( $id1 )->url();
	}
}

However the appropriate code to use will depend on the nature of how the content created by your editor will be used.

Note that you do not have to (and shouldn't) create a separate extension for every single editor. It is common practice for example, to use one extension for every setting field within your application. The $id parameters allow you to know specifically what piece of content is referenced, as explained below.

You must also create a language string which identfies your editor with the key "editor__app_Key". This is used to display to the admin when they are configuring which buttons show up in which areas. For example, in the core application, the key "editor__core_Contact" is defined as "Contact Form".

 

Creating the element

When creating the element you must provide an $options parameter specifying the extension you just created, along with some additional information:

  • autoSaveKey is a string which identifies this editor's purpose. For example, if the editor is for replying to a topic with ID 5, you could use "topic-reply-5". Make sure you pass the same key every time, but a different key for different editors.
  • attachIds are discussed below. Can contain up to 3 elements, and the first 2 must be numeric, but the last can be a string

For example, the code to create your element will look something like:

$form->add( new \IPS\Helpers\Form\Editor( 'my_editor', NULL, TRUE, array( 'app' => 'app', 'key' => 'Key', 'autoSaveKey' => 'my-editor-field', 'attachIds' => array( ... ) ) );

 

Claiming Attachments

Generally speaking, there are two types of content: things which always exist (like settings) and content which is created and deleted. Attachments are handled differently for each:

Things which always exist

Pass an identifier to the attachIds parameter. For example, you might do:

'attachIds' => array( 1 )

And then in your extension you will look at $id1 and know that 1 is for this instance of the editor. Then you would use different numbers for other editors using the same extension.

Even if there is only one editor using this extension, you must provide a value for attachIds. The system will then automatically handle claiming attachments.

Things which are created and deleted

When displaying the editor on the "create" screen you will pass NULL for attachIds (because of course at this point you don't know what ID you will save it with). Then, in the code for your form which handles creating the content, after you have created the content and therefore have an ID for it, you call this code:

\IPS\File::claimAttachments( $autoSaveKey, $id1, $id2, $id3 );

$autoSaveKey is the same value used for the autoSaveKey - each of the $id fields are optional but you must provide at least one. They are what will be passed to the method in your extension.

When displaying the editor on the "edit" screen, you pass the ID values to attachIds and do not call claimAttachments.

For example:

$editing = NULL;
if ( \IPS\Request::i()->id )
{
	try
	{
		$editing = \IPS\app\Thing::load( \IPS\Request::i()->id );
	}
	catch ( \OutOfRangeException $e )
	{
		\IPS\Output::i()->error( ... );
	}
}

$form = new \IPS\Helpers\Form;
$form->add( new \IPS\Helpers\Form\Editor( 'my_editor', NULL, TRUE, array( 'app' => 'app', 'key' => 'Key', 'autoSaveKey' => $editing ? 'creating-thing' : "editing-thing-{$editing->id}", 'attachIds' => $editing ? array( $editing->id ) : NULL ) ) );

if ( $values = $form->values() )
{
	if ( !$editing )
	{
		$item = new \IPS\app\Thing;
		$item->content = $values['my_editor'];
		$item->save();
		
		\IPS\File::claimAttachments( 'creating-thing', $item->id );
	}
	else
	{
		$editing->content = $values['my_editor'];
		$editing->save();
	}
}

 

Displaying the value

The value is automatically parsed including replacing profanity and other settings configured by the administrator, and sanitised of any security concerns. You can safely store the display value and display it without any further parsing.

When sending any variable to a template, the system will automatically escape it to prevent XSS vulnerabilities. Since editor content is allowed to contain HTML, and has been sanitised, you can override this escaping by doing:

{$val|raw}

 

Wyświetl pełny artykuł

Udostępnij ten post


Link to postu
Udostępnij na innych stronach

Bądź aktywny! Zaloguj się lub utwórz konto

Tylko zarejestrowani użytkownicy mogą komentować zawartość tej strony

Zaloguj się, aby obserwować  

  • Kto przegląda   0 użytkowników

    Brak zalogowanych użytkowników przeglądających tę stronę.

×

Ważne informacje

Kontynuując przeglądanie strony, wyrażasz zgodę na używanie przez nas plików cookies.