AuditLogController   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 8
Bugs 4 Features 3
Metric Value
wmc 7
c 8
b 4
f 3
lcom 0
cbo 0
dl 0
loc 69
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B actionDownload() 0 63 7
1
<?php
2
3
namespace Craft;
4
5
/**
6
 * Audit Log Controller.
7
 *
8
 * Handles requests for the Audit Log
9
 *
10
 * @author    Bob Olde Hampsink <[email protected]>
11
 * @copyright Copyright (c) 2015, Bob Olde Hampsink
12
 * @license   MIT
13
 *
14
 * @link      http://github.com/boboldehampsink/auditlog
15
 */
16
class AuditLogController extends BaseController
17
{
18
    /**
19
     * Download a CSV of the changes list.
20
     */
21
    public function actionDownload()
22
    {
23
        // Get criteria
24
        $criteria = craft()->elements->getCriteria('AuditLog', craft()->request->getParam('criteria'));
25
26
        // Get element type
27
        $elementType = craft()->elements->getElementType('AuditLog');
28
29
        // Get order and sort
30
        $viewState = craft()->request->getParam('viewState', array(
31
            'order' => 'id',
32
            'sort' => 'desc',
33
        ));
34
35
        // Set sort on criteria
36
        $criteria->order = $viewState['order'] == 'score' ? 'id' : $viewState['order'].' '.$viewState['sort'];
37
38
        // Did we search?
39
        $criteria->search = craft()->request->getParam('search');
40
41
        // Get source
42
        $sources = $elementType->getSources();
43
        $source = craft()->request->getParam('source', '*');
44
45
        // Set type
46
        $criteria->type = $source != '*' ? $sources[$source]['criteria']['type'] : null;
47
48
        // Get data
49
        $log = craft()->auditLog->log($criteria);
50
51
        // Set status attribute
52
        $attributes = array('status' => Craft::t('Status'));
53
54
        // Get nice attributes
55
        $availableAttributes = $elementType->defineAvailableTableAttributes();
56
57
        // Make 'em fit
58
        foreach ($availableAttributes as $key => $result) {
59
            $attributes[$key] = $result['label'];
60
        }
61
62
        // Ditch the changes button
63
        unset($attributes['changes']);
64
65
        // Re-order data
66
        $data = StringHelper::convertToUTF8('"'.implode('","', $attributes)."\"\r\n");
67
        foreach ($log as $element) {
68
69
            // Gather parsed fields
70
            $fields = array();
71
72
            // Parse fields
73
            foreach ($attributes as $handle => $field) {
74
                $fields[] = $handle == 'status' ? $elementType->getStatuses()[$element->$handle] : strip_tags($elementType->getTableAttributeHtml($element, $handle));
75
            }
76
77
            // Set data
78
            $data .= StringHelper::convertToUTF8('"'.implode('","', $fields)."\"\r\n");
79
        }
80
81
        // Download the file
82
        craft()->request->sendFile('auditlog.csv', $data, array('forceDownload' => true, 'mimeType' => 'text/csv'));
83
    }
84
}
85