1<?php
2/*********************************************************************
3    helptopics.php
4
5    Help Topics.
6
7    Peter Rotich <peter@osticket.com>
8    Copyright (c)  2006-2013 osTicket
9    http://www.osticket.com
10
11    Released under the GNU General Public License WITHOUT ANY WARRANTY.
12    See LICENSE.TXT for details.
13
14    vim: expandtab sw=4 ts=4 sts=4:
15**********************************************************************/
16require('admin.inc.php');
17include_once(INCLUDE_DIR.'class.topic.php');
18include_once(INCLUDE_DIR.'class.faq.php');
19require_once(INCLUDE_DIR.'class.dynamic_forms.php');
20
21$topic=null;
22if($_REQUEST['id'] && !($topic=Topic::lookup($_REQUEST['id'])))
23    $errors['err']=sprintf(__('%s: Unknown or invalid ID.'), __('help topic'));
24
25if($_POST){
26    switch(strtolower($_POST['do'])){
27        case 'update':
28            if(!$topic){
29                $errors['err']=sprintf(__('%s: Unknown or invalid'), __('help topic'));
30            }elseif($topic->update($_POST,$errors)){
31                $msg=sprintf(__('Successfully updated %s.'),
32                    __('this help topic'));
33            }elseif(!$errors['err']){
34                $errors['err'] = sprintf('%s %s',
35                    sprintf(__('Unable to update %s.'), __('this help topic')),
36                    __('Correct any errors below and try again.'));
37            }
38            break;
39        case 'create':
40            $_topic = Topic::create();
41            if ($_topic->update($_POST, $errors)) {
42                $topic = $_topic;
43                $msg=sprintf(__('Successfully added %s.'), Format::htmlchars($_POST['topic']));
44                $type = array('type' => 'created');
45                Signal::send('object.created', $topic, $type);
46                $_REQUEST['a']=null;
47            }elseif(!$errors['err']){
48                $errors['err']=sprintf('%s %s',
49                    sprintf(__('Unable to add %s.'), __('this help topic')),
50                    __('Correct any errors below and try again.'));
51            }
52            break;
53        case 'mass_process':
54            switch(strtolower($_POST['a'])) {
55            case 'sort':
56                // Pass
57                break;
58            default:
59                if(!$_POST['ids'] || !is_array($_POST['ids']) || !count($_POST['ids']))
60                    $errors['err'] = sprintf(__('You must select at least %s.'),
61                        __('one help topic'));
62            }
63            if (!$errors) {
64                $count=$_POST['ids']?count($_POST['ids']):0;
65
66                $activeTopics = Topic::getHelpTopics(false, false);
67                $allTopics = count(Topic::getAllHelpTopics());
68                $diff = array_intersect($_POST['ids'], array_keys($activeTopics));
69
70                switch(strtolower($_POST['a'])) {
71                    case 'enable':
72                        $topics = Topic::objects()->filter(array(
73                          'topic_id__in'=>$_POST['ids'],
74                        ));
75                        foreach ($topics as $t) {
76                          $t->setFlag(Topic::FLAG_ARCHIVED, false);
77                          $t->setFlag(Topic::FLAG_ACTIVE, true);
78                          $filter_actions = FilterAction::objects()->filter(array('type' => 'topic', 'configuration' => '{"topic_id":'. $t->getId().'}'));
79                          FilterAction::setFilterFlags($filter_actions, 'Filter::FLAG_INACTIVE_HT', false);
80                          if($t->save()) {
81                              $type = array('type' => 'edited', 'status' => 'Active');
82                              Signal::send('object.edited', $t, $type);
83                              $num++;
84                          }
85
86                        }
87
88                        if ($num > 0) {
89                            if($num==$count)
90                                $msg = sprintf(__('Successfully enabled %s'),
91                                    _N('selected help topic', 'selected help topics', $count));
92                            else
93                                $warn = sprintf(__('%1$d of %2$d %3$s enabled'), $num, $count,
94                                    _N('selected help topic', 'selected help topics', $count));
95                        } else {
96                            $errors['err'] = sprintf(__('Unable to enable %s'),
97                                _N('selected help topic', 'selected help topics', $count));
98                        }
99                        break;
100                    case 'disable':
101                        $num=0;
102                        $topics = Topic::objects()->filter(array(
103                          'topic_id__in'=>$_POST['ids'],
104                        ))->exclude(array(
105                            'topic_id'=>$cfg->getDefaultTopicId()
106                        ));
107
108                        if (($count >= $allTopics) ||
109                            (count($diff) == count($activeTopics))) {
110                            $errors['err'] = __('At least one Topic must be Active');
111                        } else {
112                            foreach ($topics as $t) {
113                              $t->setFlag(Topic::FLAG_ARCHIVED, false);
114                              $t->setFlag(Topic::FLAG_ACTIVE, false);
115                              $filter_actions = FilterAction::objects()->filter(array('type' => 'topic', 'configuration' => '{"topic_id":'. $t->getId().'}'));
116                              FilterAction::setFilterFlags($filter_actions, 'Filter::FLAG_INACTIVE_HT', true);
117                              if($t->save()) {
118                                  $type = array('type' => 'edited', 'status' => 'Disabled');
119                                  Signal::send('object.edited', $t, $type);
120                                  $num++;
121                              }
122                            }
123                        }
124
125                        if ($num > 0) {
126                            if($num==$count)
127                                $msg = sprintf(__('Successfully disabled %s'),
128                                    _N('selected help topic', 'selected help topics', $count));
129                            else
130                                $warn = sprintf(__('%1$d of %2$d %3$s disabled'), $num, $count,
131                                    _N('selected help topic', 'selected help topics', $count));
132                        } else {
133                            $errors['err'] = $errors['err'] ?: sprintf(__('Unable to disable %s'),
134                                _N('selected help topic', 'selected help topics', $count));
135                        }
136                        break;
137                    case 'archive':
138                        $num=0;
139                        $topics = Topic::objects()->filter(array(
140                          'topic_id__in'=>$_POST['ids'],
141                        ))->exclude(array(
142                            'topic_id'=>$cfg->getDefaultTopicId()
143                        ));
144
145                        if (($count >= $allTopics) ||
146                            (count($diff) == count($activeTopics))) {
147                            $errors['err'] = __('At least one Topic must be Active');
148                        } else {
149                            foreach ($topics as $t) {
150                              $t->setFlag(Topic::FLAG_ARCHIVED, true);
151                              $t->setFlag(Topic::FLAG_ACTIVE, false);
152                              $filter_actions = FilterAction::objects()->filter(array('type' => 'topic', 'configuration' => '{"topic_id":'. $t->getId().'}'));
153                              FilterAction::setFilterFlags($filter_actions, 'Filter::FLAG_INACTIVE_HT', true);
154                              if($t->save()) {
155                                $type = array('type' => 'edited', 'status' => 'Archived');
156                                Signal::send('object.edited', $t, $type);
157                                $num++;
158                              }
159                            }
160                        }
161
162                        if ($num > 0) {
163                            if($num==$count)
164                                $msg = sprintf(__('Successfully archived %s'),
165                                    _N('selected help topic', 'selected help topics', $count));
166                            else
167                                $warn = sprintf(__('%1$d of %2$d %3$s archived'), $num, $count,
168                                    _N('selected help topic', 'selected help topics', $count));
169                        } else {
170                            $errors['err'] = $errors['err'] ?: sprintf(__('Unable to archive %s'),
171                                _N('selected help topic', 'selected help topics', $count));
172                        }
173                        break;
174                    case 'delete':
175                        $i=1;
176                        $topics = Topic::objects()->filter(array(
177                            'topic_id__in'=>$_POST['ids']
178                        ));
179
180                        //dont allow deletion of all topics
181                        if (($count >= $allTopics) ||
182                             count($diff) == count($activeTopics)) {
183                            $errors['err'] = __('At least one Topic must be Active');
184                        } else {
185                            foreach($topics as $t) {
186                                if($t->getId()!=$cfg->getDefaultTopicId() && $t->delete())
187                                    $i++;
188                            }
189                        }
190
191                        if($i==($count + 1))
192                            $msg = sprintf(__('Successfully deleted %s.'),
193                                _N('selected help topic', 'selected help topics', $count));
194                        elseif($i>1)
195                            $warn = sprintf(__('%1$d of %2$d %3$s deleted'), ($i-1), $count,
196                                _N('selected help topic', 'selected help topics', $count));
197                        elseif(!$errors['err'])
198                            $errors['err']  = sprintf(__('Unable to delete %s.'),
199                                _N('selected help topic', 'selected help topics', $count));
200                        break;
201                    case 'sort':
202                        try {
203                            $cfg->setTopicSortMode($_POST['help_topic_sort_mode']);
204                            if ($cfg->getTopicSortMode() == 'm') {
205                                foreach ($_POST as $k=>$v) {
206                                    if (strpos($k, 'sort-') === 0
207                                            && is_numeric($v)
208                                            && ($t = Topic::lookup(substr($k, 5))))
209                                        $t->setSortOrder($v);
210                                }
211                            }
212                            $msg = __('Successfully set sorting configuration');
213                        }
214                        catch (Exception $ex) {
215                            $errors['err'] = __('Unable to set sorting mode');
216                        }
217                        break;
218                    default:
219                        $errors['err']=sprintf('%s - %s', __('Unknown action'), __('Get technical help!'));
220                }
221            }
222            break;
223        default:
224            $errors['err']=__('Unknown action');
225            break;
226    }
227    if ($id or $topic) {
228        if (!$id) $id=$topic->getId();
229    }
230}
231
232$page='helptopics.inc.php';
233$tip_namespace = 'manage.helptopic';
234if($topic || ($_REQUEST['a'] && !strcasecmp($_REQUEST['a'],'add')))
235{
236    if ($topic && ($dept=$topic->getDept()) && !$dept->isActive())
237      $warn = sprintf(__('%s is assigned a %s that is not active.'), __('Help Topic'), __('Department'));
238
239    $page='helptopic.inc.php';
240}
241
242$nav->setTabActive('manage');
243$ost->addExtraHeader('<meta name="tip-namespace" content="' . $tip_namespace . '" />',
244    "$('#content').data('tipNamespace', '".$tip_namespace."');");
245require(STAFFINC_DIR.'header.inc.php');
246require(STAFFINC_DIR.$page);
247include(STAFFINC_DIR.'footer.inc.php');
248?>
249