3030use OCA \Deck \Db \Acl ;
3131use OCA \Deck \Db \AssignedUsersMapper ;
3232use OCA \Deck \Db \BoardMapper ;
33+ use OCA \Deck \Db \Card ;
3334use OCA \Deck \Db \CardMapper ;
3435use OCA \Deck \Db \ChangeHelper ;
3536use OCA \Deck \Db \LabelMapper ;
3839use OCA \Deck \StatusException ;
3940use Symfony \Component \EventDispatcher \EventDispatcherInterface ;
4041use Symfony \Component \EventDispatcher \GenericEvent ;
42+ use OCP \IL10N ;
43+ use OCA \Deck \Event \FTSEvent ;
44+ use OCA \Deck \Service \AssignmentService ;
4145
4246class StackService {
4347 private $ stackMapper ;
@@ -49,10 +53,13 @@ class StackService {
4953 private $ cardService ;
5054 private $ assignedUsersMapper ;
5155 private $ attachmentService ;
56+
5257 private $ activityManager ;
5358 /** @var EventDispatcherInterface */
5459 private $ eventDispatcher ;
5560 private $ changeHelper ;
61+ private $ l10n ;
62+ private $ assignmentService ;
5663
5764 public function __construct (
5865 StackMapper $ stackMapper ,
@@ -66,7 +73,9 @@ public function __construct(
6673 AttachmentService $ attachmentService ,
6774 ActivityManager $ activityManager ,
6875 EventDispatcherInterface $ eventDispatcher ,
69- ChangeHelper $ changeHelper
76+ ChangeHelper $ changeHelper ,
77+ IL10N $ l10n ,
78+ AssignmentService $ assignmentService
7079 ) {
7180 $ this ->stackMapper = $ stackMapper ;
7281 $ this ->boardMapper = $ boardMapper ;
@@ -80,6 +89,8 @@ public function __construct(
8089 $ this ->activityManager = $ activityManager ;
8190 $ this ->eventDispatcher = $ eventDispatcher ;
8291 $ this ->changeHelper = $ changeHelper ;
92+ $ this ->l10n = $ l10n ;
93+ $ this ->assignmentService = $ assignmentService ;
8394 }
8495
8596 private function enrichStackWithCards ($ stack , $ since = -1 ) {
@@ -362,4 +373,106 @@ public function reorder($id, $order) {
362373
363374 return $ result ;
364375 }
376+
377+ /**
378+ * @param $id
379+ * @param $boardId
380+ * @param $userId
381+ * @return Stack
382+ * @throws StatusException
383+ * @throws BadRequestException
384+ */
385+ public function clone ($ id , $ boardId , $ userId ) {
386+ if (is_numeric ($ id ) === false ) {
387+ throw new BadRequestException ('stack id must be a number ' );
388+ }
389+ if (is_numeric ($ boardId ) === false ) {
390+ throw new BadRequestException ('board id must be a number ' );
391+ }
392+
393+ $ this ->permissionService ->checkPermission (null , $ boardId , Acl::PERMISSION_MANAGE );
394+ $ this ->permissionService ->checkPermission (null , $ boardId , Acl::PERMISSION_READ );
395+ if ($ this ->boardService ->isArchived (null , $ boardId )) {
396+ throw new StatusException ('Operation not allowed. This board is archived. ' );
397+ }
398+
399+ $ stack = $ this ->stackMapper ->find ($ id );
400+ $ board = $ this ->boardMapper ->find ($ boardId );
401+
402+
403+ $ newStack = new Stack ();
404+ $ newStack ->setTitle ($ stack ->getTitle () . ' ( ' . $ this ->l10n ->t ('copy ' ) . ') ' );
405+ $ newStack ->setBoardId ($ boardId );
406+ $ newStack ->setOrder ($ stack ->getOrder () +1 );
407+ $ newStack = $ this ->stackMapper ->insert ($ newStack );
408+
409+ $ this ->activityManager ->triggerEvent (
410+ ActivityManager::DECK_OBJECT_BOARD , $ newStack , ActivityManager::SUBJECT_STACK_CREATE
411+ );
412+ $ this ->changeHelper ->boardChanged ($ boardId );
413+
414+ $ this ->eventDispatcher ->dispatch (
415+ '\OCA\Deck\Stack::onCreate ' ,
416+ new GenericEvent (null , ['id ' => $ newStack ->getId (), 'stack ' => $ newStack ])
417+ );
418+
419+ $ cards = $ this ->cardMapper ->findAll ($ id );
420+ foreach ($ cards as $ card ) {
421+
422+ $ newCard = new Card ();
423+ $ newCard ->setTitle ($ card ->getTitle ());
424+ $ newCard ->setStackId ($ newStack ->getId ());
425+ $ newCard ->setType ($ card ->getType ());
426+ $ newCard ->setOrder ($ card ->getOrder ());
427+ $ newCard ->setOwner ($ userId );
428+ $ newCard ->setDescription ($ card ->getDescription ());
429+ $ newCard ->setDuedate ($ card ->getDuedate ());
430+
431+ $ newCard = $ this ->cardMapper ->insert ($ newCard );
432+
433+ $ this ->activityManager ->triggerEvent (ActivityManager::DECK_OBJECT_CARD , $ newCard , ActivityManager::SUBJECT_CARD_CREATE );
434+ $ this ->changeHelper ->cardChanged ($ newCard ->getId (), false );
435+ $ this ->eventDispatcher ->dispatch ('\OCA\Deck\Card::onCreate ' ,
436+ new FTSEvent (
437+ null , ['id ' => $ newCard ->getId (), 'card ' => $ newCard , 'userId ' => $ owner , 'stackId ' => $ stackId ]
438+ )
439+ );
440+
441+ if ($ boardId === $ stack ->getBoardId ()) {
442+ $ labels = $ this ->labelMapper ->findAll ($ card ->getId ());
443+ $ labels = $ this ->labelMapper ->findAssignedLabelsForCard ($ card ->id );
444+
445+ $ l = [];
446+ foreach ($ labels as $ label ) {
447+ $ l = $ this ->cardMapper ->assignLabel ($ newCard ->getId (), $ label ->getId ());
448+ }
449+ $ newCard ->setLabels ($ l );
450+
451+
452+ $ assignedUsers = $ this ->assignedUsersMapper ->find ($ card ->getId ());
453+ /* foreach ($assignedUsers as $assignedUser) {
454+ $u = $this->assignmentService->assignUser($newCard->getId(), $assignedUser->getId());
455+ $newCard->setAssignedUsers($u);
456+ } */
457+
458+ //attachments???
459+
460+
461+
462+
463+ }
464+
465+ }
466+
467+ $ createdCards = $ this ->cardMapper ->findAll ($ newStack ->getId ());
468+ $ newStack ->setCards ($ createdCards );
469+
470+
471+
472+
473+
474+
475+
476+ return $ newStack ;
477+ }
365478}
0 commit comments