| | |
| | | |
| | | trait Admin { |
| | | use _Base; |
| | | protected bool $canTest = false; |
| | | //An array of AdminAction objects |
| | | protected array $actions = []; |
| | | protected array $instructions = []; |
| | |
| | | 'label' => 'Save Changes', |
| | | 'icon' => 'floppy-disk', |
| | | 'action' => [$this, 'saveCredentials'] |
| | | ], |
| | | [ |
| | | 'name' => 'Clear Credentials', |
| | | 'icon' => 'broom', |
| | | 'action' => [$this, 'clearCredentials'] |
| | | ], |
| | | ] |
| | | ]; |
| | | if ($this->isSetUp()) { |
| | | $actions[] = [ |
| | | 'name' => 'Clear Credentials', |
| | | 'label' => 'Clear Credentials', |
| | | 'icon' => 'broom', |
| | | 'action' => [$this, 'deleteCredentials'] |
| | | ]; |
| | | } |
| | | if ($this->canTest) { |
| | | $actions[] = [ |
| | | 'name' => 'Test Connection', |
| | | 'label' => 'Test Connection', |
| | | 'icon' => 'plugs-connected', |
| | | 'action' => [$this, 'testConnectionAction'] |
| | | ]; |
| | | } |
| | | foreach ($actions as $a) { |
| | | $this->addAction($a['name'], $a['action'], $a['slug']??null, $a['icon']??null, $a['label']??null); |
| | | } |
| | | |
| | | $this->defineActions(); |
| | | } |
| | | |
| | | abstract protected function defineActions():void; |
| | | |
| | | public function addAction(string $name, callable $callback, ?string $slug = null, ?string $icon = null, ?string $label = null):void |
| | | { |
| | | $action = new AdminActions($name, $callback); |
| | |
| | | return $result; |
| | | } |
| | | |
| | | public function response(bool $success, string $message):array |
| | | { |
| | | return [ |
| | | 'success' => $success, |
| | | 'message' => $message |
| | | ]; |
| | | } |
| | | |
| | | public function renderConnection():string |
| | | public function renderConnection(bool $return = false):string |
| | | { |
| | | if ($this->userID && !JVB()->userCanConnect($this->service_name, $this->userID)) { |
| | | if ($this->userID && !$this->userCanConnect($this->userID)) { |
| | | return ''; |
| | | } |
| | | |
| | |
| | | $isConnected = !empty($credentials); |
| | | |
| | | $meta = ''; |
| | | if ($isConnected && array_key_exists('updated_at', $credentials) && $credentials['updated_at'] > 0) { |
| | | $meta = sprintf( |
| | | '<div class="meta"> |
| | | <small>Last updated: %s ago</small> |
| | | </div>', |
| | | human_time_diff($credentials['updated_at']) |
| | | ); |
| | | if ($isConnected) { |
| | | $updated = Auth::getInstance()->getLastUpdated($this->service_name, $this->userID); |
| | | $meta = []; |
| | | if ($updated) { |
| | | $meta[] = sprintf( |
| | | '<small>Last updated: %s ago</small>', |
| | | human_time_diff(strtotime($updated)) |
| | | ); |
| | | } |
| | | $tested = Auth::getInstance()->getLastTested($this->service_name, $this->userID); |
| | | if ($tested && $tested !== '0000-00-00 00:00:00') { |
| | | $meta[] = sprintf( |
| | | '<small>Last tested: %s ago</small>', |
| | | human_time_diff(strtotime($tested)) |
| | | ); |
| | | } |
| | | if (!empty($meta)) { |
| | | $meta = sprintf( |
| | | '<div class="meta">%s</div>', |
| | | implode('<br>', $meta) |
| | | ); |
| | | } |
| | | } |
| | | |
| | | $form = sprintf( |
| | |
| | | $meta, |
| | | ); |
| | | |
| | | if (!empty($this->instructions)) { |
| | | if (!$this->isSetUp() && !empty($this->instructions)) { |
| | | $form .= sprintf( |
| | | '<details%s> |
| | | <summary>Instructions</summary> |
| | |
| | | </details>', |
| | | $isConnected ? '' : ' open', |
| | | implode('',array_map(function($el) { |
| | | if (is_array($el)) { |
| | | $bits = implode('', array_map(function ($b) { |
| | | return sprintf('<li>%s</li>', $b); |
| | | }, $el)); |
| | | return sprintf('<ol>%s</ol>', $bits); |
| | | } |
| | | return sprintf('<li>%s</li>', $el); |
| | | }, $this->instructions)) |
| | | ); |
| | | } |
| | | |
| | | $setupFields = array_filter($this->fields, function($field) { |
| | | return $field->isSetupField(); |
| | | }); |
| | | |
| | | if (current_user_can('manage_options')) { |
| | | if ((method_exists($this, 'userCanConnect') && $this->userCanConnect(get_current_user_id())) || current_user_can('manage_options')) { |
| | | $form .= sprintf( |
| | | '<details class="initial-setup"%s> |
| | | <summary>Setup</summary> |
| | |
| | | %s |
| | | </details>', |
| | | $isConnected ? '' : ' open', |
| | | $this->outputFields($this->credentialFields), |
| | | $this->outputFields($setupFields), |
| | | method_exists($this, 'renderWebhookUrl') ? $this->renderWebhookUrl() : '' |
| | | ); |
| | | } |
| | |
| | | $form .= $this->renderOAuthConnectionForm(); |
| | | } |
| | | |
| | | if (!empty($this->optionalFields) || $this->hasExtraOptions) { |
| | | $form .= '<div class="integration-content">'; |
| | | if (!empty($this->optionalFields)) { |
| | | $form .= sprintf( |
| | | $advancedFields = array_filter($this->fields, function($field) { |
| | | return $field->isAdvancedField(); |
| | | }); |
| | | |
| | | if (!empty($advancedFields) && $this->isSetUp()) { |
| | | $form .= sprintf( |
| | | '<details class="advanced"> |
| | | <summary>Advanced Settings</summary>%s</details>', |
| | | $this->outputFields($this->optionalFields) |
| | | <summary>Advanced Options</summary> |
| | | %s |
| | | </details>', |
| | | $this->outputFields($advancedFields), |
| | | ); |
| | | } |
| | | |
| | | $optionalFields = array_filter($this->fields, function($field) { |
| | | return $field->isAdditionalField(); |
| | | }); |
| | | |
| | | if ($this->isSetUp() && (!empty($optionalFields) || $this->hasExtraOptions())) { |
| | | $form .= '<div class="integration-content">'; |
| | | if (!empty($optionalFields)) { |
| | | $form .= sprintf( |
| | | '<details class="advanced"%s> |
| | | <summary>Optional Settings</summary>%s</details>', |
| | | $this->isSetUp() ? ' open' : '', |
| | | $this->outputFields($optionalFields) |
| | | ); |
| | | } |
| | | if ($this->hasExtraOptions) { |
| | | if ($this->hasExtraOptions()) { |
| | | $form .= sprintf( |
| | | '<a href="%s" class="btn">More Settings</a>', |
| | | $this->determineOptionPage() |
| | |
| | | $form .= $this->outputActionButtons(); |
| | | $form .= '</form>'; |
| | | |
| | | if (!$return) { |
| | | echo $form; |
| | | } |
| | | |
| | | return $form; |
| | | } |
| | | protected function outputActionButtons():string |
| | |
| | | $label .= $action->getLabel(); |
| | | return sprintf( |
| | | '<button type="button" data-action="%s"%s%s>%s</button>', |
| | | $action->getSlug, |
| | | $action->getSlug(), |
| | | $action->getName(), |
| | | empty($action->getConfirm()) ? '' : ' data-confirm="'.$action->getConfirm().'"', |
| | | $label |
| | |
| | | if (!empty($value)) { |
| | | $config['readonly'] = true; |
| | | } |
| | | if (!empty($value) && $f->isHideStored()) { |
| | | return sprintf( |
| | | '<span class="label">%s</span><code>%s</code>', |
| | | $config['label'], |
| | | preg_replace('/.(?=.{4})/', '*', $value) |
| | | ); |
| | | } |
| | | $config['base'] = $this->service_name.'_'; |
| | | return Form::render($f->name, $value, $f->getConfig()); |
| | | }, $fields)); |
| | |
| | | $this->fields[$name]->setRequired(); |
| | | return true; |
| | | } |
| | | public function updateField(string $name, Field $field):void |
| | | { |
| | | $this->fields[$name] = $field; |
| | | } |
| | | |
| | | public function testConnectionAction(?array $data = null):array |
| | | { |
| | | $success = $this->testConnection(); |
| | | $message = $success ? 'Successfully tested connection' : 'Could not test connection'; |
| | | return $this->response($success, $message); |
| | | } |
| | | |
| | | } |