function index(Request $request, Application $app)
 {
     $userGroup = new UserGroupModel();
     $form = $app['form.factory']->create(new AdminUserGroupNewForm($app['config']), $userGroup);
     if ('POST' == $request->getMethod()) {
         $form->bind($request);
         if ($form->isValid()) {
             $ugRepository = new UserGroupRepository();
             $ugRepository->createForIndex($userGroup, $app['currentUser']);
             return $app->redirect("/sysadmin/usergroup/" . $userGroup->getId());
         }
     }
     return $app['twig']->render('sysadmin/usergroupnew/index.html.twig', array('form' => $form->createView()));
 }
 public function create(SiteModel $site, UserAccountModel $owner, $countries, SiteQuotaModel $siteQuota, $canAnyUserVerifiedEdit = false)
 {
     global $DB, $CONFIG, $EXTENSIONHOOKRUNNER;
     $createdat = \TimeSource::getFormattedForDataBase();
     if (!$site->isSlugValid($site->getSlug(), $CONFIG)) {
         throw new Exception("Slug not valid");
     }
     try {
         $DB->beginTransaction();
         // TODO should check slug not already exist and nice error
         $timezones = array();
         foreach ($countries as $country) {
             foreach (explode(",", $country->getTimezones()) as $timeZone) {
                 $timezones[] = $timeZone;
             }
         }
         $site->setCachedTimezonesAsList($timezones);
         $site->setCachedIsMultipleCountries(count($countries) > 1);
         // Site
         $stat = $DB->prepare("INSERT INTO site_information (title, slug, slug_canonical, " . "created_at,cached_is_multiple_timezones,cached_is_multiple_countries," . "cached_timezones,is_feature_map,is_feature_importer,is_feature_curated_list," . "is_listed_in_index,is_web_robots_allowed, " . " prompt_emails_days_in_advance,site_quota_id, " . "is_feature_tag,is_feature_physical_events,is_feature_virtual_events) " . "VALUES (:title, :slug, :slug_canonical, " . " :created_at,:cached_is_multiple_timezones,:cached_is_multiple_countries," . ":cached_timezones,:is_feature_map,:is_feature_importer,:is_feature_curated_list," . ":is_listed_in_index,:is_web_robots_allowed, " . " :prompt_emails_days_in_advance, :site_quota_id, " . ":is_feature_tag,:is_feature_physical_events,:is_feature_virtual_events) RETURNING id");
         $stat->execute(array('title' => substr($site->getTitle(), 0, VARCHAR_COLUMN_LENGTH_USED), 'slug' => $site->getSlug(), 'slug_canonical' => SiteModel::makeCanonicalSlug($site->getSlug()), 'cached_is_multiple_timezones' => $site->getCachedIsMultipleTimezones() ? 1 : 0, 'cached_is_multiple_countries' => $site->getCachedIsMultipleCountries() ? 1 : 0, 'cached_timezones' => $site->getCachedTimezones(), 'created_at' => $createdat, 'is_feature_curated_list' => $site->getIsFeatureCuratedList() ? 1 : 0, 'is_feature_importer' => $site->getIsFeatureImporter() ? 1 : 0, 'is_feature_map' => $site->getIsFeatureMap() ? 1 : 0, 'is_feature_tag' => $site->getIsFeatureTag() ? 1 : 0, 'is_feature_virtual_events' => $site->getIsFeatureVirtualEvents() ? 1 : 0, 'is_feature_physical_events' => $site->getIsFeaturePhysicalEvents() ? 1 : 0, 'is_listed_in_index' => $site->getIsListedInIndex() ? 1 : 0, 'is_web_robots_allowed' => $site->getIsWebRobotsAllowed() ? 1 : 0, 'prompt_emails_days_in_advance' => $site->getPromptEmailsDaysInAdvance(), 'site_quota_id' => $siteQuota->getId()));
         $data = $stat->fetch();
         $site->setId($data['id']);
         $stat = $DB->prepare("INSERT INTO site_history (site_id, user_account_id, " . "title, slug, slug_canonical, created_at,is_feature_map,is_feature_importer," . "is_feature_curated_list,is_listed_in_index,is_web_robots_allowed, " . " prompt_emails_days_in_advance, is_new," . "is_feature_tag,is_feature_physical_events,is_feature_virtual_events) " . "VALUES (:site_id, :user_account_id, :title, " . ":slug, :slug_canonical,  :created_at,:is_feature_map,:is_feature_importer," . ":is_feature_curated_list,:is_listed_in_index,:is_web_robots_allowed, " . " :prompt_emails_days_in_advance, '1', " . ":is_feature_tag,:is_feature_physical_events,:is_feature_virtual_events)");
         $stat->execute(array('site_id' => $site->getId(), 'user_account_id' => $owner->getId(), 'title' => substr($site->getTitle(), 0, VARCHAR_COLUMN_LENGTH_USED), 'slug' => $site->getSlug(), 'slug_canonical' => SiteModel::makeCanonicalSlug($site->getSlug()), 'created_at' => $createdat, 'is_feature_curated_list' => $site->getIsFeatureCuratedList() ? 1 : 0, 'is_feature_importer' => $site->getIsFeatureImporter() ? 1 : 0, 'is_feature_map' => $site->getIsFeatureMap() ? 1 : 0, 'is_feature_tag' => $site->getIsFeatureTag() ? 1 : 0, 'is_feature_virtual_events' => $site->getIsFeatureVirtualEvents() ? 1 : 0, 'is_feature_physical_events' => $site->getIsFeaturePhysicalEvents() ? 1 : 0, 'is_listed_in_index' => $site->getIsListedInIndex() ? 1 : 0, 'is_web_robots_allowed' => $site->getIsWebRobotsAllowed() ? 1 : 0, 'prompt_emails_days_in_advance' => $site->getPromptEmailsDaysInAdvance()));
         // Permissions
         $ugr = new UserGroupRepository();
         $userGroupEditors = new UserGroupModel();
         $userGroupEditors->setTitle("Editors");
         $userGroupEditors->setIsIncludesVerifiedUsers($canAnyUserVerifiedEdit);
         $ugr->createForSite($site, $userGroupEditors, $owner, array(array('org.openacalendar', 'CALENDAR_CHANGE')), array($owner));
         $userGroupEditors = new UserGroupModel();
         $userGroupEditors->setTitle("Administrators");
         $ugr->createForSite($site, $userGroupEditors, $owner, array(array('org.openacalendar', 'CALENDAR_ADMINISTRATE')), array($owner));
         // Countries!
         $stat = $DB->prepare("INSERT INTO country_in_site_information (site_id,country_id,is_in,is_previously_in,created_at) VALUES (:site_id,:country_id,'1','1',:created_at)");
         foreach ($countries as $country) {
             $stat->execute(array('country_id' => $country->getId(), 'site_id' => $site->getId(), 'created_at' => $createdat));
         }
         $stat = $DB->prepare("INSERT INTO user_watches_site_information (user_account_id,site_id,is_watching,is_was_once_watching,last_watch_started,created_at) " . "VALUES (:user_account_id,:site_id,:is_watching,:is_was_once_watching,:last_watch_started,:created_at)");
         $stat->execute(array('user_account_id' => $owner->getId(), 'site_id' => $site->getId(), 'is_watching' => '1', 'is_was_once_watching' => '1', 'created_at' => \TimeSource::getFormattedForDataBase(), 'last_watch_started' => \TimeSource::getFormattedForDataBase()));
         $DB->commit();
         $EXTENSIONHOOKRUNNER->afterSiteCreate($site, $owner);
     } catch (Exception $e) {
         $DB->rollBack();
     }
 }
 function index($id, Request $request, Application $app)
 {
     $this->build($id, $request, $app);
     if ($request->request->get('action') == "addpermission" && $request->request->get('CSFRToken') == $app['websession']->getCSFRToken()) {
         $extension = $app['extensions']->getExtensionById($request->request->get("extension"));
         if ($extension) {
             $permission = $extension->getUserPermission($request->request->get("permission"));
             if ($permission) {
                 $ugr = new UserGroupRepository();
                 $ugr->addPermissionToGroup($permission, $this->parameters['usergroup'], $app['currentUser']);
                 return $app->redirect('/sysadmin/usergroup/' . $this->parameters['usergroup']->getId());
             }
         }
     } else {
         if ($request->request->get('action') == "removepermission" && $request->request->get('CSFRToken') == $app['websession']->getCSFRToken()) {
             $extension = $app['extensions']->getExtensionById($request->request->get("extension"));
             if ($extension) {
                 $permission = $extension->getUserPermission($request->request->get("permission"));
                 if ($permission) {
                     $ugr = new UserGroupRepository();
                     $ugr->removePermissionFromGroup($permission, $this->parameters['usergroup'], $app['currentUser']);
                     return $app->redirect('/sysadmin/usergroup/' . $this->parameters['usergroup']->getId());
                 }
             }
         }
     }
     $form = $app['form.factory']->create(new ActionForm());
     if ('POST' == $request->getMethod()) {
         $form->bind($request);
         if ($form->isValid()) {
             $data = $form->getData();
             $action = new ActionParser($data['action']);
             if ($action->getCommand() == 'addusername') {
                 $uar = new UserAccountRepository();
                 $user = $uar->loadByUserName($action->getParam(0));
                 if ($user) {
                     $ugr = new UserGroupRepository();
                     $ugr->addUserToGroup($user, $this->parameters['usergroup'], $app['currentUser']);
                     return $app->redirect('/sysadmin/usergroup/' . $this->parameters['usergroup']->getId());
                 }
             } else {
                 if ($action->getCommand() == 'removeusername') {
                     $uar = new UserAccountRepository();
                     $user = $uar->loadByUserName($action->getParam(0));
                     if ($user) {
                         $ugr = new UserGroupRepository();
                         $ugr->removeUserFromGroup($user, $this->parameters['usergroup'], $app['currentUser']);
                         return $app->redirect('/sysadmin/usergroup/' . $this->parameters['usergroup']->getId());
                     }
                 } else {
                     if ($action->getCommand() == 'includesanonymous') {
                         $ugr = new UserGroupRepository();
                         $this->parameters['usergroup']->setIsIncludesAnonymous($action->getParamBoolean(0));
                         $ugr->editIsIncludesAnonymous($this->parameters['usergroup'], $app['currentUser']);
                         return $app->redirect('/sysadmin/usergroup/' . $this->parameters['usergroup']->getId());
                     } else {
                         if ($action->getCommand() == 'includesusers') {
                             $ugr = new UserGroupRepository();
                             $this->parameters['usergroup']->setIsIncludesUsers($action->getParamBoolean(0));
                             $ugr->editIsIncludesUser($this->parameters['usergroup'], $app['currentUser']);
                             return $app->redirect('/sysadmin/usergroup/' . $this->parameters['usergroup']->getId());
                         } else {
                             if ($action->getCommand() == 'includesverifiedusers') {
                                 $ugr = new UserGroupRepository();
                                 $this->parameters['usergroup']->setIsIncludesVerifiedUsers($action->getParamBoolean(0));
                                 $ugr->editIsIncludesVerifiedUser($this->parameters['usergroup'], $app['currentUser']);
                                 return $app->redirect('/sysadmin/usergroup/' . $this->parameters['usergroup']->getId());
                             }
                         }
                     }
                 }
             }
         }
     }
     $this->parameters['form'] = $form->createView();
     $urb = new UserAccountRepositoryBuilder();
     $urb->setInUserGroup($this->parameters['usergroup']);
     $this->parameters['users'] = $urb->fetchAll();
     $r = new UserPermissionsRepository($app['extensions']);
     $this->parameters['userpermissions'] = $r->getPermissionsForUserGroup($this->parameters['usergroup'], false);
     $this->parameters['userpermissionstoadd'] = array();
     foreach ($app['extensions']->getExtensionsIncludingCore() as $ext) {
         foreach ($ext->getUserPermissions() as $key) {
             $per = $ext->getUserPermission($key);
             if ($per->isForIndex() && !in_array($per, $this->parameters['userpermissions'])) {
                 $this->parameters['userpermissionstoadd'][] = $per;
             }
         }
     }
     return $app['twig']->render('sysadmin/usergroup/index.html.twig', $this->parameters);
 }
 function users($id, Request $request, Application $app)
 {
     if (!$this->build($id, $request, $app)) {
         $app->abort(404, "User Group does not exist.");
     }
     if ($request->request->get('action') == "removeuser" && $request->request->get('CSFRToken') == $app['websession']->getCSFRToken()) {
         $ur = new UserAccountRepository();
         $user = $ur->loadById($request->request->get('id'));
         if ($user) {
             $ugr = new UserGroupRepository();
             $ugr->removeUserFromGroup($user, $this->parameters['usergroup'], $app['currentUser']);
             return $app->redirect('/admin/usergroup/' . $this->parameters['usergroup']->getId() . '/users');
         }
     } else {
         if ($request->request->get('action') == "adduser" && $request->request->get('CSFRToken') == $app['websession']->getCSFRToken()) {
             $ur = new UserAccountRepository();
             $user = $ur->loadByUserName($request->request->get('username'));
             if ($user) {
                 $ugr = new UserGroupRepository();
                 $ugr->addUserToGroup($user, $this->parameters['usergroup'], $app['currentUser']);
                 return $app->redirect('/admin/usergroup/' . $this->parameters['usergroup']->getId() . '/users');
             } else {
                 $app['flashmessages']->addError("Could not find user");
             }
         } else {
             if ($request->request->get('action') == "removeanonymous" && $request->request->get('CSFRToken') == $app['websession']->getCSFRToken()) {
                 $this->parameters['usergroup']->setIsIncludesAnonymous(false);
                 $ugr = new UserGroupRepository();
                 $ugr->editIsIncludesAnonymous($this->parameters['usergroup'], $app['currentUser']);
                 return $app->redirect('/admin/usergroup/' . $this->parameters['usergroup']->getId() . '/users');
             } else {
                 if ($request->request->get('action') == "addanonymous" && $request->request->get('CSFRToken') == $app['websession']->getCSFRToken()) {
                     $this->parameters['usergroup']->setIsIncludesAnonymous(true);
                     $ugr = new UserGroupRepository();
                     $ugr->editIsIncludesAnonymous($this->parameters['usergroup'], $app['currentUser']);
                     return $app->redirect('/admin/usergroup/' . $this->parameters['usergroup']->getId() . '/users');
                 } else {
                     if ($request->request->get('action') == "removeusers" && $request->request->get('CSFRToken') == $app['websession']->getCSFRToken()) {
                         $this->parameters['usergroup']->setIsIncludesUsers(false);
                         $ugr = new UserGroupRepository();
                         $ugr->editIsIncludesUser($this->parameters['usergroup'], $app['currentUser']);
                         return $app->redirect('/admin/usergroup/' . $this->parameters['usergroup']->getId() . '/users');
                     } else {
                         if ($request->request->get('action') == "addusers" && $request->request->get('CSFRToken') == $app['websession']->getCSFRToken()) {
                             $this->parameters['usergroup']->setIsIncludesUsers(true);
                             $ugr = new UserGroupRepository();
                             $ugr->editIsIncludesUser($this->parameters['usergroup'], $app['currentUser']);
                             return $app->redirect('/admin/usergroup/' . $this->parameters['usergroup']->getId() . '/users');
                         } else {
                             if ($request->request->get('action') == "removeverifiedusers" && $request->request->get('CSFRToken') == $app['websession']->getCSFRToken()) {
                                 $this->parameters['usergroup']->setIsIncludesVerifiedUsers(false);
                                 $ugr = new UserGroupRepository();
                                 $ugr->editIsIncludesVerifiedUser($this->parameters['usergroup'], $app['currentUser']);
                                 return $app->redirect('/admin/usergroup/' . $this->parameters['usergroup']->getId() . '/users');
                             } else {
                                 if ($request->request->get('action') == "addverifiedusers" && $request->request->get('CSFRToken') == $app['websession']->getCSFRToken()) {
                                     $this->parameters['usergroup']->setIsIncludesVerifiedUsers(true);
                                     $ugr = new UserGroupRepository();
                                     $ugr->editIsIncludesVerifiedUser($this->parameters['usergroup'], $app['currentUser']);
                                     return $app->redirect('/admin/usergroup/' . $this->parameters['usergroup']->getId() . '/users');
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     $urb = new UserAccountRepositoryBuilder();
     $urb->setInUserGroup($this->parameters['usergroup']);
     $this->parameters['users'] = $urb->fetchAll();
     $r = new UserPermissionsRepository($app['extensions']);
     $this->parameters['userpermissions'] = $r->getPermissionsForUserGroup($this->parameters['usergroup'], false);
     return $app['twig']->render('site/adminusergroup/users.html.twig', $this->parameters);
 }
 function edit($id, Request $request, Application $app)
 {
     if (!$this->build($id, $request, $app)) {
         $app->abort(404, "User Group does not exist.");
     }
     $form = $app['form.factory']->create(new UserGroupEditForm($app), $this->parameters['usergroup']);
     if ('POST' == $request->getMethod()) {
         $form->bind($request);
         if ($form->isValid()) {
             $ugr = new UserGroupRepository();
             $meta = new UserGroupEditMetaDataModel();
             $meta->setUserAccount($app['currentUser']);
             $meta->setFromRequest($request);
             $ugr->editTitleWithMetaData($this->parameters['usergroup'], $meta);
             return $app->redirect("/admin/usergroup/" . $this->parameters['usergroup']->getId());
         }
     }
     $this->parameters['form'] = $form->createView();
     return $app['twig']->render('site/adminusergroup/edit.html.twig', $this->parameters);
 }