| 1 |
<?php |
| 2 |
|
| 3 |
namespace Booktics\Dummy_Data; |
| 4 |
|
| 5 |
use Booktics\Models\Service_Model; |
| 6 |
use ENS\Config; |
| 7 |
use ENS\Flow\Flow; |
| 8 |
use Exception; |
| 9 |
use stdClass; |
| 10 |
|
| 11 |
class Dummy_Data_Manager { |
| 12 |
private $generator; |
| 13 |
|
| 14 |
public function __construct() { |
| 15 |
$this->generator = new Generator(); |
| 16 |
} |
| 17 |
|
| 18 |
/** |
| 19 |
* Generate dummy data for onboarding |
| 20 |
*/ |
| 21 |
public function generate_onboarding_data( $service_count = 3 ) { |
| 22 |
$flow_result = $this->create_automation_flows(); |
| 23 |
$result = $this->create_services( $service_count ); |
| 24 |
if ( empty( $result['service_ids'] ) ) { |
| 25 |
return false; |
| 26 |
} |
| 27 |
|
| 28 |
return array( |
| 29 |
'flows' => $flow_result['flow_ids'], |
| 30 |
'services' => $result['service_ids'], |
| 31 |
'errors' => array_merge( $result['errors'], $flow_result['errors'] ), |
| 32 |
); |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Create fake services |
| 37 |
* |
| 38 |
* @param int $count Number of services to create |
| 39 |
* |
| 40 |
* @return array Created service IDs |
| 41 |
*/ |
| 42 |
public function create_services( int $count = 3 ): array { |
| 43 |
$result = array( |
| 44 |
'service_ids' => array(), |
| 45 |
'errors' => array(), |
| 46 |
); |
| 47 |
$current_services = Service_Model::paginate(); |
| 48 |
if ( $current_services['total'] > 0 ) { |
| 49 |
return $result; |
| 50 |
} |
| 51 |
|
| 52 |
$service_model = new \Booktics\Models\Service_Model(); |
| 53 |
$service_ids = array(); |
| 54 |
$service_types = array( |
| 55 |
'Consultation' => 30, |
| 56 |
'Check-up' => 60, |
| 57 |
'Therapy' => 45, |
| 58 |
'Examination' => 90, |
| 59 |
'Follow-up' => 20, |
| 60 |
'Massage' => 60, |
| 61 |
'Treatment' => 45, |
| 62 |
'Evaluation' => 30, |
| 63 |
); |
| 64 |
|
| 65 |
for ( $i = 0; $i < $count; $i++ ) { |
| 66 |
$service_name = $this->generator->random_element( array_keys( $service_types ) ); |
| 67 |
$duration = $service_types[ $service_name ]; |
| 68 |
$description = $this->generator->sentence( 7 ); |
| 69 |
$service_data = array( |
| 70 |
'title' => $service_name, |
| 71 |
'description' => $description, |
| 72 |
'content' => $description, |
| 73 |
'duration_time' => $duration, |
| 74 |
'duration_unit' => 'minutes', |
| 75 |
'charge_amount' => $this->generator->number_between( 50, 300 ), |
| 76 |
'status' => 'active', |
| 77 |
); |
| 78 |
|
| 79 |
try { |
| 80 |
$service_id = $service_model->create( $service_data ); |
| 81 |
if ( $service_id ) { |
| 82 |
$result['service_ids'][] = $service_id; |
| 83 |
} |
| 84 |
} catch ( Exception $e ) { |
| 85 |
$result['errors'][] = 'Failed to create service: ' . $e->getMessage(); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
return $result; |
| 90 |
} |
| 91 |
|
| 92 |
public function create_automation_flows() { |
| 93 |
$result = array( |
| 94 |
'flow_ids' => array(), |
| 95 |
'errors' => array(), |
| 96 |
); |
| 97 |
|
| 98 |
$automation_flows = array( |
| 99 |
array( |
| 100 |
'name' => 'Order Created', |
| 101 |
'trigger' => 'order_created', |
| 102 |
'status' => 'publish', |
| 103 |
'flow_config' => array( |
| 104 |
'nodes' => array( |
| 105 |
array( |
| 106 |
'id' => 'node_1', |
| 107 |
'type' => 'trigger', |
| 108 |
'name' => 'trigger', |
| 109 |
'data' => array( |
| 110 |
'label' => 'trigger: order_created', |
| 111 |
'subtitle' => 'Order Created', |
| 112 |
'triggerValue' => 'order_created', |
| 113 |
), |
| 114 |
'position' => array( |
| 115 |
'x' => 200, |
| 116 |
'y' => 50, |
| 117 |
), |
| 118 |
), |
| 119 |
array( |
| 120 |
'id' => 'node_2', |
| 121 |
'type' => 'end', |
| 122 |
'name' => 'end', |
| 123 |
'data' => array( |
| 124 |
'label' => 'end_flow', |
| 125 |
'subtitle' => 'End execution path', |
| 126 |
), |
| 127 |
'position' => array( |
| 128 |
'x' => 200, |
| 129 |
'y' => 410, |
| 130 |
), |
| 131 |
), |
| 132 |
array( |
| 133 |
'id' => 'node_3', |
| 134 |
'type' => 'action', |
| 135 |
'name' => 'email', |
| 136 |
'data' => array( |
| 137 |
'actionType' => 'send_email', |
| 138 |
'label' => 'send_email', |
| 139 |
'subtitle' => 'Subject: You just placed and order!', |
| 140 |
'receiverType' => 'user_email', |
| 141 |
'from_address' => '', |
| 142 |
'subject' => 'You are invited to join the team', |
| 143 |
'body' => '<p>Hi {{customer_name}}, </p><p>We are glad to let you know that your order placed successful.</p><p>Your order no: {{order_no}}</p>', |
| 144 |
), |
| 145 |
'position' => array( |
| 146 |
'x' => 200, |
| 147 |
'y' => 230, |
| 148 |
), |
| 149 |
), |
| 150 |
), |
| 151 |
'edges' => array( |
| 152 |
array( |
| 153 |
'id' => 'edge_node_1-node_3', |
| 154 |
'type' => 'smoothstep', |
| 155 |
'markerEnd' => array( |
| 156 |
'type' => 'arrowclosed', |
| 157 |
), |
| 158 |
'source' => 'node_1', |
| 159 |
'target' => 'node_3', |
| 160 |
'data' => new stdClass(), |
| 161 |
), |
| 162 |
array( |
| 163 |
'id' => 'edge_node_3-node_2', |
| 164 |
'type' => 'smoothstep', |
| 165 |
'markerEnd' => array( |
| 166 |
'type' => 'arrowclosed', |
| 167 |
), |
| 168 |
'source' => 'node_3', |
| 169 |
'target' => 'node_2', |
| 170 |
'data' => new stdClass(), |
| 171 |
), |
| 172 |
), |
| 173 |
), |
| 174 |
), |
| 175 |
array( |
| 176 |
'name' => 'Team Member Invitation', |
| 177 |
'trigger' => 'team_member_created', |
| 178 |
'status' => 'publish', |
| 179 |
'flow_config' => array( |
| 180 |
'nodes' => array( |
| 181 |
array( |
| 182 |
'id' => 'node_1', |
| 183 |
'type' => 'trigger', |
| 184 |
'name' => 'trigger', |
| 185 |
'data' => array( |
| 186 |
'label' => 'trigger: team_member_created', |
| 187 |
'subtitle' => 'Team Member Created', |
| 188 |
'triggerValue' => 'team_member_created', |
| 189 |
), |
| 190 |
'position' => array( |
| 191 |
'x' => 200, |
| 192 |
'y' => 50, |
| 193 |
), |
| 194 |
), |
| 195 |
array( |
| 196 |
'id' => 'node_2', |
| 197 |
'type' => 'end', |
| 198 |
'name' => 'end', |
| 199 |
'data' => array( |
| 200 |
'label' => 'end_flow', |
| 201 |
'subtitle' => 'End execution path', |
| 202 |
), |
| 203 |
'position' => array( |
| 204 |
'x' => 200, |
| 205 |
'y' => 410, |
| 206 |
), |
| 207 |
), |
| 208 |
array( |
| 209 |
'id' => 'node_3', |
| 210 |
'type' => 'action', |
| 211 |
'name' => 'email', |
| 212 |
'data' => array( |
| 213 |
'actionType' => 'send_email', |
| 214 |
'label' => 'send_email', |
| 215 |
'subtitle' => 'Subject: You are invited to join the team.', |
| 216 |
'receiverType' => 'team_member_email', |
| 217 |
'from_address' => '', |
| 218 |
'subject' => 'You are invited to join the team', |
| 219 |
'body' => '<p>Hi {{team_member_name}}, </p><p>We are glad to let you know that you are invited to join our team.</p><p>Please reset your password from the below link.</p><p><a href="{{password_reset_url}}" rel="noopener noreferrer" target="_blank">{{password_reset_url}}</a></p>', |
| 220 |
), |
| 221 |
'position' => array( |
| 222 |
'x' => 200, |
| 223 |
'y' => 230, |
| 224 |
), |
| 225 |
), |
| 226 |
), |
| 227 |
'edges' => array( |
| 228 |
array( |
| 229 |
'id' => 'edge_node_1-node_3', |
| 230 |
'type' => 'smoothstep', |
| 231 |
'markerEnd' => array( |
| 232 |
'type' => 'arrowclosed', |
| 233 |
), |
| 234 |
'source' => 'node_1', |
| 235 |
'target' => 'node_3', |
| 236 |
'data' => new stdClass(), |
| 237 |
), |
| 238 |
array( |
| 239 |
'id' => 'edge_node_3-node_2', |
| 240 |
'type' => 'smoothstep', |
| 241 |
'markerEnd' => array( |
| 242 |
'type' => 'arrowclosed', |
| 243 |
), |
| 244 |
'source' => 'node_3', |
| 245 |
'target' => 'node_2', |
| 246 |
'data' => new stdClass(), |
| 247 |
), |
| 248 |
), |
| 249 |
), |
| 250 |
), |
| 251 |
); |
| 252 |
try { |
| 253 |
Config::set( 'general_prefix', 'bt' ); |
| 254 |
$prefix_for_cpt = Config::get( 'general_prefix' ); |
| 255 |
$post_type = $prefix_for_cpt . '-flow'; |
| 256 |
foreach ( $automation_flows as $key => $automation_flow ) { |
| 257 |
$existing_flow = get_posts( |
| 258 |
array( |
| 259 |
'post_type' => $post_type, |
| 260 |
'meta_query' => array( |
| 261 |
array( |
| 262 |
'key' => '_bt_notification_flow_trigger', |
| 263 |
'value' => $automation_flow['trigger'], |
| 264 |
'compare' => '=', |
| 265 |
), |
| 266 |
), |
| 267 |
'numberposts' => 1, |
| 268 |
) |
| 269 |
); |
| 270 |
if ( ! empty( $existing_flow ) ) { |
| 271 |
continue; |
| 272 |
} |
| 273 |
|
| 274 |
$flow = new Flow( 0 ); |
| 275 |
$flow->set_props( $automation_flow ); |
| 276 |
$flow_id = $flow->save(); |
| 277 |
if ( $flow_id ) { |
| 278 |
$result['flow_ids'][] = $flow_id; |
| 279 |
} |
| 280 |
} |
| 281 |
} catch ( Exception $e ) { |
| 282 |
$result['errors'][] = 'Failed to create service: ' . $e->getMessage(); |
| 283 |
} |
| 284 |
|
| 285 |
return $result; |
| 286 |
} |
| 287 |
} |
| 288 |
|