Proyectos de Subversion LeadersLinked - Services

Rev

Rev 561 | Rev 721 | Ir a la última revisión | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 561 Rev 562
Línea 88... Línea 88...
88
        $this->translator   = $translator;
88
        $this->translator   = $translator;
89
    }
89
    }
Línea 90... Línea 90...
90
 
90
 
91
    /**
91
    /**
-
 
92
     * 
-
 
93
     * Handles the display of the knowledge area content listing.
92
     * 
94
     * Fetches knowledge areas based on user permissions, network settings, and filters.
93
     * Generación del listado de perfiles
95
     * Returns a JSON response suitable for a data table or list view.
94
     * {@inheritDoc}
96
     * {@inheritDoc}
95
     * @see \Laminas\Mvc\Controller\AbstractActionController::indexAction()
97
     * @see \Laminas\Mvc\Controller\AbstractActionController::indexAction()
96
     */
98
     */
97
    public function indexAction()
99
    public function indexAction()
-
 
100
    {
98
    {
101
        // Get current user and network information from plugins.
99
        $currentUserPlugin = $this->plugin('currentUserPlugin');
102
        $currentUserPlugin = $this->plugin('currentUserPlugin');
Línea 100... Línea 103...
100
        $currentUser = $currentUserPlugin->getUser();
103
        $currentUser = $currentUserPlugin->getUser();
101
 
104
 
Línea 102... Línea 105...
102
        $currentNetworkPlugin = $this->plugin('currentNetworkPlugin');
105
        $currentNetworkPlugin = $this->plugin('currentNetworkPlugin');
103
        $currentNetwork = $currentNetworkPlugin->getNetwork();
106
        $currentNetwork = $currentNetworkPlugin->getNetwork();
Línea 104... Línea -...
104
 
-
 
105
        $request = $this->getRequest();
-
 
-
 
107
 
-
 
108
        $request = $this->getRequest();
106
        if ($request->isGet()) {
109
        if ($request->isGet()) {
107
 
110
 
108
            
111
            // --- ACL Permissions ---
109
 
112
            // Determine user permissions for various knowledge area actions.
110
                $acl = $this->getEvent()->getViewModel()->getVariable('acl');
113
            $acl = $this->getEvent()->getViewModel()->getVariable('acl');
111
                $allowAdd       = $acl->isAllowed($currentUser->usertype_id, 'knowledge-area/add') ? 1 : 0;
-
 
112
                $allowEdit = $acl->isAllowed($currentUser->usertype_id, 'knowledge-area/edit');
-
 
113
                $allowDelete = $acl->isAllowed($currentUser->usertype_id, 'knowledge-area/delete');
114
            $allowAdd       = $acl->isAllowed($currentUser->usertype_id, 'knowledge-area/add') ? 1 : 0;
-
 
115
            $allowEdit      = $acl->isAllowed($currentUser->usertype_id, 'knowledge-area/edit');
-
 
116
            $allowDelete    = $acl->isAllowed($currentUser->usertype_id, 'knowledge-area/delete');
114
                $allowView = $acl->isAllowed($currentUser->usertype_id, 'knowledge-area/view');
117
            $allowView      = $acl->isAllowed($currentUser->usertype_id, 'knowledge-area/view');
115
 
118
 
116
 
119
            // --- Query Parameters ---
117
 
-
 
118
                $category_filter_id = Functions::sanitizeFilterString($this->params()->fromQuery('category_id'));
120
            // Retrieve and sanitize query parameters for filtering, searching, and pagination.
119
                $search = Functions::sanitizeFilterString($this->params()->fromQuery('search'));
121
            $category_filter_id = Functions::sanitizeFilterString($this->params()->fromQuery('category_id'));
120
                $page   = intval($this->params()->fromQuery('start', 1), 10);
122
            $search             = Functions::sanitizeFilterString($this->params()->fromQuery('search'));
121
 
-
 
122
                $order_field        = 'added_on';
-
 
123
                $order_direction    = 'asc';
123
            $page               = intval($this->params()->fromQuery('start', 1), 10);
124
                $records_x_page     = 12;
124
            $order_field        = 'added_on'; // Default order field
125
 
125
            $order_direction    = 'asc';      // Default order direction
126
 
126
            $records_x_page     = 12;         // Default records per page
-
 
127
 
-
 
128
            // --- Category Fetching and Processing ---
127
 
129
            // Initialize arrays to store category information.
128
                $category_with_edition_ids = [];
-
 
129
                $category_with_edition_uuids = [];
130
            $category_with_edition_ids = [];   // IDs of categories the user can edit.
-
 
131
            $category_with_edition_uuids = []; // UUIDs of categories the user can edit (for response).
130
                $category_ids = [];
132
            $user_accessible_category_ids = []; // All category IDs accessible to the user directly.
-
 
133
            $categories = [];                  // Master array of category details (id => [uuid, name]).
131
                $categories = [];
134
 
132
 
135
            // Instantiate mappers.
133
                
136
            $knowledgeAreaCategoryMapper = KnowledgeAreaCategoryMapper::getInstance($this->adapter);
134
                $knowledgeAreaCategoryMapper = KnowledgeAreaCategoryMapper::getInstance($this->adapter);
-
 
135
 
-
 
136
                $knowledgeAreaCategoryUserMapper = KnowledgeAreaCategoryUserMapper::getInstance($this->adapter);
-
 
137
                $records =  $knowledgeAreaCategoryUserMapper->fetchAllByUserId($currentUser->id);
137
            $knowledgeAreaCategoryUserMapper = KnowledgeAreaCategoryUserMapper::getInstance($this->adapter);
138
                
138
 
139
       
-
 
-
 
139
            // 1. Fetch categories based on user's direct permissions (via KnowledgeAreaCategoryUser).
140
                
140
            $userCategoryRecords =  $knowledgeAreaCategoryUserMapper->fetchAllByUserId($currentUser->id);
141
                foreach ($records as $record) {
-
 
-
 
141
            foreach ($userCategoryRecords as $record) {
142
                    if ($record->role == KnowledgeAreaCategoryUser::ROLE_ADMINISTRATOR || $record->role == KnowledgeAreaCategoryUser::ROLE_USER) {
142
                if ($record->role == KnowledgeAreaCategoryUser::ROLE_ADMINISTRATOR || $record->role == KnowledgeAreaCategoryUser::ROLE_USER) {
143
 
143
                    // User has admin or user role in this category, allowing edition.
144
                        array_push($category_with_edition_ids, $record->category_id);
144
                    array_push($category_with_edition_ids, $record->category_id);
145
                        
-
 
146
                        $knowledgeAreaCategory = $knowledgeAreaCategoryMapper->fetchOne($record->category_id);
-
 
147
                        if($knowledgeAreaCategory) {
145
                    // Fetch category details to get UUID for the response.
148
                            array_push($category_with_edition_uuids, $knowledgeAreaCategory->uuid);
-
 
149
                        }
-
 
150
 
146
                    $categoryDetails = $knowledgeAreaCategoryMapper->fetchOne($record->category_id);
-
 
147
                    if($categoryDetails) {
-
 
148
                        array_push($category_with_edition_uuids, $categoryDetails->uuid);
Línea 151... Línea 149...
151
                    }
149
                    }
152
 
-
 
153
                    array_push($category_ids, $record->category_id);
150
                }
154
                }
151
                array_push($user_accessible_category_ids, $record->category_id);
155
 
-
 
156
                $knowledgeAreaCategoryMapper = KnowledgeAreaCategoryMapper::getInstance($this->adapter);
-
 
157
 
-
 
158
                if ($category_ids) {
-
 
159
                    $records =  $knowledgeAreaCategoryMapper->fetchAllByIds($category_ids);
-
 
160
                    foreach ($records as $record) {
-
 
161
                        if (!isset($categories[$record->id])) {
-
 
162
                        
-
 
163
                            $categories[$record->id] = [
-
 
164
                                'uuid' => $record->uuid,
-
 
165
                                'name' => $record->name,
-
 
166
                            ];
-
 
167
                        }
-
 
168
                    }
-
 
169
                }
152
            }
170
                
153
 
171
       
-
 
172
 
154
            // 2. Fetch details for all directly accessible categories if any exist.
173
                $records =  $knowledgeAreaCategoryMapper->fetchAllPublicByNetworkId($currentNetwork->id);
155
            if (!empty($user_accessible_category_ids)) {
174
                foreach ($records as $record) {
156
                $detailedUserCategories =  $knowledgeAreaCategoryMapper->fetchAllByIds($user_accessible_category_ids);
175
                    if (!isset($categories[$record->id])) {
157
                foreach ($detailedUserCategories as $record) {
176
 
158
                    if (!isset($categories[$record->id])) { // Add if not already present
177
                        $categories[$record->id] = [
159
                        $categories[$record->id] = [
-
 
160
                            'uuid' => $record->uuid,
Línea -... Línea 161...
-
 
161
                            'name' => $record->name,
-
 
162
                        ];
-
 
163
                    }
-
 
164
                }
-
 
165
            }
-
 
166
                
-
 
167
            // 3. Fetch all public categories within the current network.
-
 
168
            $publicCategories =  $knowledgeAreaCategoryMapper->fetchAllPublicByNetworkId($currentNetwork->id);
-
 
169
            foreach ($publicCategories as $record) {
-
 
170
                if (!isset($categories[$record->id])) { // Add if not already present (ensures no duplicates from user-specific list)
178
                            'uuid' => $record->uuid,
171
                    $categories[$record->id] = [
-
 
172
                        'uuid' => $record->uuid,
179
                            'name' => $record->name,
173
                        'name' => $record->name,
180
                        ];
174
                    ];
-
 
175
                }
-
 
176
            }
-
 
177
            
-
 
178
            // Sort all collected categories alphabetically by name.
-
 
179
            uasort($categories, function ($a, $b) {
-
 
180
                return $a['name'] <=> $b['name'];
-
 
181
            });
-
 
182
 
-
 
183
            // --- Filtering by Category ---
-
 
184
            // If a specific category filter is applied, narrow down the category_ids to search in.
-
 
185
            $category_ids_to_filter = array_keys($categories); // Start with all accessible category IDs.
181
                    }
186
            if ($category_filter_id) {
-
 
187
                $categoryFilter = $knowledgeAreaCategoryMapper->fetchOneByUuid($category_filter_id);
-
 
188
                if ($categoryFilter && isset($categories[$categoryFilter->id])) { // Ensure filtered category is accessible
-
 
189
                    $category_ids_to_filter = [$categoryFilter->id];
-
 
190
                } else {
-
 
191
                    $category_ids_to_filter = []; // Filtered category not found or not accessible, show no results for this filter.
-
 
192
                }
182
                }
193
            }
-
 
194
            
-
 
195
            // --- Content Fetching ---
-
 
196
            $storage = Storage::getInstance($this->config, $this->adapter);
183
                
197
            // $path is the base path for knowledge area, used by getGenericImage.
-
 
198
            $path = $storage->getPathKnowledgeArea(); 
-
 
199
 
-
 
200
            $knowledgeAreaContentMapper = KnowledgeAreaContentMapper::getInstance($this->adapter);
-
 
201
            // Fetch paginated knowledge area content based on accessible categories and search criteria.
-
 
202
            $paginator = $knowledgeAreaContentMapper->fetchAllDataTableByCategoryIds($category_ids_to_filter, $search, $page, $records_x_page, $order_field, $order_direction);
-
 
203
 
184
 
204
            // --- Response Item Construction ---
185
                uasort($categories, function ($a, $b) {
205
            $items = [];
186
                    return $a['name'] <=> $b['name'];
206
            $contentRecords = $paginator->getCurrentItems();
187
                });
207
            foreach ($contentRecords as $record) {
-
 
208
                // Ensure category details are available for the current content item.
-
 
209
                // This might happen if content somehow exists for a category not in the user's compiled list (e.g. stale data).
-
 
210
                if (!isset($categories[$record->category_id])) {
188
 
211
                    $category = $knowledgeAreaCategoryMapper->fetchOne($record->category_id);
189
 
212
                    if ($category) {
-
 
213
                        $categories[$category->id] = [
190
                if ($category_filter_id) {
214
                            'uuid' =>  $category->uuid,
191
                    $categoryFilter = $knowledgeAreaCategoryMapper->fetchOneByUuid($category_filter_id);
215
                            'name' =>  $category->name,
Línea 192... Línea -...
192
                    if ($categoryFilter) {
-
 
193
                        $category_ids = [$categoryFilter->id];
-
 
194
                    } else {
-
 
195
                        $category_ids = [];
-
 
196
                    }
-
 
197
                }
-
 
198
                
-
 
199
                $storage = Storage::getInstance($this->config, $this->adapter);
-
 
200
                $path = $storage->getPathKnowledgeArea();
-
 
201
 
-
 
202
                $knowledgeAreaContentMapper = KnowledgeAreaContentMapper::getInstance($this->adapter);
-
 
203
                $paginator = $knowledgeAreaContentMapper->fetchAllDataTableByCategoryIds($category_ids,  $search, $page, $records_x_page, $order_field, $order_direction);
-
 
204
 
-
 
205
 
216
                        ];
206
                $items = [];
-
 
207
                $records = $paginator->getCurrentItems();
-
 
208
                foreach ($records as $record) 
-
 
209
                {
-
 
210
 
-
 
211
                    
-
 
212
                    if (!isset($categories[$record->category_id])) {
-
 
213
                        $category = $knowledgeAreaCategoryMapper->fetchOne($record->category_id);
-
 
214
                        if ($category) {
-
 
215
                            $categories[$category->id] = [
-
 
216
                                'uuid' =>  $category->uuid,
-
 
217
                                'name' =>  $category->name,
-
 
218
                            ];
-
 
219
                        }
217
                    } else {
220
                    }
218
                        // Fallback if category is still not found (should be rare).
221
                    
219
                        $categories[$record->category_id] = ['uuid' => null, 'name' => 'Unknown Category'];
222
                   
-
 
223
 
-
 
224
 
-
 
225
 
-
 
226
                    $description = strip_tags($record->description);
-
 
227
                    if (strlen($description) > 120) {
-
 
228
                        $description = substr($description, 0, 120) . '...';
-
 
229
                    }
-
 
230
 
-
 
231
                    $item = [
-
 
232
                        'image' => $storage->getGenericImage($path, ($categories[$record->category_id]['uuid'] ?? null), $record->image),
-
 
233
                        'title' => $record->title,
-
 
234
                        'description' => $description,
-
 
235
                        'category' => $categories[$record->category_id]['name'],
-
 
236
                        'link_view' => $allowView ?  $this->url()->fromRoute('knowledge-area/view', ['id' => $record->uuid]) : '',
-
 
237
 
-
 
238
                    ];
-
 
239
 
-
 
240
 
-
 
241
 
220
                    }
Línea 242... Línea -...
242
                    if (in_array($record->category_id, $category_with_edition_ids)) {
-
 
243
                        $item['link_edit'] = $allowEdit ?  $this->url()->fromRoute('knowledge-area/edit', ['id' => $record->uuid]) : '';
-
 
244
                        $item['link_delete'] = $allowDelete ? $this->url()->fromRoute('knowledge-area/delete', ['id' => $record->uuid]) : '';
221
                }
245
                    }
-
 
246
 
222
                
247
                    array_push($items, $item);
223
                // Strip HTML tags and truncate description for display.
-
 
224
                $description = strip_tags($record->description);
248
                }
225
                if (strlen($description) > 120) {
249
 
226
                    $description = substr($description, 0, 120) . '...';
250
 
227
                }
251
                
228
 
252
                $image_size = $this->config['leaderslinked.image_sizes.knowledge_area'];
229
                // Construct the item for the JSON response.
-
 
230
                $item = [
253
 
231
                    // Use the KnowledgeAreaCategory's UUID for the image path.
254
                return new JsonModel([
232
                    'image' => $storage->getGenericImage($path, ($categories[$record->category_id]['uuid'] ?? null), $record->image),
255
                    'success' => true,
233
                    'title' => $record->title,
256
                    'data' => [
-
 
257
                        'items' => $items,
234
                    'description' => $description,
258
                        'total' => $paginator->getTotalItemCount(),
-
 
259
                        'page' => $paginator->getCurrentPageNumber(),
235
                    'category' => $categories[$record->category_id]['name'],
-
 
236
                    'link_view' => $allowView ?  $this->url()->fromRoute('knowledge-area/view', ['id' => $record->uuid]) : '',
260
                        'total_pages' => $paginator->getPages()->pageCount,
237
                ];
Línea -... Línea 238...
-
 
238
 
-
 
239
                // Add edit/delete links if user has permission for the content's category.
-
 
240
                if (in_array($record->category_id, $category_with_edition_ids)) {
-
 
241
                    $item['link_edit'] = $allowEdit ?  $this->url()->fromRoute('knowledge-area/edit', ['id' => $record->uuid]) : '';
-
 
242
                    $item['link_delete'] = $allowDelete ? $this->url()->fromRoute('knowledge-area/delete', ['id' => $record->uuid]) : '';
-
 
243
                }
-
 
244
                array_push($items, $item);
-
 
245
            }
-
 
246
            
-
 
247
            // --- JSON Response ---
-
 
248
            // Configured image size for frontend reference.
-
 
249
            $image_size = $this->config['leaderslinked.image_sizes.knowledge_area'];
-
 
250
 
-
 
251
            // Return all data in a JSON format.
-
 
252
            return new JsonModel([
-
 
253
                'success' => true,
-
 
254
                'data' => [
-
 
255
                    'items' => $items,                         // Paginated content items.
-
 
256
                    'total' => $paginator->getTotalItemCount(), // Total number of content items.
261
                        'categories' => $categories,
257
                    'page' => $paginator->getCurrentPageNumber(), // Current page number.
262
                        'categories_with_edition' => $category_with_edition_uuids,
258
                    'total_pages' => $paginator->getPages()->pageCount, // Total number of pages.
263
                        'link_add' => $allowAdd ? $this->url()->fromRoute('knowledge-area/add') : '',
259
                    'categories' => array_values($categories), // List of all accessible categories (re-indexed).
264
                        'image_size' => $image_size,
260
                    'categories_with_edition' => $category_with_edition_uuids, // UUIDs of categories user can edit.
265
                        'content_edit' => count($category_with_edition_ids) > 0,
261
                    'link_add' => $allowAdd ? $this->url()->fromRoute('knowledge-area/add') : '', // Link to add new content if allowed.