Add external links for tasks with plugin api

This commit is contained in:
Frederic Guillot
2016-01-30 20:38:20 -05:00
parent ec66a779c9
commit 5c92f46786
81 changed files with 2544 additions and 75 deletions

View File

@@ -16,6 +16,7 @@ use Pimple\Container;
* @property \Kanboard\Analytic\AverageLeadCycleTimeAnalytic $averageLeadCycleTimeAnalytic
* @property \Kanboard\Analytic\AverageTimeSpentColumnAnalytic $averageTimeSpentColumnAnalytic
* @property \Kanboard\Core\Action\ActionManager $actionManager
* @property \Kanboard\Core\ExternalLink\ExternalLinkManager $externalLinkManager
* @property \Kanboard\Core\Cache\MemoryCache $memoryCache
* @property \Kanboard\Core\Event\EventManager $eventManager
* @property \Kanboard\Core\Group\GroupManager $groupManager
@@ -97,6 +98,7 @@ use Pimple\Container;
* @property \Kanboard\Model\TaskCreation $taskCreation
* @property \Kanboard\Model\TaskDuplication $taskDuplication
* @property \Kanboard\Model\TaskExport $taskExport
* @property \Kanboard\Model\TaskExternalLink $taskExternalLink
* @property \Kanboard\Model\TaskImport $taskImport
* @property \Kanboard\Model\TaskFinder $taskFinder
* @property \Kanboard\Model\TaskFilter $taskFilter
@@ -132,6 +134,7 @@ use Pimple\Container;
* @property \Kanboard\Validator\SubtaskValidator $subtaskValidator
* @property \Kanboard\Validator\SwimlaneValidator $swimlaneValidator
* @property \Kanboard\Validator\TaskLinkValidator $taskLinkValidator
* @property \Kanboard\Validator\TaskExternalLinkValidator $taskExternalLinkValidator
* @property \Kanboard\Validator\TaskValidator $taskValidator
* @property \Kanboard\Validator\UserValidator $userValidator
* @property \Psr\Log\LoggerInterface $logger

View File

@@ -0,0 +1,36 @@
<?php
namespace Kanboard\Core\ExternalLink;
/**
* External Link Interface
*
* @package externalLink
* @author Frederic Guillot
*/
interface ExternalLinkInterface
{
/**
* Get link title
*
* @access public
* @return string
*/
public function getTitle();
/**
* Get link URL
*
* @access public
* @return string
*/
public function getUrl();
/**
* Set link URL
*
* @access public
* @param string $url
*/
public function setUrl($url);
}

View File

