Version 4.7.0
This commit is contained in:
1 parent
517a5e1f70
commit
8ba7d43898
1933 files changed
+65613
-11332
No files matched your search
+171
-24
@@ -20,7 +20,7 @@ if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
/**
|
||||
* Webhook
|
||||
*/
|
||||
class _Webhook extends \IPS\Patterns\ActiveRecord
|
||||
class _Webhook extends \IPS\Node\Model
|
||||
{
|
||||
/**
|
||||
* @brief [ActiveRecord] Multiton Store
|
||||
@@ -42,9 +42,103 @@ class _Webhook extends \IPS\Patterns\ActiveRecord
|
||||
* @note Defined cache keys will be cleared automatically as needed
|
||||
*/
|
||||
protected $caches = array('webhooks');
|
||||
|
||||
|
||||
/**
|
||||
* Fire all webhooks for given event
|
||||
* @brief [Node] Node Title
|
||||
*/
|
||||
public static $nodeTitle = 'api_webhooks';
|
||||
|
||||
/**
|
||||
* @brief [Node] ACP Restrictions
|
||||
* @code
|
||||
array(
|
||||
'app' => 'core', // The application key which holds the restrictrions
|
||||
'module' => 'foo', // The module key which holds the restrictions
|
||||
'map' => array( // [Optional] The key for each restriction - can alternatively use "prefix"
|
||||
'add' => 'foo_add',
|
||||
'edit' => 'foo_edit',
|
||||
'permissions' => 'foo_perms',
|
||||
'delete' => 'foo_delete'
|
||||
),
|
||||
'all' => 'foo_manage', // [Optional] The key to use for any restriction not provided in the map (only needed if not providing all 4)
|
||||
'prefix' => 'foo_', // [Optional] Rather than specifying each key in the map, you can specify a prefix, and it will automatically look for restrictions with the key "[prefix]_add/edit/permissions/delete"
|
||||
* @endcode
|
||||
*/
|
||||
protected static $restrictions = array(
|
||||
'app' => 'core',
|
||||
'module' => 'applications',
|
||||
'prefix' => 'api_',
|
||||
);
|
||||
|
||||
/**
|
||||
* Set Default Values
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function setDefaultValues()
|
||||
{
|
||||
$this->_data['filters'] = '{}';
|
||||
$this->_data['url'] = '';
|
||||
$this->_data['events'] = '';
|
||||
}
|
||||
|
||||
/**
|
||||
* [Node] Add/Edit Form
|
||||
*
|
||||
* @param \IPS\Helpers\Form $form The form
|
||||
* @return void
|
||||
*/
|
||||
public function form( &$form )
|
||||
{
|
||||
$form->add( new \IPS\Helpers\Form\Url( 'url', $this->url ?? "", TRUE ) );
|
||||
$events = [];
|
||||
|
||||
foreach ( \IPS\Api\Webhook::getAvailableWebhooks() as $app => $hooks )
|
||||
{
|
||||
if( \count( $hooks ))
|
||||
{
|
||||
foreach( $hooks as $hook => $params)
|
||||
{
|
||||
$events[$hook] = "webhook_". $hook ;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$form->add( new \IPS\core\Form\WebhookSelector('webhook_events', $this->events ?? [], TRUE , [ 'options' => $events] ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* [Node] Format form values from add/edit form for save
|
||||
*
|
||||
* @param array $values Values from the form
|
||||
* @return array
|
||||
*/
|
||||
public function formatFormValues( $values )
|
||||
{
|
||||
$values['events'] = $values['webhook_events'];
|
||||
unset( $values['webhook_events'] );
|
||||
|
||||
|
||||
return $values;
|
||||
}
|
||||
|
||||
public function get__title()
|
||||
{
|
||||
return (string) $this->url;
|
||||
}
|
||||
|
||||
/**
|
||||
* [Node] Get Node Description
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
protected function get__description()
|
||||
{
|
||||
return \IPS\Theme::i()->getTemplate( 'api', 'core' )->webhookDesc( $this );
|
||||
}
|
||||
|
||||
/**
|
||||
* This method will log the webhook to be fired, it's going to be fired via a task!
|
||||
*
|
||||
* @param string $event The event key
|
||||
* @param mixed $data Data
|
||||
@@ -52,14 +146,7 @@ class _Webhook extends \IPS\Patterns\ActiveRecord
|
||||
* @return void
|
||||
*/
|
||||
public static function fire( $event, $data = NULL, $filters = array() )
|
||||
{
|
||||
/* Normalise data */
|
||||
if ( \is_object( $data ) and method_exists( $data, 'apiOutput' ) )
|
||||
{
|
||||
$data = $data->apiOutput();
|
||||
}
|
||||
$data = $data ? json_encode( $data ) : NULL;
|
||||
|
||||
{
|
||||
/* Get our webhooks from cache */
|
||||
if ( !isset( \IPS\Data\Store::i()->webhooks ) )
|
||||
{
|
||||
@@ -87,6 +174,28 @@ class _Webhook extends \IPS\Patterns\ActiveRecord
|
||||
$enable = FALSE;
|
||||
if ( isset( \IPS\Data\Store::i()->webhooks[ $event ] ) )
|
||||
{
|
||||
/* Normalise data */
|
||||
if ( \is_object( $data ) and method_exists( $data, 'apiOutput' ) )
|
||||
{
|
||||
$data = $data->apiOutput();
|
||||
}
|
||||
else if ( \is_array( $data ) )
|
||||
{
|
||||
foreach ( $data as $key => &$item )
|
||||
{
|
||||
/* Normalise data */
|
||||
if ( \is_object( $item ) and method_exists( $item, 'apiOutput' ) )
|
||||
{
|
||||
$item = $item->apiOutput();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* We need to replace and langstring hashes ( like node names) */
|
||||
\IPS\Member::loggedIn()->language()->parseOutputForDisplay( $data );
|
||||
|
||||
$data = $data ? json_encode( $data ) : NULL;
|
||||
|
||||
/* Loop through each one... */
|
||||
foreach ( \IPS\Data\Store::i()->webhooks[ $event ] as $id => $webhook )
|
||||
{
|
||||
@@ -168,31 +277,30 @@ class _Webhook extends \IPS\Patterns\ActiveRecord
|
||||
/**
|
||||
* Set Events
|
||||
*
|
||||
* @param array $events List of events
|
||||
* @param string|array $events List of events
|
||||
* @return void
|
||||
*/
|
||||
public function set_events( array $events )
|
||||
public function set_events( $events )
|
||||
{
|
||||
if( !\is_array( $events ) )
|
||||
{
|
||||
$events = [$events];
|
||||
}
|
||||
$this->_data['events'] = implode( ',', $events );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get URL
|
||||
*
|
||||
* @return array
|
||||
* @return
|
||||
*/
|
||||
public function get_url()
|
||||
{
|
||||
if ( $this->_url === NULL )
|
||||
{
|
||||
$this->_url = new \IPS\Http\Url( $this->_data['url'] );
|
||||
}
|
||||
|
||||
return $this->_url;
|
||||
return $this->_data['url'] ? new \IPS\Http\Url( $this->_data['url'] ) : '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Set Events
|
||||
* Set Url
|
||||
*
|
||||
* @param \IPS\Http\Url $url The URL
|
||||
* @return void
|
||||
@@ -227,12 +335,13 @@ class _Webhook extends \IPS\Patterns\ActiveRecord
|
||||
/**
|
||||
* Get output for API
|
||||
*
|
||||
* @param \IPS\Member|NULL $authorizedMember The member making the API request or NULL for API Key / client_credentials
|
||||
* @return array
|
||||
* @apiresponse int id ID number
|
||||
* @apiresponse array events List of events to subscribe to
|
||||
* @apiresponse array events List of events this hook is subscribed to
|
||||
* @apiresponse string url URL to send webhook to
|
||||
*/
|
||||
public function apiOutput()
|
||||
public function apiOutput( \IPS\Member $authorizedMember = NULL )
|
||||
{
|
||||
return array(
|
||||
'id' => $this->id,
|
||||
@@ -240,4 +349,42 @@ class _Webhook extends \IPS\Patterns\ActiveRecord
|
||||
'url' => (string) $this->url
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public static function getAvailableWebhooks()
|
||||
{
|
||||
$hooks = [];
|
||||
foreach ( \IPS\Application::applications() as $app )
|
||||
{
|
||||
if( $app->enabled )
|
||||
{
|
||||
$hooks[$app->directory] = $app->getWebhooks();
|
||||
}
|
||||
}
|
||||
|
||||
return $hooks;
|
||||
}
|
||||
|
||||
/**
|
||||
* Search
|
||||
*
|
||||
* @param string $column Column to search
|
||||
* @param string $query Search query
|
||||
* @param string|null $order Column to order by
|
||||
* @param mixed $where Where clause
|
||||
* @return array
|
||||
*/
|
||||
public static function search( $column, $query, $order=NULL, $where=array() )
|
||||
{
|
||||
if ( $column === '_title' )
|
||||
{
|
||||
$column = 'url';
|
||||
}
|
||||
|
||||
if( $order == '_title' )
|
||||
{
|
||||
$order = 'url';
|
||||
}
|
||||
|
||||
return parent::search( $column, $query, $order, $where );
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user