mirror of
https://github.com/danielbrendel/hortusfox-web.git
synced 2026-05-08 14:49:41 -05:00
#205 Add/Edit/Delete functionality
This commit is contained in:
@@ -138,6 +138,9 @@ return [
|
||||
array('/api/plants/remove', 'ANY', 'api@remove_plant'),
|
||||
array('/api/plants/list', 'ANY', 'api@get_plant_list'),
|
||||
array('/api/plants/search', 'ANY', 'api@search_plants'),
|
||||
array('/api/plants/attributes/add', 'ANY', 'api@add_attribute'),
|
||||
array('/api/plants/attributes/edit', 'ANY', 'api@edit_attribute'),
|
||||
array('/api/plants/attributes/remove', 'ANY', 'api@remove_attribute'),
|
||||
|
||||
/** Backup Controller */
|
||||
array('/export/start', 'POST', 'backup@export'),
|
||||
|
||||
+86
-2
@@ -6,7 +6,7 @@
|
||||
class ApiController extends BaseController {
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
//parent::__construct();
|
||||
|
||||
$token = null;
|
||||
if (isset($_GET['token'])) {
|
||||
@@ -30,10 +30,16 @@ class ApiController extends BaseController {
|
||||
$plantId = $request->params()->query('plant', null);
|
||||
|
||||
$plant = PlantsModel::getDetails($plantId);
|
||||
$cust_attr = CustPlantAttrModel::getForPlant($plantId);
|
||||
|
||||
$data = [
|
||||
'default' => $plant?->asArray(),
|
||||
'custom' => $cust_attr
|
||||
];
|
||||
|
||||
return json([
|
||||
'code' => 200,
|
||||
'data' => $plant?->asArray()
|
||||
'data' => $data
|
||||
]);
|
||||
} catch (\Exception $e) {
|
||||
return json([
|
||||
@@ -149,4 +155,82 @@ class ApiController extends BaseController {
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles URL: /api/plants/attributes/add
|
||||
*
|
||||
* @param Asatru\Controller\ControllerArg $request
|
||||
* @return Asatru\View\JsonHandler
|
||||
*/
|
||||
public static function add_attribute($request)
|
||||
{
|
||||
try {
|
||||
$plant = $request->params()->query('plant', null);
|
||||
$label = $request->params()->query('label', null);
|
||||
$datatype = $request->params()->query('datatype', null);
|
||||
$content = $request->params()->query('content', null);
|
||||
|
||||
CustPlantAttrModel::addAttribute($plant, $label, $datatype, $content, true);
|
||||
|
||||
return json([
|
||||
'code' => 200
|
||||
]);
|
||||
} catch (\Exception $e) {
|
||||
return json([
|
||||
'code' => 500,
|
||||
'msg' => $e->getMessage()
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles URL: /api/plants/attributes/edit
|
||||
*
|
||||
* @param Asatru\Controller\ControllerArg $request
|
||||
* @return Asatru\View\JsonHandler
|
||||
*/
|
||||
public static function edit_attribute($request)
|
||||
{
|
||||
try {
|
||||
$attribute = $request->params()->query('attribute', null);
|
||||
$label = $request->params()->query('label', null);
|
||||
$datatype = $request->params()->query('datatype', null);
|
||||
$content = $request->params()->query('content', null);
|
||||
|
||||
CustPlantAttrModel::editAttribute($attribute, null, $label, $datatype, $content, true);
|
||||
|
||||
return json([
|
||||
'code' => 200
|
||||
]);
|
||||
} catch (\Exception $e) {
|
||||
return json([
|
||||
'code' => 500,
|
||||
'msg' => $e->getMessage()
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles URL: /api/plants/attributes/remove
|
||||
*
|
||||
* @param Asatru\Controller\ControllerArg $request
|
||||
* @return Asatru\View\JsonHandler
|
||||
*/
|
||||
public static function remove_attribute($request)
|
||||
{
|
||||
try {
|
||||
$attribute = $request->params()->query('attribute', null);
|
||||
|
||||
CustPlantAttrModel::removeAttribute($attribute, true);
|
||||
|
||||
return json([
|
||||
'code' => 200
|
||||
]);
|
||||
} catch (\Exception $e) {
|
||||
return json([
|
||||
'code' => 500,
|
||||
'msg' => $e->getMessage()
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -76,22 +76,27 @@ class CustPlantAttrModel extends \Asatru\Database\Model {
|
||||
* @param $label
|
||||
* @param $datatype
|
||||
* @param $content
|
||||
* @param $api
|
||||
* @return void
|
||||
* @throws \Exception
|
||||
*/
|
||||
public static function addAttribute($plant, $label, $datatype, $content)
|
||||
public static function addAttribute($plant, $label, $datatype, $content, $api = false)
|
||||
{
|
||||
try {
|
||||
$user = UserModel::getAuthUser();
|
||||
if (!$user) {
|
||||
throw new \Exception('Invalid user');
|
||||
if (!$api) {
|
||||
$user = UserModel::getAuthUser();
|
||||
if (!$user) {
|
||||
throw new \Exception('Invalid user');
|
||||
}
|
||||
}
|
||||
|
||||
static::raw('INSERT INTO `' . self::tableName() . '` (plant, label, datatype, content) VALUES(?, ?, ?, ?)', [
|
||||
$plant, $label, $datatype, static::interpretContent($content, $datatype)
|
||||
]);
|
||||
|
||||
LogModel::addLog($user->get('id'), $plant, $label . ' (' . $datatype . ')', $content, url('/plants/details/' . $plant));
|
||||
if (!$api) {
|
||||
LogModel::addLog($user->get('id'), $plant, '[add] ' . $label . ' (' . $datatype . ')', $content, url('/plants/details/' . $plant));
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
throw $e;
|
||||
}
|
||||
@@ -103,22 +108,27 @@ class CustPlantAttrModel extends \Asatru\Database\Model {
|
||||
* @param $label
|
||||
* @param $datatype
|
||||
* @param $content
|
||||
* @param $api
|
||||
* @return void
|
||||
* @throws \Exception
|
||||
*/
|
||||
public static function editAttribute($id, $plant, $label, $datatype, $content)
|
||||
public static function editAttribute($id, $plant, $label, $datatype, $content, $api = false)
|
||||
{
|
||||
try {
|
||||
$user = UserModel::getAuthUser();
|
||||
if (!$user) {
|
||||
throw new \Exception('Invalid user');
|
||||
if (!$api) {
|
||||
$user = UserModel::getAuthUser();
|
||||
if (!$user) {
|
||||
throw new \Exception('Invalid user');
|
||||
}
|
||||
}
|
||||
|
||||
static::raw('UPDATE `' . self::tableName() . '` SET label = ?, datatype = ?, content = ? WHERE id = ?', [
|
||||
$label, $datatype, static::interpretContent($content, $datatype), $id
|
||||
]);
|
||||
|
||||
LogModel::addLog($user->get('id'), $plant, $label . ' (' . $datatype . ')', $content, url('/plants/details/' . $plant));
|
||||
if (!$api) {
|
||||
LogModel::addLog($user->get('id'), $plant, '[edit] ' . $label . ' (' . $datatype . ')', $content, url('/plants/details/' . $plant));
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
throw $e;
|
||||
}
|
||||
@@ -126,13 +136,30 @@ class CustPlantAttrModel extends \Asatru\Database\Model {
|
||||
|
||||
/**
|
||||
* @param $id
|
||||
* @param $api
|
||||
* @return void
|
||||
* @throws \Exception
|
||||
*/
|
||||
public static function removeAttribute($id)
|
||||
public static function removeAttribute($id, $api = false)
|
||||
{
|
||||
try {
|
||||
if (!$api) {
|
||||
$user = UserModel::getAuthUser();
|
||||
if (!$user) {
|
||||
throw new \Exception('Invalid user');
|
||||
}
|
||||
}
|
||||
|
||||
$item = static::raw('SELECT * FROM `' . self::tableName() . '` WHERE id = ?', [$id])->first();
|
||||
if (!$item) {
|
||||
throw new \Exception('Custom plant attribute not found: ' . $id);
|
||||
}
|
||||
|
||||
static::raw('DELETE FROM `' . self::tableName() . '` WHERE id = ?', [$id]);
|
||||
|
||||
if (!$api) {
|
||||
LogModel::addLog($user->get('id'), $item->get('plant'), '[remove] ' . $item->get('label') . ' (' . $item->get('datatype') . ')', $item->get('content'), url('/plants/details/' . $item->get('plant')));
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
throw $e;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user