From f3062b8c39034ca273681ca487f1a04cd2f44752 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Sat, 9 Feb 2019 18:37:29 -0500 Subject: [PATCH] Civi\Api4\Afform - Refactor to use BasicGetAction, BasicUpdateAction, BasicBatchAction --- .../core/Civi/Api4/Action/Afform/Create.php | 17 --- .../core/Civi/Api4/Action/Afform/Delete.php | 19 --- .../core/Civi/Api4/Action/Afform/Get.php | 11 -- .../Civi/Api4/Action/Afform/GetFields.php | 14 -- .../core/Civi/Api4/Action/Afform/Revert.php | 40 ------ .../core/Civi/Api4/Action/Afform/Update.php | 13 -- ext/afform/core/Civi/Api4/Afform.php | 136 ++++++++++++++---- .../core/Civi/Api4/Traits/AfformCrudTrait.php | 98 ------------- 8 files changed, 110 insertions(+), 238 deletions(-) delete mode 100644 ext/afform/core/Civi/Api4/Action/Afform/Create.php delete mode 100644 ext/afform/core/Civi/Api4/Action/Afform/Delete.php delete mode 100644 ext/afform/core/Civi/Api4/Action/Afform/Get.php delete mode 100644 ext/afform/core/Civi/Api4/Action/Afform/GetFields.php delete mode 100644 ext/afform/core/Civi/Api4/Action/Afform/Revert.php delete mode 100644 ext/afform/core/Civi/Api4/Action/Afform/Update.php delete mode 100644 ext/afform/core/Civi/Api4/Traits/AfformCrudTrait.php diff --git a/ext/afform/core/Civi/Api4/Action/Afform/Create.php b/ext/afform/core/Civi/Api4/Action/Afform/Create.php deleted file mode 100644 index d38c5d659a..0000000000 --- a/ext/afform/core/Civi/Api4/Action/Afform/Create.php +++ /dev/null @@ -1,17 +0,0 @@ -where)) { - throw new \API_Exception('Cannot revert with no "where" paramater specified'); - } - - $scanner = \Civi::service('afform_scanner'); - - parent::_run($result); - - $files = [\CRM_Afform_AfformScanner::METADATA_FILE, \CRM_Afform_AfformScanner::LAYOUT_FILE]; - - foreach ($result as $afform) { - foreach ($files as $file) { - $metaPath = $scanner->createSiteLocalPath($afform['name'], $file); - if (file_exists($metaPath)) { - if (!@unlink($metaPath)) { - throw new \API_Exception("Failed to remove afform overrides in $file"); - } - } - } - } - - // We may have changed list of files covered by the cache. - $scanner->clear(); - - // FIXME if `server_route` changes, then flush the menu cache. - // FIXME if asset-caching is enabled, then flush the asset cache - } - -} diff --git a/ext/afform/core/Civi/Api4/Action/Afform/Update.php b/ext/afform/core/Civi/Api4/Action/Afform/Update.php deleted file mode 100644 index f89f23e0c3..0000000000 --- a/ext/afform/core/Civi/Api4/Action/Afform/Update.php +++ /dev/null @@ -1,13 +0,0 @@ -getWhere(); + if (count($where) === 1 && $where[0][0] === 'name' && $where[0][1] == '=') { + $names = [$where[0][2]]; + } + else { + $names = array_keys($scanner->findFilePaths()); + } + + $values = []; + foreach ($names as $name) { + $record = $scanner->getMeta($name); + $layout = $scanner->findFilePath($name, 'aff.html'); + if ($layout) { + // FIXME check for file existence+substance+validity + $record['layout'] = $converter->convertHtmlToArray(file_get_contents($layout)); + } + $values[] = $record; + } + + return $values; + }); } /** - * @return \Civi\Api4\Action\Afform\Revert + * @return BasicBatchAction */ public static function revert() { - return new Revert('Afform'); + return new BasicBatchAction('Afform', __FUNCTION__, ['name'], function($item, BasicBatchAction $action) { + $scanner = \Civi::service('afform_scanner'); + $files = [ + \CRM_Afform_AfformScanner::METADATA_FILE, + \CRM_Afform_AfformScanner::LAYOUT_FILE + ]; + + foreach ($files as $file) { + $metaPath = $scanner->createSiteLocalPath($item['name'], $file); + if (file_exists($metaPath)) { + if (!@unlink($metaPath)) { + throw new \API_Exception("Failed to remove afform overrides in $file"); + } + } + } + + // We may have changed list of files covered by the cache. + $scanner->clear(); + + // FIXME if `server_route` changes, then flush the menu cache. + // FIXME if asset-caching is enabled, then flush the asset cache + + return $item; + }); } /** - * @return \Civi\Api4\Action\Afform\Update + * @return BasicUpdateAction */ public static function update() { - return new Update('Afform'); - } + $save = function ($item, BasicUpdateAction $action) { + /** @var \CRM_Afform_AfformScanner $scanner */ + $scanner = \Civi::service('afform_scanner'); + $converter = new \CRM_Afform_ArrayHtml(); - // /** - // * @return \Civi\Api4\Action\Afform\Create - // */ - // public static function create() { - // return new Create('Afform'); - // } - - // /** - // * @return \Civi\Api4\Action\Afform\Delete - // */ - // public static function delete() { - // return new Delete('Afform'); - // } + if (empty($item['name']) || !preg_match('/^[a-zA-Z][a-zA-Z0-9\-]*$/', $item['name'])) { + throw new \API_Exception("Afform.create: name is a mandatory field. It should use alphanumerics and dashes."); + } + $name = $item['name']; + + // FIXME validate all field data. + $updates = _afform_fields_filter($item); + + // Create or update aff.html. + if (isset($updates['layout'])) { + $layoutPath = $scanner->createSiteLocalPath($name, 'aff.html'); + \ CRM_Utils_File::createDir(dirname($layoutPath)); + file_put_contents($layoutPath, $converter->convertArrayToHtml($updates['layout'])); + // FIXME check for writability then success. Report errors. + } + + // Create or update *.aff.json. + $orig = \Civi\Api4\Afform::get() + ->setCheckPermissions($action->getCheckPermissions()) + ->addWhere('name', '=', $name) + ->execute(); + + if (isset($orig[0])) { + $meta = _afform_fields_filter(array_merge($orig[0], $updates)); + } + else { + $meta = $updates; + } + unset($meta['layout']); + unset($meta['name']); + if (!empty($meta)) { + $metaPath = $scanner->createSiteLocalPath($name, \CRM_Afform_AfformScanner::METADATA_FILE); + // printf("[%s] Update meta %s: %s\n", $name, $metaPath, print_R(['updates'=>$updates, 'meta'=>$meta], 1)); + \CRM_Utils_File::createDir(dirname($metaPath)); + file_put_contents($metaPath, json_encode($meta, JSON_PRETTY_PRINT)); + // FIXME check for writability then success. Report errors. + } + + // We may have changed list of files covered by the cache. + $scanner->clear(); + + // FIXME if `server_route` changes, then flush the menu cache. + // FIXME if asset-caching is enabled, then flush the asset cache. + + return $updates; + }; + return new BasicUpdateAction('Afform', __FUNCTION__, $save, 'name'); + } /** * @return array diff --git a/ext/afform/core/Civi/Api4/Traits/AfformCrudTrait.php b/ext/afform/core/Civi/Api4/Traits/AfformCrudTrait.php deleted file mode 100644 index 6360c9f05d..0000000000 --- a/ext/afform/core/Civi/Api4/Traits/AfformCrudTrait.php +++ /dev/null @@ -1,98 +0,0 @@ -where) === 1 && $this->where[0][0] === 'name' && $this->where[0][1] == '=') { - $names = [$this->where[0][2]]; - } - else { - $names = array_keys($scanner->findFilePaths()); - } - - $values = []; - foreach ($names as $name) { - $record = $scanner->getMeta($name); - $layout = $scanner->findFilePath($name, 'aff.html'); - if ($layout) { - // FIXME check for file existence+substance+validity - $record['layout'] = $converter->convertHtmlToArray(file_get_contents($layout)); - } - $values[] = $record; - } - - return $this->processArrayData($values); - } - - /** - * Write a record as part of a create/update action. - * - * @param array $record - * The record to write to the DB. - * @return array - * The record after being written to the DB (e.g. including newly assigned "id"). - * @throws \API_Exception - */ - protected function writeObject($record) { - /** @var \CRM_Afform_AfformScanner $scanner */ - $scanner = \Civi::service('afform_scanner'); - $converter = new \CRM_Afform_ArrayHtml(); - - if (empty($record['name']) || !preg_match('/^[a-zA-Z][a-zA-Z0-9\-]*$/', $record['name'])) { - throw new \API_Exception("Afform.create: name is a mandatory field. It should use alphanumerics and dashes."); - } - $name = $record['name']; - - // FIXME validate all field data. - $updates = _afform_fields_filter($record); - - // Create or update aff.html. - if (isset($updates['layout'])) { - $layoutPath = $scanner->createSiteLocalPath($name, 'aff.html'); - \ CRM_Utils_File::createDir(dirname($layoutPath)); - file_put_contents($layoutPath, $converter->convertArrayToHtml($updates['layout'])); - // FIXME check for writability then success. Report errors. - } - - // Create or update *.aff.json. - $orig = \Civi\Api4\Afform::get() - ->setCheckPermissions($this->getCheckPermissions()) - ->addWhere('name', '=', $name) - ->execute(); - - if (isset($orig[0])) { - $meta = _afform_fields_filter(array_merge($orig[0], $updates)); - } - else { - $meta = $updates; - } - unset($meta['layout']); - unset($meta['name']); - if (!empty($meta)) { - $metaPath = $scanner->createSiteLocalPath($name, \CRM_Afform_AfformScanner::METADATA_FILE); - // printf("[%s] Update meta %s: %s\n", $name, $metaPath, print_R(['updates'=>$updates, 'meta'=>$meta], 1)); - \CRM_Utils_File::createDir(dirname($metaPath)); - file_put_contents($metaPath, json_encode($meta, JSON_PRETTY_PRINT)); - // FIXME check for writability then success. Report errors. - } - - // We may have changed list of files covered by the cache. - $scanner->clear(); - - // FIXME if `server_route` changes, then flush the menu cache. - // FIXME if asset-caching is enabled, then flush the asset cache. - - return $updates; - } - -} -- 2.25.1