FapiFormWidget.php
112 lines
| 1 | <?php |
| 2 | |
| 3 | namespace FapiMember\Elementor\Widgets; |
| 4 | |
| 5 | use Elementor\Controls_Manager; |
| 6 | use Elementor\Widget_Base; |
| 7 | |
| 8 | final class FapiFormWidget extends Widget_Base { |
| 9 | |
| 10 | /** |
| 11 | * @var array<mixed>|null |
| 12 | */ |
| 13 | private array|null $formOptions = null; |
| 14 | |
| 15 | public function get_name() { |
| 16 | return 'fapi_form'; |
| 17 | } |
| 18 | |
| 19 | public function get_title() { |
| 20 | return esc_html__( 'FAPI form', 'fapi-member' ); |
| 21 | } |
| 22 | |
| 23 | public function get_icon() { |
| 24 | return 'eicon-cart'; |
| 25 | } |
| 26 | |
| 27 | public function get_custom_help_url() { |
| 28 | return 'https://fapi.cz'; |
| 29 | } |
| 30 | |
| 31 | public function get_categories() { |
| 32 | return array( 'fapi' ); |
| 33 | } |
| 34 | |
| 35 | public function get_keywords() { |
| 36 | return array( 'fapi', 'sale', 'form' ); |
| 37 | } |
| 38 | |
| 39 | protected function register_controls() { |
| 40 | $this->start_controls_section( |
| 41 | 'content_section', |
| 42 | array( |
| 43 | 'label' => esc_html__( 'FAPI', 'fapi-member' ), |
| 44 | 'tab' => Controls_Manager::TAB_CONTENT, |
| 45 | ) |
| 46 | ); |
| 47 | |
| 48 | $this->add_control( |
| 49 | 'path', |
| 50 | array( |
| 51 | 'type' => Controls_Manager::SELECT, |
| 52 | 'label' => esc_html__( 'Prodejní formulář', 'fapi-member' ), |
| 53 | 'options' => $this->getFormOptions(), |
| 54 | 'default' => '', |
| 55 | 'prefix_class' => 'form-control', |
| 56 | ) |
| 57 | ); |
| 58 | |
| 59 | $this->end_controls_section(); |
| 60 | |
| 61 | } |
| 62 | |
| 63 | protected function render() { |
| 64 | $settings = $this->get_settings_for_display(); |
| 65 | |
| 66 | echo sprintf( '<script type="text/javascript" src="https://form.fapi.cz/script.php?id=%s"></script>', $settings['path'] ); |
| 67 | } |
| 68 | |
| 69 | protected function content_template() { |
| 70 | echo '<div style="text-align: center"> <<< Zde bude prodejní formulář >>> </div>'; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * @return array<mixed> |
| 75 | */ |
| 76 | private function getFormOptions() { |
| 77 | if ( $this->formOptions !== null ) { |
| 78 | return $this->formOptions; |
| 79 | } |
| 80 | |
| 81 | global $FapiPlugin; |
| 82 | global $apiService; |
| 83 | |
| 84 | $allClientsForms = array(); |
| 85 | $clients = $apiService->getApiClients(); |
| 86 | |
| 87 | foreach ($clients as $client) { |
| 88 | $allClientsForms[$client->getConnection()->getApiUser()] = $client->getForms(); |
| 89 | } |
| 90 | |
| 91 | $this->formOptions = array( |
| 92 | '' => esc_html__( |
| 93 | '-- vyberte prodejní formulář --', |
| 94 | 'fapi-member' |
| 95 | ), |
| 96 | ); |
| 97 | |
| 98 | if ( $allClientsForms === false || empty( $allClientsForms ) ) { |
| 99 | return $this->formOptions; |
| 100 | } |
| 101 | |
| 102 | foreach ($allClientsForms as $client => $clientForms) { |
| 103 | foreach ($clientForms as $form) { |
| 104 | $this->formOptions[ $form['path'] ] = $form['name'] . sprintf( ' (%s)', $client ); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | return $this->formOptions; |
| 109 | } |
| 110 | |
| 111 | } |
| 112 |