1<?php
2
3/* Copyright (c) 1998-2013 ILIAS open source, Extended GPL, see docs/LICENSE */
4
5require_once("./Services/COPage/classes/class.ilPCBlog.php");
6require_once("./Services/COPage/classes/class.ilPageContentGUI.php");
7
8/**
9* Class ilPCBlogGUI
10*
11* Handles user commands on blog data
12*
13* @author Jörg Lützenkirchen <luetzenkirchen@leifos.com>
14* @version $I$
15*
16* @ingroup ServicesCOPage
17*/
18class ilPCBlogGUI extends ilPageContentGUI
19{
20    /**
21     * @var ilObjUser
22     */
23    protected $user;
24
25
26    /**
27    * Constructor
28    * @access	public
29    */
30    public function __construct($a_pg_obj, $a_content_obj, $a_hier_id, $a_pc_id = "")
31    {
32        global $DIC;
33
34        $this->tpl = $DIC["tpl"];
35        $this->ctrl = $DIC->ctrl();
36        $this->user = $DIC->user();
37        $this->lng = $DIC->language();
38        parent::__construct($a_pg_obj, $a_content_obj, $a_hier_id, $a_pc_id);
39    }
40
41    /**
42    * execute command
43    */
44    public function executeCommand()
45    {
46        // get next class that processes or forwards current command
47        $next_class = $this->ctrl->getNextClass($this);
48
49        // get current command
50        $cmd = $this->ctrl->getCmd();
51
52        switch ($next_class) {
53            default:
54                $ret = $this->$cmd();
55                break;
56        }
57
58        return $ret;
59    }
60
61    /**
62     * Insert blog form
63     *
64     * @param ilPropertyFormGUI $a_form
65     */
66    public function insert(ilPropertyFormGUI $a_form = null)
67    {
68        $tpl = $this->tpl;
69
70        $this->displayValidationError();
71
72        if (!$a_form) {
73            $a_form = $this->initForm(true);
74        }
75        $tpl->setContent($a_form->getHTML());
76    }
77
78    /**
79     * Edit blog form
80     *
81     * @param ilPropertyFormGUI $a_form
82     */
83    public function edit(ilPropertyFormGUI $a_form = null)
84    {
85        $tpl = $this->tpl;
86
87        $this->displayValidationError();
88
89        if (!$a_form) {
90            $a_form = $this->initForm();
91        }
92        $tpl->setContent($a_form->getHTML());
93    }
94
95    /**
96     * Init blog form
97     *
98     * @param bool $a_insert
99     * @return ilPropertyFormGUI
100     */
101    protected function initForm($a_insert = false)
102    {
103        $ilCtrl = $this->ctrl;
104        $ilUser = $this->user;
105        $lng = $this->lng;
106
107        include_once("./Services/Form/classes/class.ilPropertyFormGUI.php");
108        $form = new ilPropertyFormGUI();
109        $form->setFormAction($ilCtrl->getFormAction($this));
110        if ($a_insert) {
111            $form->setTitle($this->lng->txt("cont_insert_blog"));
112        } else {
113            $form->setTitle($this->lng->txt("cont_update_blog"));
114        }
115
116        $options = array();
117        $blogs_ids = ilBlogPosting::searchBlogsByAuthor($ilUser->getId());
118        if ($blogs_ids) {
119            foreach ($blogs_ids as $blog_id) {
120                $options[$blog_id] = ilObject::_lookupTitle($blog_id);
121            }
122            asort($options);
123        }
124        $obj = new ilSelectInputGUI($this->lng->txt("cont_pc_blog"), "blog");
125        $obj->setRequired(true);
126        $obj->setOptions($options);
127        $form->addItem($obj);
128
129        if ($a_insert) {
130            $form->addCommandButton("create_blog", $this->lng->txt("select"));
131            $form->addCommandButton("cancelCreate", $this->lng->txt("cancel"));
132        } else {
133            $obj->setValue($this->content_obj->getBlogId());
134            $form->addCommandButton("update", $this->lng->txt("select"));
135            $form->addCommandButton("cancelUpdate", $this->lng->txt("cancel"));
136        }
137
138        return $form;
139    }
140
141    /**
142    * Create new blog
143    */
144    public function create()
145    {
146        if (!$_POST["blog_id"]) {
147            $form = $this->initForm(true);
148            if ($form->checkInput()) {
149                return $this->insertPosting($_POST["blog"]);
150            }
151
152            $form->setValuesByPost();
153            return $this->insert($form);
154        } else {
155            $form = $this->initPostingForm($_POST["blog_id"], true);
156            if ($form->checkInput()) {
157                $this->content_obj = new ilPCBlog($this->getPage());
158                $this->content_obj->create($this->pg_obj, $this->hier_id, $this->pc_id);
159                $this->content_obj->setData($form->getInput("blog_id"), $form->getInput("posting"));
160                $this->updated = $this->pg_obj->update();
161                if ($this->updated === true) {
162                    $this->ctrl->returnToParent($this, "jump" . $this->hier_id);
163                }
164            }
165
166            $form->setValuesByPost();
167            return $this->insertPosting($_POST["blog_id"], $form);
168        }
169    }
170
171    /**
172    * Update blog
173    */
174    public function update()
175    {
176        if (!$_POST["blog_id"]) {
177            $form = $this->initForm();
178            if ($form->checkInput()) {
179                return $this->editPosting($_POST["blog"]);
180            }
181
182            $this->pg_obj->addHierIDs();
183            $form->setValuesByPost();
184            return $this->edit($form);
185        } else {
186            $form = $this->initPostingForm($_POST["blog_id"]);
187            if ($form->checkInput()) {
188                $this->content_obj->setData($form->getInput("blog_id"), $form->getInput("posting"));
189                $this->updated = $this->pg_obj->update();
190                if ($this->updated === true) {
191                    $this->ctrl->returnToParent($this, "jump" . $this->hier_id);
192                }
193            }
194
195            $this->pg_obj->addHierIDs();
196            $form->setValuesByPost();
197            return $this->editPosting($_POST["blog_id"], $form);
198        }
199    }
200
201
202    /**
203     * Insert new blog posting form.
204     *
205     * @param int $a_blog_id
206     * @param ilPropertyFormGUI $a_form
207     */
208    public function insertPosting($a_blog_id, ilPropertyFormGUI $a_form = null)
209    {
210        $tpl = $this->tpl;
211
212        $this->displayValidationError();
213
214        if (!$a_form) {
215            $a_form = $this->initPostingForm($a_blog_id, true);
216        }
217        $tpl->setContent($a_form->getHTML());
218    }
219
220    /**
221     * Edit blog posting form
222     *
223     * @param int $a_blog_id
224     * @param ilPropertyFormGUI $a_form
225     */
226    public function editPosting($a_blog_id, ilPropertyFormGUI $a_form = null)
227    {
228        $tpl = $this->tpl;
229
230        $this->displayValidationError();
231
232        if (!$a_form) {
233            $a_form = $this->initPostingForm($a_blog_id);
234        }
235        $tpl->setContent($a_form->getHTML());
236    }
237
238    /**
239     * Init blog posting form
240     *
241     * @param int $a_blog_id
242     * @param bool $a_insert
243     * @return ilPropertyFormGUI
244     */
245    protected function initPostingForm($a_blog_id, $a_insert = false)
246    {
247        $ilCtrl = $this->ctrl;
248        $ilUser = $this->user;
249
250        include_once("./Services/Form/classes/class.ilPropertyFormGUI.php");
251        $form = new ilPropertyFormGUI();
252        $form->setFormAction($ilCtrl->getFormAction($this));
253        if ($a_insert) {
254            $form->setTitle($this->lng->txt("cont_insert_blog"));
255        } else {
256            $form->setTitle($this->lng->txt("cont_update_blog"));
257        }
258
259        $options = array();
260        $postings = ilBlogPosting::getAllPostings($a_blog_id);
261        if ($postings) {
262            foreach ($postings as $post) {
263                // could be posting from someone else
264                if ($post["author"] == $ilUser->getId()) {
265                    $date = new ilDateTime($post["date"], IL_CAL_DATETIME);
266                    $title = $post["title"] . " - " .
267                        ilDatePresentation::formatDate($date);
268
269                    $cbox = new ilCheckboxInputGUI($title, "posting");
270                    $cbox->setValue($post["id"]);
271
272                    $options[] = $cbox;
273                }
274            }
275        }
276        asort($options);
277        $obj = new ilCheckboxGroupInputGUI($this->lng->txt("cont_pc_blog_posting"), "posting");
278        $obj->setRequired(true);
279        $obj->setOptions($options);
280        $form->addItem($obj);
281
282        $blog_id = new ilHiddenInputGUI("blog_id");
283        $blog_id->setValue($a_blog_id);
284        $form->addItem($blog_id);
285
286        if ($a_insert) {
287            $form->addCommandButton("create_blog", $this->lng->txt("save"));
288            $form->addCommandButton("cancelCreate", $this->lng->txt("cancel"));
289        } else {
290            $obj->setValue($this->content_obj->getPostings());
291            $form->addCommandButton("update", $this->lng->txt("save"));
292            $form->addCommandButton("cancelUpdate", $this->lng->txt("cancel"));
293        }
294
295        return $form;
296    }
297}
298