public function saveForumLayoutEvent($runData)
 {
     $site = $runData->getTemp("site");
     $pl = $runData->getParameterList();
     $json = new JSONService(SERVICES_JSON_LOOSE_TYPE);
     $cats0 = $json->decode($pl->getParameterValue("categories"));
     $groups0 = $json->decode($pl->getParameterValue("groups"));
     $db = Database::connection();
     $db->begin();
     // compare against stored groups and categories. add if necessary, delete if necessary etc.
     for ($i = 0; $i < count($groups0); $i++) {
         $group = $groups0[$i];
         $g = null;
         if ($group['group_id'] == null) {
             // new group, add to database!
             $g = new DB_ForumGroup();
             $g->setName(trim($group['name']));
             $g->setDescription(trim($group['description']));
             $g->setVisible($group['visible']);
             $g->setSiteId($site->getSiteId());
             $g->setSortIndex($i);
             $g->save();
         } else {
             $c = new Criteria();
             $c->add("site_id", $site->getSiteId());
             $c->add("group_id", $group['group_id']);
             $g = DB_ForumGroupPeer::instance()->selectOne($c);
             if ($g == null) {
                 throw new ProcessException(_("Error fatching one of the forum groups."));
             }
             // update values
             $changed = false;
             if ($g->getName() !== trim($group['name'])) {
                 $g->setName(trim($group['name']));
                 $changed = true;
             }
             if ($g->getDescription() !== trim($group['description'])) {
                 $g->setDescription(trim($group['description']));
                 $changed = true;
             }
             if ($g->getVisible() !== $group['visible']) {
                 $g->setVisible($group['visible']);
                 $changed = true;
             }
             if ($g->getSortIndex() !== $i) {
                 $g->setSortIndex($i);
                 $changed = true;
             }
             if ($changed) {
                 $g->save();
             }
         }
         // now proceed with categories for this group!!!
         $cates = $cats0[$i];
         for ($j = 0; $j < count($cates); $j++) {
             $cat = $cates[$j];
             if ($cat['category_id'] == null) {
                 // new category!
                 $ca = new DB_ForumCategory();
                 $ca->setName(trim($cat['name']));
                 $ca->setDescription(trim($cat['description']));
                 $ca->setMaxNestLevel($cat['max_nest_level']);
                 $ca->setSiteId($site->getSiteId());
                 $ca->setGroupId($g->getGroupId());
                 $ca->setSortIndex($j);
                 $ca->save();
             } else {
                 $c = new Criteria();
                 $c->add("site_id", $site->getSiteId());
                 $c->add("category_id", $cat['category_id']);
                 $ca = DB_ForumCategoryPeer::instance()->selectOne($c);
                 if ($ca == null) {
                     throw new ProcessException(_("Error fatching one of the forum categories."));
                 }
                 $changed = false;
                 if ($ca->getName() !== trim($cat['name'])) {
                     $ca->setName(trim($cat['name']));
                     $changed = true;
                 }
                 if ($ca->getDescription() !== trim($cat['description'])) {
                     $ca->setDescription(trim($cat['description']));
                     $changed = true;
                 }
                 if ($ca->getMaxNestLevel() !== $cat['max_nest_level']) {
                     $ca->setMaxNestLevel($cat['max_nest_level']);
                     $changed = true;
                 }
                 if ($ca->getSortIndex() !== $j) {
                     $ca->setSortIndex($j);
                     $changed = true;
                 }
                 if ($ca->getGroupId() != $g->getGroupId()) {
                     $ca->setGroupId($g->getGroupId());
                     $changed = true;
                 }
                 if ($changed) {
                     $ca->save();
                 }
             }
         }
     }
     // and deleted categories
     $dcats = $json->decode($pl->getParameterValue("deleted_categories"));
     foreach ($dcats as $dcat) {
         $c = new Criteria();
         $c->add("site_id", $site->getSiteId());
         $c->add("category_id", $dcat);
         // check if empty
         $cacount = DB_ForumThreadPeer::instance()->selectCount($c);
         if ($cacount > 0) {
             throw new ProcessException(_("One of the categories marked for deletation was not empty."));
         }
         DB_ForumCategoryPeer::instance()->delete($c);
     }
     // now process deleted groups...
     $dgroups = $json->decode($pl->getParameterValue("deleted_groups"));
     for ($i = 0; $i < count($dgroups); $i++) {
         $group = $dgroups[$i];
         // check if has group_id - if not, this should not be in the database...
         if ($group['group_id'] !== null) {
             $c = new Criteria();
             $c->add("site_id", $site->getSiteId());
             $c->add("group_id", $group['group_id']);
             $cacount = DB_ForumCategoryPeer::instance()->selectCount($c);
             if ($cacount > 0) {
                 throw new ProcessException(_("One of the groups marked for deletation was not empty."));
             }
             DB_ForumGroupPeer::instance()->delete($c);
         }
     }
     // and deleted categories
     $dcats = $json->decode($pl->getParameterValue("deleted_categories"));
     foreach ($dcats as $dcat) {
         $c = new Criteria();
         $c->add("site_id", $site->getSiteId());
         $c->add("category_id", $dcat);
         // check if empty
         $cacount = DB_ForumThreadPeer::instance()->selectCount($c);
         if ($cacount > 0) {
             throw new ProcessException(_("One of the categories marked for deletation was not empty."));
         }
         DB_ForumCategoryPeer::instance()->delete($c);
     }
     $outdater = new Outdater();
     $outdater->forumEvent("outdate_forum");
     $db->commit();
     if (GlobalProperties::$UI_SLEEP) {
         sleep(1);
     }
 }
 public function build($runData)
 {
     $site = $runData->getTemp("site");
     $page = $runData->getTemp("page");
     $pl = $runData->getParameterList();
     if ($page == null) {
         $pageId = $pl->getParameterValue("pageId");
         if ($pageId !== null && is_numeric($pageId)) {
             $page = DB_PagePeer::instance()->selectByPrimaryKey($pageId);
         } else {
             $pageName = $runData->getTemp("pageUnixName");
             $site = $runData->getTemp("site");
             $page = DB_PagePeer::instance()->selectByName($site->getSiteId(), $pageName);
         }
         if ($page == null || $page->getSiteId() !== $site->getSiteId()) {
             throw new ProcessException(_("Can not find related page."), "no_page");
         }
     }
     // check for a discussion thread. if not exists - create it!
     $c = new Criteria();
     $c->add("page_id", $page->getPageId());
     $c->add("site_id", $site->getSiteId());
     $thread = DB_ForumThreadPeer::instance()->selectOne($c);
     if ($thread == null) {
         // create thread!!!
         $c = new Criteria();
         $c->add("site_id", $site->getSiteId());
         $c->add("per_page_discussion", true);
         $category = DB_ForumCategoryPeer::instance()->selectOne($c);
         if ($category == null) {
             // create this category!
             $category = new DB_ForumCategory();
             $category->setName(_("Per page discussions"));
             $category->setDescription(_("This category groups discussions related to particular pages within this site."));
             $category->setPerPageDiscussion(true);
             $category->setSiteId($site->getSiteId());
             // choose group. create one?
             $c = new Criteria();
             $c->add("site_id", $site->getSiteId());
             $c->add("name", "Hidden");
             $group = DB_ForumGroupPeer::instance()->selectOne($c);
             if ($group == null) {
                 $group = new DB_ForumGroup();
                 $group->setName(_("Hidden"));
                 $group->setDescription(_("Hidden group used for storing some discussion threads."));
                 $group->setSiteId($site->getSiteId());
                 $group->setVisible(false);
                 $group->save();
             }
             $category->setGroupId($group->getGroupId());
             $category->save();
         }
         // now create thread...
         $thread = new DB_ForumThread();
         $thread->setCategoryId($category->getCategoryId());
         $thread->setSiteId($site->getSiteId());
         $thread->setPageId($page->getPageId());
         $thread->setUserId(-1);
         $thread->setDateStarted(new ODate());
         $thread->setNumberPosts(0);
         $thread->save();
         $page->setThreadId($thread->getThreadId());
         $page->save();
         $category->setNumberThreads($category->getNumberThreads() + 1);
         $category->save();
     } else {
         $category = $thread->getForumCategory();
     }
     $this->threadId = $thread->getThreadId();
     $c = new Criteria();
     $c->add("thread_id", $thread->getThreadId());
     $c->add("site_id", $site->getSiteId());
     $c->addJoin("user_id", "ozone_user.user_id");
     $c->addOrderAscending("post_id");
     $posts = DB_ForumPostPeer::instance()->select($c);
     // make a mapping first.
     $map = array();
     $levels = array();
     foreach ($posts as $post) {
         $parentId = $post->getParentId();
         $postId = $post->getPostId();
         if ($parentId === null) {
             // if no parrent - simply add at the end of $map
             $map[] = $postId;
             $levels[$postId] = 0;
         } else {
             // find a parent
             $cpos = array_search($parentId, $map);
             $clevel = $levels[$parentId];
             // find a place for the post, i.e. the place where level_next == level or the end of array
             $cpos++;
             while (isset($map[$cpos]) && $levels[$map[$cpos]] > $clevel) {
                 $cpos++;
             }
             // insert at this position!!!
             array_splice($map, $cpos, 0, $postId);
             $levels[$postId] = $clevel + 1;
         }
     }
     // create container control list
     $cc = array();
     foreach ($map as $pos => $m) {
         // open if previous post has LOWER level
         $clevel = $levels[$m];
         if (isset($map[$pos + 1])) {
             $nlevel = $levels[$map[$pos + 1]];
             if ($nlevel > $clevel) {
                 $cc[$pos] = 'k';
             }
             if ($nlevel < $clevel) {
                 $cc[$pos] = str_repeat('c', $clevel - $nlevel);
             }
         } else {
             $cc[$pos] = str_repeat('c', $clevel);
         }
     }
     $runData->contextAdd("postmap", $map);
     $runData->contextAdd("levels", $levels);
     $runData->contextAdd("containerControl", $cc);
     $runData->contextAdd("thread", $thread);
     $runData->contextAdd("category", $category);
     $runData->contextAdd("posts", $posts);
     $runData->ajaxResponseAdd("threadId", $thread->getThreadId());
 }
