source: extensions/Copyrights/batch_single.php @ 16859

Last change on this file since 16859 was 11794, checked in by Mattias, 13 years ago

added support for -1 copyright
added an order by cr_id
the plugin handles deletes (no copyright) better

File size: 3.1 KB
Line 
1<?php
2
3// Add event handlers for the prefilter
4add_event_handler('loc_end_element_set_unit', 'CR_set_prefilter_batch_single', 55 );
5add_event_handler('loc_begin_element_set_unit', 'CR_batch_single_submit', 50 );
6
7// Change the variables used by the function that changes the template
8add_event_handler('loc_end_element_set_unit', 'CR_add_batch_single_vars_to_template');
9
10// Add a prefilter to the template
11function CR_set_prefilter_batch_single()
12{
13        global $template;
14        $template->set_prefilter('batch_manager_unit', 'CR_batch_single');
15}
16
17// Insert the copyright selector to the template
18function CR_batch_single($content, &$smarty)
19{
20        $search = "#<td><strong>{'Creation date'#";
21
22        // We use the <tr> from the Creation date, and give them a new <tr>
23        $replacement = '<td><strong>{\'Copyright\'|@translate}</strong></td>
24                <td>
25                        <select id="copyright-{$element.ID}" name="copyright-{$element.ID}">
26                                <option value="">--</option>
27                                {html_options options=$CRoptions selected=$CRcopyrights[$element.ID]}
28                        </select>
29                </td>
30        </tr>
31       
32        <tr>
33                <td><strong>{\'Creation date\'';
34
35  return preg_replace($search, $replacement, $content);
36}
37
38// Assign the variables to the Smarty template
39function CR_add_batch_single_vars_to_template()
40{
41        global $template;
42
43        load_language('plugin.lang', dirname(__FILE__).'/');
44
45        // Fetch all the copyrights and assign them to the template
46        $query = sprintf(
47                'SELECT `cr_id`,`name`
48                FROM %s
49                WHERE `visible`<>0
50                ORDER BY cr_id ASC
51                ;',
52                COPYRIGHTS_ADMIN);
53        $result = pwg_query($query);
54
55        $CRoptions = array();
56        while ($row = pwg_db_fetch_assoc($result)) {
57                $CRoptions[$row['cr_id']] = $row['name'];
58        }
59        $template->assign('CRoptions', $CRoptions);
60       
61        // Get the copyright for each element
62        $query = sprintf(
63                'SELECT `media_id`, `cr_id`
64                FROM %s
65                ;',
66                COPYRIGHTS_MEDIA);
67        $result = pwg_query($query);
68       
69        $CRcopyrights = array();
70        while ($row = pwg_db_fetch_assoc($result)) {
71                $CRcopyrights[$row['media_id']] = $row['cr_id'];
72  }
73
74  // Assign the copyrights to the template
75        $template->assign('CRcopyrights', $CRcopyrights);
76}
77
78// Catch the submit and update the copyrights tables
79function CR_batch_single_submit()
80{
81        if (isset($_POST['submit']))
82        {
83                // The image id's:
84                $collection = explode(',', $_POST['element_ids']);
85
86                // Delete all existing id's of which the copyright is going to be set
87                if (count($collection) > 0) {
88                        $query = sprintf(
89                                'DELETE
90                                FROM %s
91                                WHERE media_id IN (%s)
92                                ;',
93                                COPYRIGHTS_MEDIA, implode(',', $collection));
94                        pwg_query($query);
95                }
96
97                // Add all copyrights to an array
98                $edits = array();
99                foreach ($collection as $image_id) {
100                        // The copyright id's
101                        $crID = pwg_db_real_escape_string($_POST['copyright-'.$image_id]);
102
103                        // If you assign no copyright, dont put them in the table
104                        if ($crID != '') {
105                                array_push(
106                                        $edits,
107                                        array(
108                                                'media_id' => $image_id,
109                                                'cr_id' => $crID,
110                                        )
111                                );
112                        }
113                }
114
115                if (count($edits) > 0) {
116                        // Insert the array to the database
117                        mass_inserts(
118                                COPYRIGHTS_MEDIA,        // Table name
119                                array_keys($edits[0]),   // Columns
120                                $edits                   // Data
121                        );
122                }
123        }
124}
125
126?>
Note: See TracBrowser for help on using the repository browser.