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

228 lines
5.7 KiB
PHP
Raw Normal View History

2016-08-04 19:01:30 +02:00
<?php namespace App\Ninja\Intents;
2016-08-07 21:42:32 +02:00
use stdClass;
2016-08-07 08:10:36 +02:00
use Exception;
2016-08-07 21:42:32 +02:00
use App\Libraries\CurlUtils;
2016-08-10 14:57:34 +02:00
use App\Libraries\Skype\SkypeResponse;
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) {
2016-08-13 21:19:37 +02:00
if ( ! $state || is_string($state)) {
2016-08-09 22:06:24 +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 = [];
}
}
}
$this->state = $state;
2016-08-06 19:54:56 +02:00
$this->data = $data;
2016-08-09 22:06:24 +02:00
2016-08-10 14:57:34 +02:00
//var_dump($state);
2016-08-04 19:01:30 +02:00
}
2016-08-07 21:42:32 +02:00
public static function createIntent($state, $data)
2016-08-04 19:01:30 +02:00
{
2016-08-06 19:54:56 +02: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') {
$entityType = $entity->entity;
break;
}
}
if ( ! $entityType) {
$entityType = $state->current->entityType;
}
$entityType = ucwords(strtolower($entityType));
$intent = str_replace('Entity', $entityType, $intent);
2016-08-09 22:06:24 +02:00
$className = "App\\Ninja\\Intents\\{$intent}Intent";
2016-08-07 08:10:36 +02:00
2016-08-13 21:19:37 +02:00
//echo "Intent: $intent<p>";
2016-08-10 14:57:34 +02:00
2016-08-07 08:10:36 +02:00
if ( ! class_exists($className)) {
throw new Exception(trans('texts.intent_not_supported'));
}
2016-08-07 21:42:32 +02:00
return (new $className($state, $data));
2016-08-04 19:01:30 +02:00
}
2016-08-09 22:06:24 +02:00
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
{
2016-08-09 22:06:24 +02:00
if ( ! is_array($entities)) {
$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') {
2016-08-07 21:42:32 +02:00
$client = $clientRepo->findPhonetically($param->entity);
}
}
return $client;
}
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
2016-08-10 14:57:34 +02:00
if ( ! isset($this->data->compositeEntities)) {
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') {
$field = $child->value;;
} 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;
}
protected function processField($field)
{
$field = str_replace(' ', '_', $field);
2016-08-07 21:42:32 +02:00
2016-08-09 22:06:24 +02:00
if (strpos($field, 'date') !== false) {
$field .= '_sql';
}
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
} 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
}