Beispiel #3
0
 public function createPageDiscussionThreadEvent($runData)
 {
     // ok, the page has no discussion yet... but check it!
     $site = $runData->getTemp("site");
     $pl = $runData->getParameterList();
     $pageId = $pl->getParameterValue("page_id");
     $page = DB_PagePeer::instance()->selectByPrimaryKey($pageId);
     if ($page == null || $page->getSiteId() != $site->getSiteId()) {
         throw new ProcessException(_("Page does not exist."), "no_page");
     }
     $db = Database::connection();
     $db->begin();
     $c = new Criteria();
     $c->add("page_id", $pageId);
     $c->add("site_id", $site->getSiteId());
     $thread = DB_ForumThreadPeer::instance()->selectOne($c);
     if ($thread) {
         // thread exists! which means it could have been created meanwhile!
         // simply return the thread it now.
         $runData->ajaxResponseAdd("thread_id", $thread->getThreadId());
         $runData->ajaxResponseAdd("thread_unix_title", $thread->getUnixifiedTitle());
         $db->commit();
         return;
     }
     // thread does not exist. check if category with page discussions exist.
     $c = new Criteria();
     $c->add("site_id", $site->getSiteId());
     $c->add("per_page_discussion", true);
     $category = DB_ForumCategoryPeer::instance()->selectOne($c);
     if ($category == null) {
         // create this category!
         $category = new DB_ForumCategory();
         $category->setName("Per page discussions");
         $category->setDescription(_("This category groups discussions related to particular pages within this site."));
         $category->setPerPageDiscussion(true);
         $category->setSiteId($site->getSiteId());
         // choose group. create one?
         $c = new Criteria();
         $c->add("site_id", $site->getSiteId());
         $c->add("name", "Hidden");
         $group = DB_ForumGroupPeer::instance()->selectOne($c);
         if ($group == null) {
             $group = new DB_ForumGroup();
             $group->setName("Hidden");
             $group->setDescription(_("Hidden group used for storing some discussion threads."));
             $group->setSiteId($site->getSiteId());
             $group->setVisible(false);
             $group->save();
         }
         $category->setGroupId($group->getGroupId());
         $category->save();
     }
     // now create thread...
     $thread = new DB_ForumThread();
     $thread->setCategoryId($category->getCategoryId());
     $thread->setSiteId($site->getSiteId());
     $thread->setPageId($pageId);
     $thread->setUserId(-1);
     $thread->setDateStarted(new ODate());
     $thread->setNumberPosts(0);
     $thread->save();
     $page->setThreadId($thread->getThreadId());
     $page->save();
     $category->setNumberThreads($category->getNumberThreads() + 1);
     $category->save();
     $runData->ajaxResponseAdd("thread_id", $thread->getThreadId());
     $runData->ajaxResponseAdd("thread_unix_title", $thread->getUnixifiedTitle());
     $o = new Outdater();
     $o->forumEvent("thread_save", $thread);
     $db->commit();
 }