@@ -40,42 +40,66 @@ public function __construct(
4040 public function prepareDataSource (array $ dataSource )
4141 {
4242 if (isset ($ dataSource ['data ' ]['items ' ])) {
43- foreach ($ dataSource ['data ' ]['items ' ] as & $ item ) {
44- if (isset ($ item ['conversation_id ' ])) {
45- $ item [$ this ->getData ('name ' )] = [
46- 'view ' => [
47- 'href ' => $ this ->urlBuilder ->getUrl (
48- 'magentomcpai/conversation/view ' ,
49- ['id ' => $ item ['conversation_id ' ]]
50- ),
51- 'label ' => __ ('View ' ),
52- 'hidden ' => false ,
53- ],
54- 'send_transcript ' => [
55- 'href ' => $ this ->urlBuilder ->getUrl (
56- 'magentomcpai/conversation/send ' ,
57- ['id ' => $ item ['conversation_id ' ]]
58- ),
59- 'label ' => __ ('Send Transcript ' ),
60- 'hidden ' => (bool )$ item ['transcript_sent ' ],
61- ],
62- 'delete ' => [
63- 'href ' => $ this ->urlBuilder ->getUrl (
64- 'magentomcpai/conversation/delete ' ,
65- ['id ' => $ item ['conversation_id ' ]]
66- ),
67- 'label ' => __ ('Delete ' ),
68- 'confirm ' => [
69- 'title ' => __ ('Delete Conversation ' ),
70- 'message ' => __ ('Are you sure you want to delete this conversation? ' )
71- ],
72- 'hidden ' => false ,
73- ]
43+ foreach ($ dataSource ['data ' ]['items ' ] as &$ item ) {
44+ $ actions = $ this ->getData ('action_list ' );
45+ if (!is_array ($ actions )) {
46+ continue ;
47+ }
48+
49+ foreach ($ actions as $ key => $ action ) {
50+ if (!$ this ->isValidAction ($ action )) {
51+ continue ;
52+ }
53+
54+ $ params = $ this ->prepareActionParams ($ action ['params ' ], $ item );
55+
56+ $ actionData = [
57+ 'href ' => $ this ->urlBuilder ->getUrl ($ action ['path ' ], $ params ),
58+ 'label ' => $ action ['label ' ],
59+ 'hidden ' => false ,
7460 ];
61+
62+ if (isset ($ action ['confirm ' ])) {
63+ $ actionData ['confirm ' ] = $ action ['confirm ' ];
64+ }
65+
66+ $ item [$ this ->getData ('name ' )][$ key ] = $ actionData ;
7567 }
7668 }
7769 }
7870
7971 return $ dataSource ;
8072 }
73+
74+
75+ /**
76+ * Validate action configuration
77+ *
78+ * @param array $action
79+ * @return bool
80+ */
81+ private function isValidAction (array $ action ): bool
82+ {
83+ return isset ($ action ['path ' ], $ action ['label ' ], $ action ['params ' ])
84+ && is_array ($ action ['params ' ]);
85+ }
86+
87+ /**
88+ * Prepare action parameters
89+ *
90+ * @param array $params
91+ * @param array $item
92+ * @return array
93+ */
94+ private function prepareActionParams (array $ params , array $ item ): array
95+ {
96+ $ preparedParams = [];
97+ foreach ($ params as $ field => $ param ) {
98+ if (isset ($ item [$ param ])) {
99+ $ preparedParams [$ field ] = $ item [$ param ];
100+ }
101+ }
102+
103+ return $ preparedParams ;
104+ }
81105}
0 commit comments