| 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 |
use FluentBoards\App\Models\Task; |
| 13 |
|
| 14 |
class Commands |
| 15 |
{ |
| 16 |
public function crm_role_assign() |
| 17 |
{ |
| 18 |
if (!defined('FLUENTCRM')) { |
| 19 |
\WP_CLI::error('FluentCRM is not installed'); |
| 20 |
} |
| 21 |
|
| 22 |
if (!defined('FLUENT_BOARDS_PRO')) { |
| 23 |
\WP_CLI::error('FluentBoards Pro is required'); |
| 24 |
} |
| 25 |
|
| 26 |
\WP_CLI::line('You are about to add Members to your Project Boards from FluentCRM Tags'); |
| 27 |
\WP_CLI::line('Please select the Project Board you want to add Members to:'); |
| 28 |
|
| 29 |
$boards = Board::orderBy('title', 'asc')->get(); |
| 30 |
|
| 31 |
$boardHtml = ''; |
| 32 |
|
| 33 |
foreach ($boards as $board) { |
| 34 |
$boardHtml .= $board->id . ' : ' . $board->title . "\n"; |
| 35 |
} |
| 36 |
|
| 37 |
\WP_CLI::line($boardHtml); |
| 38 |
|
| 39 |
// asl for the board id |
| 40 |
\WP_CLI::line('Enter the ID of the board you want to add members to:'); |
| 41 |
$boardId = trim(fgets(STDIN)); // Read input from the user |
| 42 |
|
| 43 |
if (!is_numeric($boardId) || !$selectedBoard = Board::find($boardId)) { |
| 44 |
\WP_CLI::error('Invalid Board ID'); |
| 45 |
} |
| 46 |
|
| 47 |
$tags = Tag::orderBy('title', 'asc')->get(); |
| 48 |
|
| 49 |
$tagHtml = ''; |
| 50 |
foreach ($tags as $tag) { |
| 51 |
$tagHtml .= $tag->id . ' : ' . $tag->title . "\n"; |
| 52 |
} |
| 53 |
|
| 54 |
\WP_CLI::line($tagHtml); |
| 55 |
|
| 56 |
|
| 57 |
\WP_CLI::line('Enter the Tag ID by which the contacts will be added as the member of the select board:'); |
| 58 |
|
| 59 |
$tagId = trim(fgets(STDIN)); // Read input from the user |
| 60 |
|
| 61 |
if (!is_numeric($tagId) || !$selectedTag = Tag::find($tagId)) { |
| 62 |
\WP_CLI::error('Invalid Board ID'); |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
$contacts = Subscriber::filterByTags([$selectedTag->id]) |
| 67 |
->has('user') |
| 68 |
->get(); |
| 69 |
|
| 70 |
$tableData = []; |
| 71 |
|
| 72 |
foreach ($contacts as $contact) { |
| 73 |
$tableData[] = [ |
| 74 |
'UserID' => $contact->user_id, |
| 75 |
'Email' => $contact->email, |
| 76 |
'Name' => $contact->first_name . ' ' . $contact->last_name |
| 77 |
]; |
| 78 |
} |
| 79 |
|
| 80 |
\WP_CLI::line('Following contacts will be added to the board as members:'); |
| 81 |
\WP_CLI\Utils\format_items('table', $tableData, ['UserID', 'Email', 'Name']); |
| 82 |
|
| 83 |
\WP_CLI::line('Do you want to proceed? (yes/no)'); |
| 84 |
|
| 85 |
$confirmation = trim(fgets(STDIN)); // Read input from the user |
| 86 |
|
| 87 |
if ($confirmation != 'yes') { |
| 88 |
\WP_CLI::line('Operation Cancelled'); |
| 89 |
return; |
| 90 |
} |
| 91 |
|
| 92 |
$userIds = $contacts->pluck('user_id')->toArray(); |
| 93 |
|
| 94 |
|
| 95 |
$meta = [ |
| 96 |
'object_id' => $board->id, |
| 97 |
'object_type' => Constant::OBJECT_TYPE_BOARD_USER, |
| 98 |
'settings' => Constant::BOARD_USER_SETTINGS, |
| 99 |
'preferences' => Constant::BOARD_NOTIFICATION_TYPES |
| 100 |
]; |
| 101 |
|
| 102 |
|
| 103 |
$added = 0; |
| 104 |
|
| 105 |
foreach ($contacts as $contact) { |
| 106 |
$relation = Relation::where('object_id', $selectedBoard->id) |
| 107 |
->where('object_type', 'board_user') |
| 108 |
->where('foreign_id', $contact->user_id) |
| 109 |
->first(); |
| 110 |
|
| 111 |
if ($relation) { |
| 112 |
continue; |
| 113 |
} |
| 114 |
$meta['foreign_id'] = $contact->user_id; |
| 115 |
Relation::create($meta); |
| 116 |
$added++; |
| 117 |
} |
| 118 |
|
| 119 |
\WP_CLI::success('Operation Completed. ' . $added . ' contacts added to the board'); |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Create random tasks for a board. |
| 124 |
* |
| 125 |
* ## OPTIONS |
| 126 |
* |
| 127 |
* [--board_id=<board_id>] |
| 128 |
* : The ID of the board to create tasks for. Default is the first board. |
| 129 |
* |
| 130 |
* [--count=<count>] |
| 131 |
* : The number of tasks to create. Default is 10. |
| 132 |
* |
| 133 |
* ## EXAMPLES |
| 134 |
* |
| 135 |
* wp fluent_boards create_random_tasks --board_id=85 --count=1000 |
| 136 |
* |
| 137 |
* @when after_wp_load |
| 138 |
*/ |
| 139 |
public function create_random_tasks($args, $assoc_args) |
| 140 |
{ |
| 141 |
// wp fluent_boards create_random_tasks --board_id=85 --count=1500 |
| 142 |
$board_id = isset($assoc_args['board_id']) ? intval($assoc_args['board_id']) : 0; |
| 143 |
$total_tasks = isset($assoc_args['count']) ? intval($assoc_args['count']) : 10; |
| 144 |
|
| 145 |
if ($board_id === 0) { |
| 146 |
$board = Board::first(); |
| 147 |
} else { |
| 148 |
$board = Board::find($board_id); |
| 149 |
} |
| 150 |
|
| 151 |
if (!$board) { |
| 152 |
WP_CLI::error('Board not found.'); |
| 153 |
return; |
| 154 |
} |
| 155 |
|
| 156 |
$stageIds = $board->stages()->pluck('id')->toArray(); |
| 157 |
|
| 158 |
if (empty($stageIds)) { |
| 159 |
WP_CLI::error('No stages found for this board.'); |
| 160 |
return; |
| 161 |
} |
| 162 |
|
| 163 |
$progress = \WP_CLI\Utils\make_progress_bar('Creating tasks', $total_tasks); |
| 164 |
|
| 165 |
$currentDateTime = new \DateTime(); |
| 166 |
$formattedDateTime = $currentDateTime->format('Y-m-d H:i:s'); |
| 167 |
|
| 168 |
$tasks = []; |
| 169 |
for ($i = 0; $i < $total_tasks; $i++) { |
| 170 |
$tasks[] = [ |
| 171 |
'title' => 'Task Random ' . ($i + 1), |
| 172 |
'board_id' => $board->id, |
| 173 |
'stage_id' => $stageIds[array_rand($stageIds)], |
| 174 |
'created_at' => $formattedDateTime, |
| 175 |
'updated_at' => $formattedDateTime, |
| 176 |
]; |
| 177 |
$progress->tick(); |
| 178 |
} |
| 179 |
|
| 180 |
// Bulk insert |
| 181 |
Task::insert($tasks); |
| 182 |
|
| 183 |
$progress->finish(); |
| 184 |
|
| 185 |
WP_CLI::success("$total_tasks tasks created successfully for board ID: {$board->id}"); |
| 186 |
} |
| 187 |
} |
| 188 |
|