From: Alexander Ebert Date: Wed, 7 Sep 2011 16:02:19 +0000 (+0200) Subject: Added actions for clipboard API X-Git-Tag: 2.0.0_Beta_1~1765^2~51 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=04f156c8068cb7e0e1369b239961dd898b4e62b4;p=GitHub%2FWoltLab%2FWCF.git Added actions for clipboard API --- diff --git a/wcfsetup/install/files/lib/acp/action/ClipboardAction.class.php b/wcfsetup/install/files/lib/acp/action/ClipboardAction.class.php new file mode 100644 index 0000000000..50b5a9c7a2 --- /dev/null +++ b/wcfsetup/install/files/lib/acp/action/ClipboardAction.class.php @@ -0,0 +1,14 @@ + + * @package com.woltlab.wcf + * @subpackage acp.action + * @category Community Framework + */ +class ClipboardAction extends \wcf\action\ClipboardAction { } diff --git a/wcfsetup/install/files/lib/acp/action/ClipboardProxyAction.class.php b/wcfsetup/install/files/lib/acp/action/ClipboardProxyAction.class.php new file mode 100644 index 0000000000..9041d12a5b --- /dev/null +++ b/wcfsetup/install/files/lib/acp/action/ClipboardProxyAction.class.php @@ -0,0 +1,14 @@ + + * @package com.woltlab.wcf + * @subpackage acp.action + * @category Community Framework + */ +class ClipboardProxyAction extends \wcf\action\ClipboardProxyAction { } diff --git a/wcfsetup/install/files/lib/action/ClipboardAction.class.php b/wcfsetup/install/files/lib/action/ClipboardAction.class.php new file mode 100644 index 0000000000..115e423a1e --- /dev/null +++ b/wcfsetup/install/files/lib/action/ClipboardAction.class.php @@ -0,0 +1,157 @@ + + * @package com.woltlab.wcf + * @subpackage action + * @category Community Framework + */ +class ClipboardAction extends AbstractSecureAction { + /** + * clipboard action + * @var string + */ + protected $action = ''; + + /** + * list of object ids + * @var array + */ + protected $objectIDs = array(); + + /** + * clipboard page class name + * @var string + */ + protected $pageClassName = ''; + + /** + * object type + * @var string + */ + protected $type = ''; + + /** + * clipboard item type id + * @var integer + */ + protected $typeID = 0; + + /** + * @see wcf\action\AbstractAction::_construct() + */ + public function __construct() { + try { + parent::__construct(); + } + catch (\Exception $e) { + if ($e instanceof AJAXException) { + throw $e; + } + else { + throw new AJAXException($e->getMessage()); + } + } + } + + /** + * @see wcf\action\Action::readParameters() + */ + public function readParameters() { + parent::readParameters(); + + if (isset($_POST['action'])) $this->action = StringUtil::trim($_POST['action']); + if (isset($_POST['objectIDs']) && is_array($_POST['objectIDs'])) $this->objectIDs = ArrayUtil::toIntegerArray($_POST['objectIDs']); + if (isset($_POST['pageClassName'])) $this->pageClassName = StringUtil::trim($_POST['pageClassName']); + if (isset($_POST['type'])) $this->type = StringUtil::trim($_POST['type']); + } + + /** + * @see wcf\action\Action::execute() + */ + public function execute() { + parent::execute(); + + // validate parameters + $this->validate(); + + // execute action + ClipboardHandler::getInstance()->{$this->action}($this->objectIDs, $this->typeID); + + // get editor items + $editorItems = $this->getEditorItems(); + // send JSON response + header('Content-type: application/json'); + echo JSON::encode(array( + 'items' => $editorItems + )); + exit; + } + + /** + * Returns a list of clipboard editor items grouped by type name. + * + * @return array + */ + protected function getEditorItems() { + $data = ClipboardHandler::getInstance()->getEditorItems($this->pageClassName); + if ($data === null) { + return array(); + } + + $editorItems = array(); + foreach ($data as $typeName => $itemData) { + $items = array( + 'label' => $itemData['label'], + 'items' => array() + ); + + foreach ($itemData['items'] as $item) { + $items['items'][] = array( + 'actionName' => $item->getName(), + 'internalData' => $item->getInternalData(), + 'parameters' => $item->getParameters(), + 'label' => WCF::getLanguage()->get('wcf.clipboard.item.' . $item->getName()), + 'url' => $item->getURL() + ); + } + + $editorItems[$typeName] = $items; + } + + return $editorItems; + } + + /** + * Validates parameters. + */ + protected function validate() { + if (empty($this->objectIDs)) { + throw new AJAXException("Invalid object ids given."); + } + + if (empty($this->pageClassName)) { + throw new AJAXException("page not given"); + } + + if ($this->action != 'mark' && $this->action != 'unmark') { + throw new AJAXException("Clipboard action '".$this->action."' is invalid."); + } + + $this->typeID = (!empty($this->type)) ? ClipboardHandler::getInstance()->getTypeID($this->type) : null; + if ($this->typeID === null) { + throw new AJAXException("Clipboard item type '".$this->type."' is invalid."); + } + } +} diff --git a/wcfsetup/install/files/lib/action/ClipboardProxyAction.class.php b/wcfsetup/install/files/lib/action/ClipboardProxyAction.class.php new file mode 100644 index 0000000000..f88982e3c1 --- /dev/null +++ b/wcfsetup/install/files/lib/action/ClipboardProxyAction.class.php @@ -0,0 +1,141 @@ + + * @package com.woltlab.wcf + * @subpackage action + * @category Community Framework + */ +class ClipboardProxyAction extends AbstractSecureAction { + /** + * IDatabaseObjectAction object + * @var wcf\data\IDatabaseObjectAction + */ + protected $objectAction = null; + + /** + * list of parameters + * @var array + */ + protected $parameters = array(); + + /** + * type name identifier + * @var string + */ + protected $typeName = ''; + + /** + * @see wcf\action\AbstractAction::_construct() + */ + public function __construct() { + try { + parent::__construct(); + } + catch (\Exception $e) { + if ($e instanceof AJAXException) { + throw $e; + } + else { + throw new AJAXException($e->getMessage()); + } + } + } + + /** + * @see wcf\action\IAction::readParameters() + */ + public function readParameters() { + parent::readParameters(); + + if (isset($_POST['parameters']) && is_array($_POST['parameters'])) $this->parameters = $_POST['parameters']; + if (isset($_POST['typeName'])) $this->typeName = StringUtil::trim($_POST['typeName']); + } + + /** + * Validates parameters. + */ + protected function validate() { + // validate required parameters + if (!isset($this->parameters['className']) || empty($this->parameters['className'])) { + throw new AJAXException("missing class name"); + } + if (!isset($this->parameters['actionName']) || empty($this->parameters['actionName'])) { + throw new AJAXException("missing action name"); + } + if (empty($this->typeName)) { + throw new AJAXException("type name cannot be empty"); + } + + // validate class name + if (!class_exists($this->parameters['className'])) { + throw new AJAXException("unknown class '".$this->parameters['className']."'"); + } + if (!ClassUtil::isInstanceOf($this->parameters['className'], 'wcf\data\IDatabaseObjectAction')) { + throw new AJAXException("'".$this->parameters['className']."' should implement wcf\system\IDatabaseObjectAction"); + } + } + + /** + * Loads object ids from clipboard. + * + * @return array + */ + protected function getObjectIDs() { + $typeID = ClipboardHandler::getInstance()->getTypeID($this->typeName); + if ($typeID === null) { + throw new AJAXException("clipboard item type '".$this->typeName."' is unknown"); + } + + $objects = ClipboardHandler::getInstance()->getMarkedItems($typeID); + if (!count($objects) || !isset($objects[$this->typeName]) || !count($objects[$this->typeName])) { + return null; + } + + return array_keys($objects[$this->typeName]); + } + + /** + * @see wcf\action\IAction::execute() + */ + public function execute() { + parent::execute(); + + // get object ids + $objectIDs = $this->getObjectIDs(); + + // create object action instance + $this->objectAction = new $this->parameters['className']($objectIDs, $this->actionName); + + // validate action + try { + $this->objectAction->validateAction(); + } + catch (ValidateActionException $e) { + throw new AJAXException("validation failed: ".$e->getMessage()); + } + + // execute action + try { + $this->response = $this->objectAction->executeAction(); + } + catch (\Exception $e) { + throw new AJAXException('unknown exception caught: '.$e->getMessage()); + } + $this->executed(); + + // send JSON-encoded response + header('Content-type: application/json'); + echo JSON::encode($this->response); + exit; + } +}