|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Class SuiteCRM_Addon |
| 4 | + * |
| 5 | + * @package formsbridge |
| 6 | + */ |
| 7 | + |
| 8 | +namespace FORMS_BRIDGE; |
| 9 | + |
| 10 | +if ( ! defined( 'ABSPATH' ) ) { |
| 11 | + exit(); |
| 12 | +} |
| 13 | + |
| 14 | +require_once 'class-suitecrm-form-bridge.php'; |
| 15 | +require_once 'hooks.php'; |
| 16 | + |
| 17 | +/** |
| 18 | + * SuiteCRM Addon class. |
| 19 | + * |
| 20 | + * Provides integration with SuiteCRM v4_1 REST API. |
| 21 | + */ |
| 22 | +class SuiteCRM_Addon extends Addon { |
| 23 | + |
| 24 | + /** |
| 25 | + * Holds the addon's title. |
| 26 | + * |
| 27 | + * @var string |
| 28 | + */ |
| 29 | + public const TITLE = 'SuiteCRM'; |
| 30 | + |
| 31 | + /** |
| 32 | + * Holds the addon's name. |
| 33 | + * |
| 34 | + * @var string |
| 35 | + */ |
| 36 | + public const NAME = 'suitecrm'; |
| 37 | + |
| 38 | + /** |
| 39 | + * Holds the addon's custom bridge class. |
| 40 | + * |
| 41 | + * @var string |
| 42 | + */ |
| 43 | + public const BRIDGE = '\FORMS_BRIDGE\SuiteCRM_Form_Bridge'; |
| 44 | + |
| 45 | + /** |
| 46 | + * Performs a request against the backend to check the connexion status. |
| 47 | + * |
| 48 | + * @param string $backend Target backend name. |
| 49 | + * |
| 50 | + * @return boolean |
| 51 | + */ |
| 52 | + public function ping( $backend ) { |
| 53 | + $bridge = new SuiteCRM_Form_Bridge( |
| 54 | + array( |
| 55 | + 'name' => '__suitecrm-' . time(), |
| 56 | + 'method' => 'get_user_id', |
| 57 | + 'endpoint' => '', |
| 58 | + 'backend' => $backend, |
| 59 | + ) |
| 60 | + ); |
| 61 | + |
| 62 | + $response = $bridge->submit(); |
| 63 | + |
| 64 | + if ( is_wp_error( $response ) ) { |
| 65 | + Logger::log( 'SuiteCRM backend ping error response', Logger::ERROR ); |
| 66 | + Logger::log( $response, Logger::ERROR ); |
| 67 | + return false; |
| 68 | + } |
| 69 | + |
| 70 | + return true; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Performs a GET request against the backend module and retrieve the response data. |
| 75 | + * |
| 76 | + * @param string $endpoint Target module name. |
| 77 | + * @param string $backend Target backend name. |
| 78 | + * |
| 79 | + * @return array|WP_Error |
| 80 | + */ |
| 81 | + public function fetch( $endpoint, $backend ) { |
| 82 | + $bridge = new SuiteCRM_Form_Bridge( |
| 83 | + array( |
| 84 | + 'name' => '__suitecrm-' . time(), |
| 85 | + 'method' => 'get_entry_list', |
| 86 | + 'endpoint' => $endpoint, |
| 87 | + 'backend' => $backend, |
| 88 | + ) |
| 89 | + ); |
| 90 | + |
| 91 | + return $bridge->submit( |
| 92 | + array( |
| 93 | + 'select_fields' => array( 'id', 'name' ), |
| 94 | + 'max_results' => 100, |
| 95 | + ) |
| 96 | + ); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Fetch available modules from the backend. |
| 101 | + * |
| 102 | + * @param Backend $backend HTTP backend object. |
| 103 | + * |
| 104 | + * @return array |
| 105 | + */ |
| 106 | + public function get_endpoints( $backend ) { |
| 107 | + $bridge = new SuiteCRM_Form_Bridge( |
| 108 | + array( |
| 109 | + 'name' => '__suitecrm-' . time(), |
| 110 | + 'method' => 'get_available_modules', |
| 111 | + 'endpoint' => '', |
| 112 | + 'backend' => $backend, |
| 113 | + ) |
| 114 | + ); |
| 115 | + |
| 116 | + $response = $bridge->submit(); |
| 117 | + |
| 118 | + if ( is_wp_error( $response ) ) { |
| 119 | + return array(); |
| 120 | + } |
| 121 | + |
| 122 | + if ( ! isset( $response['data']['modules'] ) ) { |
| 123 | + return array(); |
| 124 | + } |
| 125 | + |
| 126 | + return array_map( |
| 127 | + function ( $module ) { |
| 128 | + return $module['module_key']; |
| 129 | + }, |
| 130 | + $response['data']['modules'] |
| 131 | + ); |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * Performs an introspection of the backend module and returns API fields |
| 136 | + * and accepted content type. |
| 137 | + * |
| 138 | + * @param string $module Target module name. |
| 139 | + * @param string $backend Target backend name. |
| 140 | + * @param string|null $method API method. |
| 141 | + * |
| 142 | + * @return array List of fields and content type of the module. |
| 143 | + */ |
| 144 | + public function get_endpoint_schema( $module, $backend, $method = null ) { |
| 145 | + if ( 'set_entry' !== $method ) { |
| 146 | + return array(); |
| 147 | + } |
| 148 | + |
| 149 | + $bridge = new SuiteCRM_Form_Bridge( |
| 150 | + array( |
| 151 | + 'name' => '__suitecrm-' . time(), |
| 152 | + 'method' => 'get_module_fields', |
| 153 | + 'endpoint' => $module, |
| 154 | + 'backend' => $backend, |
| 155 | + ) |
| 156 | + ); |
| 157 | + |
| 158 | + $response = $bridge->submit(); |
| 159 | + |
| 160 | + if ( is_wp_error( $response ) ) { |
| 161 | + return array(); |
| 162 | + } |
| 163 | + |
| 164 | + if ( ! isset( $response['data']['module_fields'] ) ) { |
| 165 | + return array(); |
| 166 | + } |
| 167 | + |
| 168 | + $fields = array(); |
| 169 | + foreach ( $response['data']['module_fields'] as $name => $spec ) { |
| 170 | + $type = 'string'; |
| 171 | + |
| 172 | + if ( in_array( $spec['type'], array( 'int', 'integer' ), true ) ) { |
| 173 | + $type = 'integer'; |
| 174 | + } elseif ( in_array( $spec['type'], array( 'decimal', 'float', 'currency' ), true ) ) { |
| 175 | + $type = 'number'; |
| 176 | + } elseif ( 'bool' === $spec['type'] ) { |
| 177 | + $type = 'boolean'; |
| 178 | + } |
| 179 | + |
| 180 | + $schema = array( |
| 181 | + 'type' => $type, |
| 182 | + 'required' => ! empty( $spec['required'] ), |
| 183 | + ); |
| 184 | + |
| 185 | + $fields[] = array( |
| 186 | + 'name' => $name, |
| 187 | + 'schema' => $schema, |
| 188 | + ); |
| 189 | + } |
| 190 | + |
| 191 | + return $fields; |
| 192 | + } |
| 193 | +} |
| 194 | + |
| 195 | +SuiteCRM_Addon::setup(); |
0 commit comments