|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @link http://www.newicon.net/neon |
|
4
|
|
|
* @copyright Copyright (c) 2017-2018 Newicon Ltd |
|
5
|
|
|
* @license http://www.newicon.net/neon/license/ |
|
6
|
|
|
* @author Steve O'Brien <[email protected]> 10/10/2017 16:20 |
|
7
|
|
|
* @package neon/phoebe |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace neon\phoebe\controllers\appforms\api; |
|
11
|
|
|
|
|
12
|
|
|
use neon\core\web\ApiController; |
|
13
|
|
|
use yii\web\HttpException; |
|
14
|
|
|
use neon\core\helpers\Hash; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* This handles form definition changes via the Phoebe Builder |
|
18
|
|
|
*/ |
|
19
|
|
|
class FormController extends ApiController |
|
20
|
|
|
{ |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @inheritdoc |
|
24
|
|
|
*/ |
|
25
|
|
|
public function verbs() |
|
26
|
|
|
{ |
|
27
|
|
|
return [ |
|
28
|
|
|
'save' => ['post'], |
|
29
|
|
|
]; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Save an applicationForm definition |
|
34
|
|
|
* @param string $class the application form class_type |
|
35
|
|
|
* @return array|null json |
|
36
|
|
|
*/ |
|
37
|
|
|
public function actionSave($class) |
|
38
|
|
|
{ |
|
39
|
|
|
$phoebe = $this->phoebe(); |
|
40
|
|
|
|
|
41
|
|
|
// temporary hack to sort out uuids being passed in rather than |
|
42
|
|
|
// a human readable definition. |
|
43
|
|
|
if ($this->is_uuid($class)) |
|
44
|
|
|
$class=str_replace(['-','_'], ['a','Z'],Hash::uuid2uuid64($class)); |
|
45
|
|
|
else |
|
46
|
|
|
$class = $phoebe->canonicaliseRef($class); |
|
47
|
|
|
|
|
48
|
|
|
$definition = neon()->request->getBodyParams(); |
|
49
|
|
|
if (!isset($definition['name'])) { |
|
50
|
|
|
throw new HttpException(400, 'You must supply a form name'); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
// start a migration for the changes |
|
54
|
|
|
$this->startMigration('appform_'.$class.'_edit'); |
|
|
|
|
|
|
55
|
|
|
|
|
56
|
|
|
// save the class information |
|
57
|
|
|
$phoebeClass = $phoebe->getClass($class); |
|
58
|
|
|
if (!$phoebeClass) |
|
59
|
|
|
$phoebeClass = $phoebe->addClass($class, 'phoebe'); |
|
60
|
|
|
$phoebeClass->editClass([ |
|
61
|
|
|
'definition' => $definition, |
|
62
|
|
|
'label' => $definition['name'] |
|
63
|
|
|
]); |
|
64
|
|
|
|
|
65
|
|
|
// close the migration |
|
66
|
|
|
$this->endMigration(); |
|
67
|
|
|
|
|
68
|
|
|
$phoebeClass = $phoebe->getClass($class); |
|
69
|
|
|
if ($phoebeClass == null) { |
|
70
|
|
|
$phoebeClass = ['error' => "No class exists with a type of '$class'"]; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
return $phoebeClass->toArray(); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
// ------------------------------------- // |
|
77
|
|
|
// ----------- Private Methods ----------// |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Temporary hack to see if the string is a uuid - not an exhaustive test |
|
81
|
|
|
*/ |
|
82
|
|
|
private function is_uuid($str) |
|
83
|
|
|
{ |
|
84
|
|
|
return (strlen($str)==36 && substr_count($str, '-')==4); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Create a migration with the provided label |
|
89
|
|
|
* @param type $label |
|
|
|
|
|
|
90
|
|
|
*/ |
|
91
|
|
|
private function startMigration($label) |
|
92
|
|
|
{ |
|
93
|
|
|
neon('dds')->getIDdsAppMigrator('phoebe')->openMigrationFile($label); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* close the currently open migration |
|
98
|
|
|
*/ |
|
99
|
|
|
private function endMigration() |
|
100
|
|
|
{ |
|
101
|
|
|
neon('dds')->getIDdsAppMigrator('phoebe')->closeMigrationFile(); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
private function phoebe() |
|
105
|
|
|
{ |
|
106
|
|
|
return neon('phoebe')->getService()->getPhoebeType('applicationForm'); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
} |
|
110
|
|
|
|