add-contact.php
2 months ago
add-email-to-sequence.php
10 months ago
add-event.php
10 months ago
add-lists-to-contact.php
8 months ago
add-note-to-company.php
8 months ago
add-note.php
5 months ago
add-tag-to-contact.php
10 months ago
attach-subscribers-company.php
10 months ago
create-campaign.php
4 months ago
create-company.php
10 months ago
create-email-template.php
8 months ago
create-list.php
8 months ago
create-sequence.php
8 months ago
create-tag.php
2 weeks ago
delete-campaign.php
8 months ago
delete-company.php
8 months ago
delete-list.php
8 months ago
delete-sequence.php
8 months ago
delete-tag.php
8 months ago
detach-subscribers-company.php
10 months ago
get-all-lists.php
8 months ago
get-all-tags.php
8 months ago
get-company-notes.php
8 months ago
get-contact-notes.php
8 months ago
get-contacts-who-clicked-email.php
8 months ago
get-contacts-who-did-not-open-email.php
8 months ago
get-contacts-who-opened-email.php
8 months ago
get-contacts-who-unsubscribed-email.php
8 months ago
list-contacts-by-list-id.php
7 months ago
list-email-templates.php
10 months ago
list-sequences.php
10 months ago
remove-contact.php
10 months ago
remove-subscriber-from-sequence.php
8 months ago
remove-tag-from-contact.php
10 months ago
retrieve-contact-by-email.php
10 months ago
retrieve-contact-by-id.php
10 months ago
retrieve-contact-by-list-ids.php
10 months ago
retrieve-contact-by-segment.php
10 months ago
retrieve-contact-by-status.php
10 months ago
retrieve-contact-by-tag-ids.php
10 months ago
retrieve-contact-by-user-id.php
10 months ago
send-email-by-campaign.php
4 months ago
send-email-by-template.php
10 months ago
send-email-to-list-contacts.php
4 months ago
update-contact-status.php
8 months ago
update-template.php
10 months ago
create-company.php
223 lines
| 1 | <?php |
| 2 | /** |
| 3 | * CreateCompany. |
| 4 | * php version 5.6 |
| 5 | * |
| 6 | * @category CreateCompany |
| 7 | * @package SureTriggers |
| 8 | * @author BSF <username@example.com> |
| 9 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3 |
| 10 | * @link https://www.brainstormforce.com/ |
| 11 | * @since 1.0.0 |
| 12 | */ |
| 13 | |
| 14 | namespace SureTriggers\Integrations\FluentCRM\Actions; |
| 15 | |
| 16 | use Exception; |
| 17 | use DateTime; |
| 18 | use SureTriggers\Integrations\AutomateAction; |
| 19 | use SureTriggers\Traits\SingletonLoader; |
| 20 | use FluentCrm\App\Services\Helper; |
| 21 | |
| 22 | /** |
| 23 | * CreateCompany |
| 24 | * |
| 25 | * @category CreateCompany |
| 26 | * @package SureTriggers |
| 27 | * @author BSF <username@example.com> |
| 28 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3 |
| 29 | * @link https://www.brainstormforce.com/ |
| 30 | * @since 1.0.0 |
| 31 | */ |
| 32 | class CreateCompany extends AutomateAction { |
| 33 | |
| 34 | |
| 35 | /** |
| 36 | * Integration type. |
| 37 | * |
| 38 | * @var string |
| 39 | */ |
| 40 | public $integration = 'FluentCRM'; |
| 41 | |
| 42 | /** |
| 43 | * Action name. |
| 44 | * |
| 45 | * @var string |
| 46 | */ |
| 47 | public $action = 'fluentcrm_create_company'; |
| 48 | |
| 49 | use SingletonLoader; |
| 50 | |
| 51 | /** |
| 52 | * Register a action. |
| 53 | * |
| 54 | * @param array $actions actions. |
| 55 | * @return array |
| 56 | */ |
| 57 | public function register( $actions ) { |
| 58 | |
| 59 | $actions[ $this->integration ][ $this->action ] = [ |
| 60 | 'label' => __( 'Create Company', 'suretriggers' ), |
| 61 | 'action' => $this->action, |
| 62 | 'function' => [ $this, 'action_listener' ], |
| 63 | ]; |
| 64 | return $actions; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Action listener. |
| 69 | * |
| 70 | * @param int $user_id user_id. |
| 71 | * @param int $automation_id automation_id. |
| 72 | * @param array $fields fields. |
| 73 | * @param array $selected_options selectedOptions. |
| 74 | * |
| 75 | * @return array|void |
| 76 | * |
| 77 | * @throws Exception Exception. |
| 78 | */ |
| 79 | public function _action_listener( $user_id, $automation_id, $fields, $selected_options ) { |
| 80 | |
| 81 | if ( ! class_exists( 'FluentCrm\App\Services\Helper' ) || ! function_exists( 'FluentCrmApi' ) ) { |
| 82 | return [ |
| 83 | 'status' => 'error', |
| 84 | 'message' => __( 'Required functions not found.', 'suretriggers' ), |
| 85 | |
| 86 | ]; |
| 87 | } |
| 88 | |
| 89 | // Check if company module is enabled. |
| 90 | $is_company_enabled = Helper::isCompanyEnabled(); |
| 91 | if ( ! $is_company_enabled ) { |
| 92 | return [ |
| 93 | 'status' => 'error', |
| 94 | 'message' => __( 'Company module disabled. You can add companies and assign contacts to companies only when it is enabled!!', 'suretriggers' ), |
| 95 | |
| 96 | ]; |
| 97 | } |
| 98 | |
| 99 | if ( '' != $selected_options['company_email'] && ! is_email( $selected_options['company_email'] ) ) { |
| 100 | return [ |
| 101 | 'status' => 'error', |
| 102 | 'message' => __( 'Email address is invalid.', 'suretriggers' ), |
| 103 | |
| 104 | ]; |
| 105 | } |
| 106 | |
| 107 | $data = [ |
| 108 | 'email' => trim( $selected_options['company_email'] ), |
| 109 | ]; |
| 110 | |
| 111 | $data['name'] = $selected_options['company_name']; |
| 112 | $data['description'] = $selected_options['company_description']; |
| 113 | $data['address_line_1'] = $selected_options['address_line_1']; |
| 114 | $data['address_line_2'] = $selected_options['address_line_2']; |
| 115 | $data['city'] = $selected_options['city']; |
| 116 | $data['state'] = $selected_options['state']; |
| 117 | $data['postal_code'] = $selected_options['postal_code']; |
| 118 | $data['country'] = $selected_options['country']; |
| 119 | $data['phone'] = $selected_options['phone']; |
| 120 | $data['type'] = $selected_options['company_type']; |
| 121 | $data['owner_id'] = $selected_options['company_owner_id']; |
| 122 | $data['employees_number'] = $selected_options['company_employee_count']; |
| 123 | $data['industry'] = $selected_options['company_industry']; |
| 124 | $data['website'] = $selected_options['company_website']; |
| 125 | $data['linkedin_url'] = $selected_options['company_linkedin_url']; |
| 126 | $data['facebook_url'] = $selected_options['company_facebook_url']; |
| 127 | $data['twitter_url'] = $selected_options['company_twitter_url']; |
| 128 | |
| 129 | if ( isset( $selected_options['show_custom_fields'] ) |
| 130 | && in_array( $selected_options['show_custom_fields'], [ true, 1, 'true', '1' ], true ) && function_exists( 'fluentcrm_get_custom_company_fields' ) ) { |
| 131 | $fcrm_custom_fields = fluentcrm_get_custom_company_fields(); |
| 132 | foreach ( $selected_options['field_row_repeater'] as $key => $field ) { |
| 133 | $type = $fcrm_custom_fields[ $key ]['type']; |
| 134 | $label = $fcrm_custom_fields[ $key ]['label']; |
| 135 | $field_name = $field['value']['name']; |
| 136 | $value = trim( $selected_options['field_row'][ $key ][ $field_name ] ); |
| 137 | |
| 138 | if ( empty( $value ) ) { |
| 139 | continue; |
| 140 | } |
| 141 | |
| 142 | if ( in_array( $type, [ 'select-one', 'radio' ], true ) ) { |
| 143 | $field_options = $fcrm_custom_fields[ $key ]['options']; |
| 144 | $field_value = null; |
| 145 | |
| 146 | foreach ( $field_options as $option ) { |
| 147 | if ( strtolower( $value ) === strtolower( $option ) ) { |
| 148 | $field_value = $option; |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | if ( ! $field_value ) { |
| 153 | return [ |
| 154 | 'status' => 'error', |
| 155 | 'message' => sprintf( __( "The value '%1\$s' is not a valid option in the %2\$s field in FluentCRM.", 'suretriggers' ), $value, $label ), |
| 156 | ]; |
| 157 | } |
| 158 | |
| 159 | $data['custom_values'][ $field_name ] = $field_value; |
| 160 | |
| 161 | } elseif ( in_array( $type, [ 'select-multi', 'checkbox' ], true ) ) { |
| 162 | $option_values = explode( ',', $value ); |
| 163 | $option_values = array_map( 'trim', $option_values ); |
| 164 | $field_options = $fcrm_custom_fields[ $key ]['options']; |
| 165 | |
| 166 | $options = []; |
| 167 | foreach ( $option_values as $option_value ) { |
| 168 | $field_value = null; |
| 169 | |
| 170 | foreach ( $field_options as $option ) { |
| 171 | if ( strtolower( $option_value ) === strtolower( $option ) ) { |
| 172 | $field_value = $option; |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | if ( ! $field_value ) { |
| 177 | return [ |
| 178 | 'status' => 'error', |
| 179 | 'message' => sprintf( __( "The value '%1\$s' is not a valid option in the %2\$s field in FluentCRM.", 'suretriggers' ), $option_value, $label ), |
| 180 | ]; |
| 181 | } |
| 182 | |
| 183 | $options[] = $field_value; |
| 184 | } |
| 185 | |
| 186 | |
| 187 | $data['custom_values'][ $field_name ] = $options; |
| 188 | |
| 189 | } elseif ( 'date' === $type ) { |
| 190 | $date = DateTime::createFromFormat( 'Y-m-d', $value ); |
| 191 | if ( ! $date ) { |
| 192 | return [ |
| 193 | 'status' => 'error', |
| 194 | 'message' => sprintf( __( "The date format does not conform to the 'yyyy-mm-dd' format in %s field.", 'suretriggers' ), $label ), |
| 195 | ]; |
| 196 | } |
| 197 | |
| 198 | $data['custom_values'][ $field_name ] = $value; |
| 199 | } elseif ( 'date_time' === $type ) { |
| 200 | $date = DateTime::createFromFormat( 'Y-m-d H:i:s', $value ); |
| 201 | if ( ! $date ) { |
| 202 | return [ |
| 203 | 'status' => 'error', |
| 204 | 'message' => sprintf( __( "The datetime format does not conform to the 'yyyy-mm-dd hh:mm:ss' format in %s field.", 'suretriggers' ), $label ), |
| 205 | ]; |
| 206 | } |
| 207 | |
| 208 | $data['custom_values'][ $field_name ] = $value; |
| 209 | } else { |
| 210 | $data['custom_values'][ $field_name ] = $value; |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | $company = FluentCrmApi( 'companies' )->createOrUpdate( $data ); |
| 216 | |
| 217 | return $company; |
| 218 | } |
| 219 | |
| 220 | } |
| 221 | |
| 222 | CreateCompany::get_instance(); |
| 223 |