@@ -0,0 +1,171 @@
<?php
namespace Kanboard\Core\ExternalLink;
use Kanboard\Core\Base;
/**
* External Link Manager
*
* @package externalLink
* @author Frederic Guillot
*/
class ExternalLinkManager extends Base
{
/**
* Automatic type value
*
* @var string
*/
const TYPE_AUTO = 'auto';
/**
* Registered providers
*
* @access private
* @var array
*/
private $providers = array();
/**
* Type chosen by the user
*
* @access private
* @var string
*/
private $userInputType = '';
/**
* Text entered by the user
*
* @access private
* @var string
*/
private $userInputText = '';
/**
* Register a new provider
*
* Providers are registered in a LIFO queue
*
* @access public
* @param ExternalLinkProviderInterface $provider
* @return ExternalLinkManager
*/
public function register(ExternalLinkProviderInterface $provider)
{
array_unshift($this->providers, $provider);
return $this;
}
/**
* Get provider
*
* @access public
* @param string $type
* @throws ExternalLinkProviderNotFound
* @return ExternalLinkProviderInterface
*/
public function getProvider($type)
{
foreach ($this->providers as $provider) {
if ($provider->getType() === $type) {
return $provider;
}
}
throw new ExternalLinkProviderNotFound('Unable to find link provider: '.$type);
}
/**
* Get link types
*
* @access public
* @return array
*/
public function getTypes()
{
$types = array();
foreach ($this->providers as $provider) {
$types[$provider->getType()] = $provider->getName();
}
asort($types);
return array(self::TYPE_AUTO => t('Auto')) + $types;
}
/**
* Get dependency label from a provider
*
* @access public
* @param string $type
* @param string $dependency
* @return string
*/
public function getDependencyLabel($type, $dependency)
{
$provider = $this->getProvider($type);
$dependencies = $provider->getDependencies();
return isset($dependencies[$dependency]) ? $dependencies[$dependency] : $dependency;
}
/**
* Find a provider that match
*
* @access public
* @throws ExternalLinkProviderNotFound
* @return ExternalLinkProviderInterface
*/
public function find()
{
$provider = null;
if ($this->userInputType === self::TYPE_AUTO) {
$provider = $this->findProvider();
} else {
$provider = $this->getProvider($this->userInputType);
$provider->setUserTextInput($this->userInputText);
}
if ($provider === null) {
throw new ExternalLinkProviderNotFound('Unable to find link information from provided information');
}
return $provider;
}
/**
* Set form values
*
* @access public
* @param array $values
* @return ExternalLinkManager
*/
public function setUserInput(array $values)
{
$this->userInputType = empty($values['type']) ? self::TYPE_AUTO : $values['type'];
$this->userInputText = empty($values['text']) ? '' : trim($values['text']);
return $this;
}
/**
* Find a provider that user input
*
* @access private
* @return ExternalLinkProviderInterface
*/
private function findProvider()
{
foreach ($this->providers as $provider) {
$provider->setUserTextInput($this->userInputText);
if ($provider->match()) {
return $provider;
}
}
return null;
}
}

View File

@@ -0,0 +1,71 @@
<?php
namespace Kanboard\Core\ExternalLink;
/**
* External Link Provider Interface
*
* @package externalLink
* @author Frederic Guillot
*/
interface ExternalLinkProviderInterface
{
/**
* Get provider name (label)
*
* @access public
* @return string
*/
public function getName();
/**
* Get link type (will be saved in the database)
*
* @access public
* @return string
*/
public function getType();
/**
* Get a dictionary of supported dependency types by the provider
*
* Example:
*
* [
* 'related' => t('Related'),
* 'child' => t('Child'),
* 'parent' => t('Parent'),
* 'self' => t('Self'),
* ]
*
* The dictionary key is saved in the database.
*
* @access public
* @return array
*/
public function getDependencies();
/**
* Set text entered by the user
*
* @access public
* @param string $input
*/
public function setUserTextInput($input);
/**
* Return true if the provider can parse correctly the user input
*
* @access public
* @return boolean
*/
public function match();
/**
* Get the link found with the properties
*
* @access public
* @return ExternalLinkInterface
*/
public function getLink();
}

View File

@@ -0,0 +1,15 @@
<?php
namespace Kanboard\Core\ExternalLink;
use Exception;
/**
* External Link Provider Not Found Exception
*
* @package externalLink
* @author Frederic Guillot
*/
class ExternalLinkProviderNotFound extends Exception
{
}

View File

@@ -33,6 +33,19 @@ class Client extends Base
*/
const HTTP_USER_AGENT = 'Kanboard';
/**
* Send a GET HTTP request
*
* @access public
* @param string $url
* @param string[] $headers
* @return string
*/
public function get($url, array $headers = array())
{
return $this->doRequest('GET', $url, '', $headers);
}
/**
* Send a GET HTTP request and parse JSON response
*

View File

@@ -68,11 +68,12 @@ class Response extends Base
*
* @access public
* @param string $url Redirection URL
* @param boolean $self If Ajax request and true: refresh the current page
*/
public function redirect($url)
public function redirect($url, $self = false)
{
if ($this->request->getServerVariable('HTTP_X_REQUESTED_WITH') === 'XMLHttpRequest') {
header('X-Ajax-Redirect: '.$url);
header('X-Ajax-Redirect: '.($self ? 'self' : $url));
} else {
header('Location: '.$url);
}