| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentBoards\App\Services; |
| 4 |
|
| 5 |
use FluentBoards\App\Models\Activity; |
| 6 |
use FluentBoards\App\Models\Stage; |
| 7 |
use FluentBoards\App\Models\Webhook; |
| 8 |
use FluentBoards\Framework\Support\Arr; |
| 9 |
use FluentBoards\Framework\Support\Str; |
| 10 |
use FluentBoards\App\Models\Board; |
| 11 |
|
| 12 |
class Helper |
| 13 |
{ |
| 14 |
public static function snake_case($string) |
| 15 |
{ |
| 16 |
return strtolower(preg_replace('/(?<!^)[A-Z]/', '_$0', $string)); |
| 17 |
} |
| 18 |
|
| 19 |
public static function slugify($text, $id = '', $length = 20) |
| 20 |
{ |
| 21 |
$text = substr($text, 0, $length); // limiting to max 20 chars |
| 22 |
$text = Str::slug($text); |
| 23 |
if ($id) { |
| 24 |
$text = $id . '-' . $text; |
| 25 |
} |
| 26 |
|
| 27 |
return $text; |
| 28 |
} |
| 29 |
|
| 30 |
public static function loadView($template, $data) |
| 31 |
{ |
| 32 |
extract($data, EXTR_OVERWRITE); |
| 33 |
|
| 34 |
$template = sanitize_file_name($template); |
| 35 |
|
| 36 |
$template = str_replace('.', DIRECTORY_SEPARATOR, $template); |
| 37 |
|
| 38 |
ob_start(); |
| 39 |
include FLUENT_BOARDS_PLUGIN_PATH . 'app/Views/' . $template . '.php'; |
| 40 |
|
| 41 |
return ob_get_clean(); |
| 42 |
} |
| 43 |
|
| 44 |
private static function sanitizeData($data, $fieldMaps) |
| 45 |
{ |
| 46 |
foreach ($data as $key => $value) { |
| 47 |
if ($value && isset($fieldMaps[$key]) && !is_array($value)) { |
| 48 |
$data[$key] = call_user_func($fieldMaps[$key], $value); |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
return $data; |
| 53 |
} |
| 54 |
|
| 55 |
public static function sanitizeTask($data) |
| 56 |
{ |
| 57 |
$fieldMaps = [ |
| 58 |
'title' => 'sanitize_text_field', |
| 59 |
'board_id' => 'intval', |
| 60 |
'parent_id' => 'intval', |
| 61 |
'crm_contact_id' => 'intval', |
| 62 |
'task_type' => 'sanitize_text_field', |
| 63 |
'stage' => 'sanitize_text_field', |
| 64 |
'reminder_type' => 'sanitize_text_field', |
| 65 |
'priority' => 'sanitize_text_field', |
| 66 |
'lead_value' => 'doubleval', |
| 67 |
'remind_at' => 'sanitize_text_field', |
| 68 |
'scope' => 'sanitize_text_field', |
| 69 |
'source' => 'sanitize_text_field', |
| 70 |
'description' => 'wp_kses_post', |
| 71 |
'due_date' => 'sanitize_text_field', |
| 72 |
'start_at' => 'sanitize_text_field', |
| 73 |
'log_minutes' => 'sanitize_text_field', |
| 74 |
'last_completed' => 'sanitize_text_field', |
| 75 |
'assignees' => 'intval', |
| 76 |
'is_archived' => 'intval', |
| 77 |
'previous_stage' => 'sanitize_text_field', |
| 78 |
'new_stage' => 'sanitize_text_field', |
| 79 |
'new_index' => 'intval', |
| 80 |
'old_index' => 'intval', |
| 81 |
'new_board_id' => 'intval', |
| 82 |
'position' => 'intval' |
| 83 |
|
| 84 |
]; |
| 85 |
|
| 86 |
return self::sanitizeData($data, $fieldMaps); |
| 87 |
} |
| 88 |
|
| 89 |
public static function sanitizeBoard($data) |
| 90 |
{ |
| 91 |
$fieldMaps = [ |
| 92 |
'board_id' => 'intval', |
| 93 |
'title' => 'sanitize_text_field', |
| 94 |
'parent_id' => 'intval', |
| 95 |
'type' => 'sanitize_text_field', |
| 96 |
'description' => 'wp_kses_post', |
| 97 |
'currency' => 'sanitize_text_field', |
| 98 |
'image_url' => 'sanitize_url', |
| 99 |
'is_auth_require' => 'intval', |
| 100 |
'crm_contact_id' => 'intval', |
| 101 |
'id' => 'sanitize_text_field', |
| 102 |
'is_image' => 'rest_sanitize_boolean', |
| 103 |
'color' => 'sanitize_text_field', // sanitize_hex_color doesn't work when color code is greater than 6 characters |
| 104 |
]; |
| 105 |
|
| 106 |
return self::sanitizeData($data, $fieldMaps); |
| 107 |
} |
| 108 |
|
| 109 |
public static function sanitizeStage($data) |
| 110 |
{ |
| 111 |
$fieldMaps = [ |
| 112 |
'title' => 'sanitize_text_field', |
| 113 |
'slug' => 'sanitize_text_field', |
| 114 |
'type' => 'sanitize_text_field', |
| 115 |
'status' => 'sanitize_text_field', |
| 116 |
]; |
| 117 |
|
| 118 |
return self::sanitizeData($data, $fieldMaps); |
| 119 |
} |
| 120 |
|
| 121 |
public static function sanitizeComment($data) |
| 122 |
{ |
| 123 |
$fieldMaps = [ |
| 124 |
'description' => 'wp_kses_post', |
| 125 |
'created_by' => 'intval', |
| 126 |
'task_id' => 'intval', |
| 127 |
'type' => 'sanitize_text_field', |
| 128 |
]; |
| 129 |
|
| 130 |
return self::sanitizeData($data, $fieldMaps); |
| 131 |
} |
| 132 |
|
| 133 |
public static function sanitizeLabel($data) |
| 134 |
{ |
| 135 |
$fieldMaps = [ |
| 136 |
'bg_color' => 'sanitize_hex_color', |
| 137 |
'color' => 'sanitize_hex_color', |
| 138 |
'label' => 'sanitize_text_field', |
| 139 |
'boardId' => 'intval', |
| 140 |
'task_id' => 'intval', |
| 141 |
'meta_value' => 'intval', |
| 142 |
]; |
| 143 |
|
| 144 |
return self::sanitizeData($data, $fieldMaps); |
| 145 |
} |
| 146 |
|
| 147 |
public static function sanitizeSubtask($data) |
| 148 |
{ |
| 149 |
$fieldMaps = [ |
| 150 |
'title' => 'sanitize_text_field', |
| 151 |
'stage' => 'sanitize_text_field', |
| 152 |
'newPosition' => 'intval', |
| 153 |
'priority' => 'sanitize_text_field', |
| 154 |
'task_type' => 'sanitize_text_field', |
| 155 |
'board_id' => 'intval', |
| 156 |
'created_by' => 'intval', |
| 157 |
'due_date' => 'sanitize_text_field', |
| 158 |
]; |
| 159 |
|
| 160 |
return self::sanitizeData($data, $fieldMaps); |
| 161 |
} |
| 162 |
|
| 163 |
public static function createActivity($data) |
| 164 |
{ |
| 165 |
return Activity::create($data); |
| 166 |
} |
| 167 |
|
| 168 |
public static function sanitizeTaskMeta($data) |
| 169 |
{ |
| 170 |
$fieldMaps = [ |
| 171 |
'title' => 'sanitize_text_field', |
| 172 |
'url' => 'sanitize_url', |
| 173 |
|
| 174 |
]; |
| 175 |
|
| 176 |
return self::sanitizeData($data, $fieldMaps); |
| 177 |
} |
| 178 |
|
| 179 |
public static function getFormattedStagesByBoardId($boardId) |
| 180 |
{ |
| 181 |
if (!$boardId) { |
| 182 |
return []; |
| 183 |
} |
| 184 |
|
| 185 |
$board = Board::findOrFail($boardId); |
| 186 |
|
| 187 |
$stages = $board->stages()->get(); |
| 188 |
// return static::formateStage($stages); |
| 189 |
return $stages; |
| 190 |
} |
| 191 |
|
| 192 |
public static function getStagesByBoardId($boardId) |
| 193 |
{ |
| 194 |
if (!$boardId) { |
| 195 |
return []; |
| 196 |
} |
| 197 |
|
| 198 |
$board = Board::findOrFail($boardId); |
| 199 |
|
| 200 |
$stages = $board->stages()->get(); |
| 201 |
return static::formateStage($stages); |
| 202 |
// return $stages; |
| 203 |
} |
| 204 |
|
| 205 |
public static function formateStage($stages) |
| 206 |
{ |
| 207 |
if (!$stages) return []; |
| 208 |
|
| 209 |
$formattedStages = []; |
| 210 |
foreach ($stages as $stage) { |
| 211 |
$boardName = $stage->board->title; |
| 212 |
$formattedStages[] = [ |
| 213 |
'id' => strval($stage->id), |
| 214 |
'title' => $boardName . ' - ' . $stage->title |
| 215 |
]; |
| 216 |
} |
| 217 |
return $formattedStages; |
| 218 |
} |
| 219 |
|
| 220 |
public static function getIdTitleArray($data) |
| 221 |
{ |
| 222 |
$array = []; |
| 223 |
foreach ($data as $item) { |
| 224 |
$array[] = [ |
| 225 |
'id' => $item->id, |
| 226 |
'title' => $item->title |
| 227 |
]; |
| 228 |
} |
| 229 |
return $array; |
| 230 |
} |
| 231 |
|
| 232 |
// get Task Url by task id and board id |
| 233 |
public static function getTaskUrl($taskId, $boardId): string |
| 234 |
{ |
| 235 |
if (!$taskId || !$boardId) { |
| 236 |
return ''; |
| 237 |
} |
| 238 |
return fluent_boards_page_url() . 'boards/' . $boardId . '/tasks/' . $taskId; |
| 239 |
} |
| 240 |
|
| 241 |
public static function getTaskUrlByTask($task): string |
| 242 |
{ |
| 243 |
if (!$task) { |
| 244 |
return ''; |
| 245 |
} |
| 246 |
return fluent_boards_page_url() . 'boards/' . $task->board_id . '/tasks/' . $task->id; |
| 247 |
} |
| 248 |
|
| 249 |
public static function getBoardUrl($boardId): string |
| 250 |
{ |
| 251 |
if (!$boardId) { |
| 252 |
return ''; |
| 253 |
} |
| 254 |
return fluent_boards_page_url() . 'boards/' . $boardId; |
| 255 |
} |
| 256 |
|
| 257 |
public static function crm_contact($id) |
| 258 |
{ |
| 259 |
if (!defined('FLUENTCRM')) { |
| 260 |
return ''; |
| 261 |
} |
| 262 |
|
| 263 |
$contact = \FluentCrm\App\Models\Subscriber::with(['tags', 'lists'])->find($id); |
| 264 |
|
| 265 |
if (!$contact) { |
| 266 |
return null; |
| 267 |
} |
| 268 |
|
| 269 |
return [ |
| 270 |
'id' => $contact->id, |
| 271 |
'email' => $contact->email, |
| 272 |
'first_name' => $contact->first_name, |
| 273 |
'last_name' => $contact->last_name, |
| 274 |
'full_name' => $contact->full_name, |
| 275 |
'avatar' => $contact->avatar, |
| 276 |
'photo' => $contact->photo, |
| 277 |
'status' => $contact->status, |
| 278 |
'contact_type' => $contact->contact_type, |
| 279 |
'last_activity' => $contact->last_activity, |
| 280 |
'life_time_value' => $contact->life_time_value, |
| 281 |
'total_points' => $contact->total_points, |
| 282 |
'user_id' => $contact->user_id, |
| 283 |
'created_at' => $contact->created_at, |
| 284 |
'tags' => Helper::getIdTitleArray($contact->tags), |
| 285 |
'lists' => Helper::getIdTitleArray($contact->lists) |
| 286 |
|
| 287 |
]; |
| 288 |
|
| 289 |
} |
| 290 |
|
| 291 |
public static function getStagesByBoardGroup() |
| 292 |
{ |
| 293 |
$boards = self::getBoards(); |
| 294 |
|
| 295 |
$groups = []; |
| 296 |
foreach ($boards as $board) { |
| 297 |
$groups[] = [ |
| 298 |
'title' => $board->title, |
| 299 |
'slug' => 'aaa' . '_' . $board->id, |
| 300 |
'options' => self::getStagesByBoardId($board->id) |
| 301 |
]; |
| 302 |
} |
| 303 |
return $groups; |
| 304 |
} |
| 305 |
|
| 306 |
public static function getBoards() |
| 307 |
{ |
| 308 |
return Board::orderBy('created_at', 'ASC')->get(); |
| 309 |
} |
| 310 |
|
| 311 |
public static function getStage($stageId) |
| 312 |
{ |
| 313 |
return Stage::findOrFail($stageId); |
| 314 |
} |
| 315 |
|
| 316 |
public static function getBoardByStageId($stageId) |
| 317 |
{ |
| 318 |
$stage = self::getStage($stageId); |
| 319 |
return $stage->board; |
| 320 |
} |
| 321 |
|
| 322 |
public static function sanitizeUserCollections($users) |
| 323 |
{ |
| 324 |
if (current_user_can('list_users')) { |
| 325 |
return $users; |
| 326 |
} |
| 327 |
|
| 328 |
$users->makeHidden(['user_email', 'user_login', 'user_nicename', 'user_registered', 'user_url', 'user_status']); |
| 329 |
|
| 330 |
return $users; |
| 331 |
} |
| 332 |
|
| 333 |
public static function sanitizeUsersArray($users) |
| 334 |
{ |
| 335 |
if (current_user_can('list_users')) { |
| 336 |
return $users; |
| 337 |
} |
| 338 |
|
| 339 |
$sanitizedUsers = []; |
| 340 |
|
| 341 |
foreach ($users as $user) { |
| 342 |
unset($user['user_email']); |
| 343 |
unset($user['email']); |
| 344 |
$sanitizedUsers[] = $user; |
| 345 |
} |
| 346 |
|
| 347 |
return $sanitizedUsers; |
| 348 |
} |
| 349 |
|
| 350 |
public static function getPriorityOptions() |
| 351 |
{ |
| 352 |
return [ |
| 353 |
[ |
| 354 |
'id' => 'low', |
| 355 |
'title' => 'Low' |
| 356 |
], |
| 357 |
[ |
| 358 |
'id' => 'medium', |
| 359 |
'title' => 'Medium' |
| 360 |
], |
| 361 |
[ |
| 362 |
'id' => 'high', |
| 363 |
'title' => 'High' |
| 364 |
], |
| 365 |
]; |
| 366 |
} |
| 367 |
|
| 368 |
public static function dueDateConversion($due_time, $unit) |
| 369 |
{ |
| 370 |
if($due_time <= 0) { |
| 371 |
return null; |
| 372 |
} |
| 373 |
$currentTime = current_time('mysql'); |
| 374 |
$readyString = '+' . $due_time . ' ' . $unit; |
| 375 |
return gmdate('Y-m-d H:i:s',strtotime($readyString,strtotime($currentTime))); |
| 376 |
} |
| 377 |
} |
| 378 |
|