| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentBoards\App\Hooks\Cli; |
| 4 |
|
| 5 |
use FluentBoards\App\Models\Relation; |
| 6 |
use FluentBoards\App\Services\Constant; |
| 7 |
use FluentCrm\App\Models\Subscriber; |
| 8 |
use FluentCrm\App\Models\Tag; |
| 9 |
use WP_CLI; |
| 10 |
|
| 11 |
use FluentBoards\App\Models\Board; |
| 12 |
|
| 13 |
class Commands |
| 14 |
{ |
| 15 |
public function crm_role_assign() |
| 16 |
{ |
| 17 |
if (!defined('FLUENTCRM')) { |
| 18 |
\WP_CLI::error('FluentCRM is not installed'); |
| 19 |
} |
| 20 |
|
| 21 |
if (!defined('FLUENT_BOARDS_PRO')) { |
| 22 |
\WP_CLI::error('FluentBoards Pro is required'); |
| 23 |
} |
| 24 |
|
| 25 |
\WP_CLI::line('You are about to add Members to your Project Boards from FluentCRM Tags'); |
| 26 |
\WP_CLI::line('Please select the Project Board you want to add Members to:'); |
| 27 |
|
| 28 |
$boards = Board::orderBy('title', 'asc')->get(); |
| 29 |
|
| 30 |
$boardHtml = ''; |
| 31 |
|
| 32 |
foreach ($boards as $board) { |
| 33 |
$boardHtml .= $board->id . ' : ' . $board->title . "\n"; |
| 34 |
} |
| 35 |
|
| 36 |
\WP_CLI::line($boardHtml); |
| 37 |
|
| 38 |
// asl for the board id |
| 39 |
\WP_CLI::line('Enter the ID of the board you want to add members to:'); |
| 40 |
$boardId = trim(fgets(STDIN)); // Read input from the user |
| 41 |
|
| 42 |
if (!is_numeric($boardId) || !$selectedBoard = Board::find($boardId)) { |
| 43 |
\WP_CLI::error('Invalid Board ID'); |
| 44 |
} |
| 45 |
|
| 46 |
$tags = Tag::orderBy('title', 'asc')->get(); |
| 47 |
|
| 48 |
$tagHtml = ''; |
| 49 |
foreach ($tags as $tag) { |
| 50 |
$tagHtml .= $tag->id . ' : ' . $tag->title . "\n"; |
| 51 |
} |
| 52 |
|
| 53 |
\WP_CLI::line($tagHtml); |
| 54 |
|
| 55 |
|
| 56 |
\WP_CLI::line('Enter the Tag ID by which the contacts will be added as the member of the select board:'); |
| 57 |
|
| 58 |
$tagId = trim(fgets(STDIN)); // Read input from the user |
| 59 |
|
| 60 |
if (!is_numeric($tagId) || !$selectedTag = Tag::find($tagId)) { |
| 61 |
\WP_CLI::error('Invalid Board ID'); |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
$contacts = Subscriber::filterByTags([$selectedTag->id]) |
| 66 |
->has('user') |
| 67 |
->get(); |
| 68 |
|
| 69 |
$tableData = []; |
| 70 |
|
| 71 |
foreach ($contacts as $contact) { |
| 72 |
$tableData[] = [ |
| 73 |
'UserID' => $contact->user_id, |
| 74 |
'Email' => $contact->email, |
| 75 |
'Name' => $contact->first_name . ' ' . $contact->last_name |
| 76 |
]; |
| 77 |
} |
| 78 |
|
| 79 |
\WP_CLI::line('Following contacts will be added to the board as members:'); |
| 80 |
\WP_CLI\Utils\format_items('table', $tableData, ['UserID', 'Email', 'Name']); |
| 81 |
|
| 82 |
\WP_CLI::line('Do you want to proceed? (yes/no)'); |
| 83 |
|
| 84 |
$confirmation = trim(fgets(STDIN)); // Read input from the user |
| 85 |
|
| 86 |
if ($confirmation != 'yes') { |
| 87 |
\WP_CLI::line('Operation Cancelled'); |
| 88 |
return; |
| 89 |
} |
| 90 |
|
| 91 |
$userIds = $contacts->pluck('user_id')->toArray(); |
| 92 |
|
| 93 |
|
| 94 |
$meta = [ |
| 95 |
'object_id' => $board->id, |
| 96 |
'object_type' => Constant::OBJECT_TYPE_BOARD_USER, |
| 97 |
'settings' => Constant::BOARD_USER_SETTINGS, |
| 98 |
'preferences' => Constant::BOARD_NOTIFICATION_TYPES |
| 99 |
]; |
| 100 |
|
| 101 |
|
| 102 |
$added = 0; |
| 103 |
|
| 104 |
foreach ($contacts as $contact) { |
| 105 |
$relation = Relation::where('object_id', $selectedBoard->id) |
| 106 |
->where('object_type', 'board_user') |
| 107 |
->where('foreign_id', $contact->user_id) |
| 108 |
->first(); |
| 109 |
|
| 110 |
if ($relation) { |
| 111 |
continue; |
| 112 |
} |
| 113 |
$meta['foreign_id'] = $contact->user_id; |
| 114 |
Relation::create($meta); |
| 115 |
$added++; |
| 116 |
} |
| 117 |
|
| 118 |
\WP_CLI::success('Operation Completed. ' . $added . ' contacts added to the board'); |
| 119 |
} |
| 120 |
} |
| 121 |
|