1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 13:12:50 +01:00
invoiceninja/app/Ninja/Intents/BaseIntent.php

328 lines
8.4 KiB
PHP
Raw Normal View History

2017-01-30 20:40:43 +01:00
<?php
namespace App\Ninja\Intents;
2016-08-04 19:01:30 +02:00
2016-08-10 14:57:34 +02:00
use App\Libraries\Skype\SkypeResponse;
2017-04-06 15:47:37 +02:00
use App\Models\Client;
2017-01-30 20:40:43 +01:00
use Exception;
use stdClass;
2016-08-04 19:01:30 +02:00
class BaseIntent
{
2016-08-07 21:42:32 +02:00
protected $state;
2016-08-04 19:01:30 +02:00
protected $parameters;
2016-08-13 21:19:37 +02:00
protected $fieldMap = [];
2016-08-04 19:01:30 +02:00
2016-08-07 21:42:32 +02:00
public function __construct($state, $data)
2016-08-04 19:01:30 +02:00
{
2016-08-09 22:06:24 +02:00
//if (true) {
2017-01-30 17:05:31 +01:00
if (! $state || is_string($state)) {
2017-04-06 15:47:37 +02:00
$state = new stdClass();
foreach (['current', 'previous'] as $reference) {
$state->$reference = new stdClass();
$state->$reference->entityType = false;
foreach ([ENTITY_INVOICE, ENTITY_CLIENT, ENTITY_INVOICE_ITEM] as $entityType) {
$state->$reference->$entityType = [];
}
}
2016-08-09 22:06:24 +02:00
}
$this->state = $state;
2016-08-06 19:54:56 +02:00
$this->data = $data;
2017-04-09 15:03:10 +02:00
2017-04-06 15:47:37 +02:00
// If they're viewing a client set it as the current state
if (! $this->hasField('Filter', 'all')) {
$url = url()->previous();
preg_match('/clients\/(\d*)/', $url, $matches);
if (count($matches) >= 2) {
if ($client = Client::scope($matches[1])->first()) {
$this->state->current->client = $client;
}
2017-04-05 21:20:11 +02:00
}
}
2017-04-06 15:47:37 +02:00
//var_dump($state);
2017-04-05 21:20:11 +02:00
}
2017-04-04 11:54:38 +02:00
public static function createIntent($platform, $state, $data)
2016-08-04 19:01:30 +02:00
{
2017-01-30 17:05:31 +01:00
if (! count($data->intents)) {
2016-08-07 08:10:36 +02:00
throw new Exception(trans('texts.intent_not_found'));
2016-08-06 19:54:56 +02:00
}
2016-08-09 22:06:24 +02:00
$intent = $data->intents[0]->intent;
$entityType = false;
2016-08-07 21:42:32 +02:00
2016-08-10 14:57:34 +02:00
foreach ($data->entities as $entity) {
if ($entity->type === 'EntityType') {
2017-04-04 16:51:01 +02:00
$entityType = rtrim($entity->entity, 's');
2016-08-10 14:57:34 +02:00
break;
}
}
2017-04-04 11:54:38 +02:00
if ($state && ! $entityType) {
2016-08-10 14:57:34 +02:00
$entityType = $state->current->entityType;
}
2017-04-05 17:04:44 +02:00
$entityType = $entityType ?: 'client';
2016-08-10 14:57:34 +02:00
$entityType = ucwords(strtolower($entityType));
2017-04-04 16:51:01 +02:00
if ($entityType == 'Recurring') {
$entityType = 'RecurringInvoice';
}
2016-08-10 14:57:34 +02:00
$intent = str_replace('Entity', $entityType, $intent);
2017-04-04 11:54:38 +02:00
if ($platform == BOT_PLATFORM_WEB_APP) {
$className = "App\\Ninja\\Intents\\WebApp\\{$intent}Intent";
} else {
$className = "App\\Ninja\\Intents\\{$intent}Intent";
}
2016-08-07 08:10:36 +02:00
2017-01-30 17:05:31 +01:00
if (! class_exists($className)) {
2017-04-06 15:47:37 +02:00
throw new Exception(trans('texts.intent_not_supported'));
2016-08-07 08:10:36 +02:00
}
2017-01-30 20:40:43 +01:00
return new $className($state, $data);
2016-08-04 19:01:30 +02:00
}
2017-04-04 17:54:17 +02:00
protected function getField($field)
{
foreach ($this->data->entities as $entity) {
if ($entity->type === $field) {
return $entity->entity;
}
}
return false;
}
2017-04-05 21:20:11 +02:00
protected function getFields($field)
{
$data = [];
foreach ($this->data->entities as $entity) {
if ($entity->type === $field) {
$data[] = $entity->entity;
}
}
return $data;
}
protected function loadStates($entityType)
{
2017-04-05 21:39:13 +02:00
$states = array_filter($this->getFields('Filter'), function($state) {
2017-04-05 21:20:11 +02:00
return in_array($state, [STATUS_ACTIVE, STATUS_ARCHIVED, STATUS_DELETED]);
});
2017-04-05 21:42:54 +02:00
if (count($states) || $this->hasField('Filter', 'all')) {
2017-04-05 21:20:11 +02:00
session(['entity_state_filter:' . $entityType => join(',', $states)]);
}
}
2017-04-04 20:08:35 +02:00
protected function hasField($field, $value = false)
{
$fieldValue = $this->getField($field);
if ($value) {
return $fieldValue && $fieldValue == $value;
} else {
return $fieldValue ? true : false;
}
}
2016-08-04 19:01:30 +02:00
public function process()
{
2016-08-14 11:30:16 +02:00
throw new Exception(trans('texts.intent_not_supported'));
2016-08-04 19:01:30 +02:00
}
2016-08-13 21:19:37 +02:00
public function setStateEntities($entityType, $entities)
2016-08-07 21:42:32 +02:00
{
2017-01-30 17:05:31 +01:00
if (! is_array($entities)) {
2016-08-09 22:06:24 +02:00
$entities = [$entities];
}
2016-08-08 16:45:37 +02:00
$state = $this->state;
2016-08-09 22:06:24 +02:00
$state->previous->$entityType = $state->current->$entityType;
$state->current->$entityType = $entities;
}
2016-08-08 16:45:37 +02:00
2016-08-13 21:19:37 +02:00
public function setStateEntityType($entityType)
2016-08-09 22:06:24 +02:00
{
$state = $this->state;
2016-08-07 21:42:32 +02:00
2016-08-09 22:06:24 +02:00
if ($state->current->entityType == $entityType) {
return;
2016-08-08 16:45:37 +02:00
}
2016-08-07 21:42:32 +02:00
2016-08-09 22:06:24 +02:00
$state->previous->entityType = $state->current->entityType;
$state->current->entityType = $entityType;
2016-08-07 21:42:32 +02:00
}
2016-08-13 21:19:37 +02:00
public function stateEntities($entityType)
2016-08-07 21:42:32 +02:00
{
2016-08-09 22:06:24 +02:00
return $this->state->current->$entityType;
2016-08-07 21:42:32 +02:00
}
2016-08-13 21:19:37 +02:00
public function stateEntity($entityType)
2016-08-08 16:45:37 +02:00
{
2016-08-09 22:06:24 +02:00
$entities = $this->state->current->$entityType;
return count($entities) ? $entities[0] : false;
}
2016-08-13 21:19:37 +02:00
public function previousStateEntities($entityType)
2016-08-09 22:06:24 +02:00
{
return $this->state->previous->$entityType;
}
2016-08-13 21:19:37 +02:00
public function stateEntityType()
2016-08-09 22:06:24 +02:00
{
return $this->state->current->entityType;
}
public function getState()
{
return $this->state;
2016-08-08 16:45:37 +02:00
}
2016-08-13 21:19:37 +02:00
protected function requestClient()
2016-08-07 21:42:32 +02:00
{
$clientRepo = app('App\Ninja\Repositories\ClientRepository');
$client = false;
foreach ($this->data->entities as $param) {
2016-08-10 14:57:34 +02:00
if ($param->type == 'Name') {
2017-04-05 21:20:11 +02:00
$param->type = rtrim($param->type, ' \' s');
2016-08-07 21:42:32 +02:00
$client = $clientRepo->findPhonetically($param->entity);
}
}
2017-04-05 21:20:11 +02:00
if (! $client) {
$client = $this->state->current->client;
}
2016-08-07 21:42:32 +02:00
return $client;
}
2017-04-04 20:08:35 +02:00
protected function requestInvoice()
{
$invoiceRepo = app('App\Ninja\Repositories\InvoiceRepository');
$invoice = false;
foreach ($this->data->entities as $param) {
if ($param->type == 'builtin.number') {
return $invoiceRepo->findPhonetically($param->entity);
}
}
return false;
}
2016-08-13 21:19:37 +02:00
protected function requestFields()
2016-08-07 21:42:32 +02:00
{
2016-08-09 22:06:24 +02:00
$data = [];
2016-08-07 21:42:32 +02:00
2017-01-30 17:05:31 +01:00
if (! isset($this->data->compositeEntities)) {
2016-08-10 14:57:34 +02:00
return [];
}
2016-08-09 22:06:24 +02:00
foreach ($this->data->compositeEntities as $compositeEntity) {
if ($compositeEntity->parentType != 'FieldValuePair') {
continue;
}
2016-08-07 21:42:32 +02:00
2016-08-09 22:06:24 +02:00
$field = false;
$value = false;
2016-08-09 16:14:26 +02:00
2016-08-09 22:06:24 +02:00
foreach ($compositeEntity->children as $child) {
if ($child->type == 'Field') {
2017-01-30 17:05:31 +01:00
$field = $child->value;
2016-08-09 22:06:24 +02:00
} elseif ($child->type == 'Value') {
$value = $child->value;
2016-08-07 21:42:32 +02:00
}
2016-08-09 22:06:24 +02:00
}
if ($field && $value) {
$field = $this->processField($field);
$value = $this->processValue($value);
$data[$field] = $value;
}
}
2016-08-13 21:19:37 +02:00
foreach ($this->fieldMap as $key => $value) {
if (isset($data[$key])) {
$data[$value] = $data[$key];
unset($data[$key]);
}
}
2016-08-09 22:06:24 +02:00
return $data;
}
2017-04-04 16:51:01 +02:00
protected function requestFieldsAsString($fields)
{
$str = '';
foreach ($this->requestFields() as $field => $value) {
if (in_array($field, $fields)) {
$str .= $field . '=' . urlencode($value) . '&';
}
}
$str = rtrim($str, '?');
$str = rtrim($str, '&');
return $str;
}
2016-08-09 22:06:24 +02:00
protected function processField($field)
{
$field = str_replace(' ', '_', $field);
2016-08-07 21:42:32 +02:00
2017-04-04 15:57:33 +02:00
/* Shouldn't be need any more
2016-08-09 22:06:24 +02:00
if (strpos($field, 'date') !== false) {
$field .= '_sql';
}
2017-04-04 15:57:33 +02:00
*/
2016-08-09 22:06:24 +02:00
return $field;
}
protected function processValue($value)
{
// look for LUIS pre-built entity matches
foreach ($this->data->entities as $entity) {
if ($entity->entity === $value) {
if ($entity->type == 'builtin.datetime.date') {
$value = $entity->resolution->date;
$value = str_replace('XXXX', date('Y'), $value);
}
2016-08-07 21:42:32 +02:00
}
}
2016-08-09 22:06:24 +02:00
return $value;
2016-08-07 21:42:32 +02:00
}
2016-08-10 14:57:34 +02:00
protected function createResponse($type, $content)
{
$response = new SkypeResponse($type);
if (is_string($content)) {
$response->setText($content);
} else {
2016-08-10 16:04:17 +02:00
if ($content instanceof \Illuminate\Database\Eloquent\Collection) {
// do nothing
2017-01-30 17:05:31 +01:00
} elseif (! is_array($content)) {
2016-08-10 14:57:34 +02:00
$content = [$content];
}
foreach ($content as $item) {
$response->addAttachment($item);
}
}
return json_encode($response);
}
2016-08-04 19:01:30 +02:00
}