Fixed dailymotion import issues
[embedvideo/.git] / EmbedVideoSiteAdmin.inc
1 <?php
2 /*
3  * Gallery - a web based photo album viewer and editor
4  * Copyright (C) 2000-2007 Bharat Mediratta
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or (at
9  * your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA  02110-1301, USA.
19  */
20
21 /**
22  * Settings for Embed Video
23  * @package EmbedVideo
24  * @subpackage UserInterface
25  * @author Alan J. Pippin <apippin@pippins.net>
26  * @version $Revision: 1 $
27  */
28 class EmbedVideoSiteAdminController extends GalleryController {
29
30     /**
31      * @see GalleryController::handleRequest
32      */
33     function handleRequest($form) {
34         $ret = GalleryCoreApi::assertUserIsSiteAdministrator();
35         if ($ret) {
36             return array($ret, null);
37         }
38
39         $status = $error = array();
40         if (isset($form['action']['add'])) {
41             $type = $form['variable']['type'];
42
43             if (empty($form[$type]['name']) || empty($form[$type]['value'])) {
44                 $error[] = "form[error][$type]";
45             } else {
46                 list ($ret, $variables) = $this->getVariables($type);
47                 if ($ret) {
48                     return array($ret, null);
49                 }
50
51                 /* modify it */
52                 $variables[$form[$type]['name']] = $form[$type]['value'];
53
54                 /* serialize it again */
55                 $ret = $this->setVariables($type, $variables);
56                 if ($ret) {
57                     return array($ret, null);
58                 }
59             }
60         } else if (isset($form['action']['delete'])) {
61             $type = $form['variable']['type'];
62             if (empty($form['delete']['variable'])) {
63                 return array(GalleryCoreApi::error(ERROR_BAD_PARAMETER), null);
64             }
65
66             list ($ret, $variables) = $this->getVariables($type);
67             if ($ret) {
68                 return array($ret, null);
69             }
70             if (!isset($variables[$form['delete']['variable']])) {
71                 return array(GalleryCoreApi::error(ERROR_BAD_PARAMETER), null);
72             }
73
74             /* delete it */
75             unset($variables[$form['delete']['variable']]);
76
77             /* serialize it again */
78             $ret = $this->setVariables($type, $variables);
79             if ($ret) {
80                 return array($ret, null);
81             }
82         }
83
84
85         /* Figure out where to redirect upon success */
86         $method = empty($error) ? 'redirect' : 'delegate';
87         $results[$method]['view'] = 'core.SiteAdmin';
88         $results[$method]['subView'] = 'embedvideo.EmbedVideoSiteAdmin';
89         $results['status'] = $status;
90         $results['error'] = $error;
91
92         return array(null, $results);
93     }
94
95     function implode_with_key($assoc, $inglue = '=', $outglue = '&') {
96         $return = '';
97         if (!empty($assoc)) {
98             foreach ($assoc as $tk => $tv) {
99                 $return = ($return != '' ? $return . $outglue : '') .
100                 $tk . $inglue . $tv;
101             }
102         }
103         return $return;
104     }
105
106     function getVariables($type) {
107         list ($ret, $variables) = GalleryCoreApi::getPluginParameter('module', 'embedvideo',
108                 'embedvideo' . $type . 'Variables');
109         if ($ret) {
110             return array($ret, null);
111         }
112
113         /* unserialize the plugin parameter */
114         if (!empty($variables)) {
115             $variables1 = explode('|', $variables);
116             foreach ($variables1 as $variable) {
117                 list ($key, $value) = explode('=', $variable);
118                 $variables2[$key] = $value;
119             }
120
121             return array(null, $variables2);
122         }
123
124         return array(null, null);
125     }
126
127     function setVariables($type, $variables) {
128         $params = $this->implode_with_key($variables, '=', '|');
129
130         return GalleryCoreApi::setPluginParameter('module', 'embedvideo',
131                 'embedvideo' . $type . 'Variables', $params);
132     }
133 }
134
135 /**
136  * Settings for Embed Video
137  */
138 class EmbedVideoSiteAdminView extends GalleryView {
139
140     /**
141     * @see GalleryView::loadTemplate
142     */
143     function loadTemplate(&$template, &$form) {
144         $AdminEmbedVideo = array();
145
146         $ret = GalleryCoreApi::assertUserIsSiteAdministrator();
147         if ($ret) {
148             return array($ret, null);
149         }
150
151         list ($ret, $params) = GalleryCoreApi::fetchAllPluginParameters('module', 'embedvideo');
152         if ($ret) {
153             return array($ret, null);
154         }
155         foreach ($params as $key => $value) {
156             $form[$key] = $value;
157         }
158
159         foreach (array('embedvideodefaultVariables', 'embedvideooverrideVariables') as $key) {
160             if (!empty($form[$key])) {
161                 $variablesArray = explode('|', $form[$key]);
162                 $variables = array();
163                 foreach ($variablesArray as $variable) {
164                     $variables[] = $variable;
165                 }
166                 $form[$key] = $variables;
167             }
168         }
169
170         $template->setVariable('AdminEmbedVideo', $AdminEmbedVideo);
171         $template->setVariable('controller', 'embedvideo.EmbedVideoSiteAdmin');
172         return array(null,
173                      array('body' => 'modules/embedvideo/templates/EmbedVideoSiteAdmin.tpl'));
174     }
175 }
176 ?>