Esempio n. 1
0
 /**
  * Creates a new Post object and saves into database. Default title 'New Post x', x in {1, 2, ...}; default body 'Enter text here'
  * @param Db object <code>$db</code>, UserController object <code>$user_controller</code>, NestedCategoryController object <code>$nested_category_controller</code>
  */
 public function add(Db $db, UserController $user_controller, NestedCategoryController $nested_category_controller)
 {
     if (isset($_SESSION['nb_new_posts'])) {
         $_SESSION['nb_new_posts']++;
     } else {
         $_SESSION['nb_new_posts'] = 1;
     }
     $post = new Post();
     $post->set_title("New Post {$_SESSION['nb_new_posts']}");
     $post->set_body('Enter text here');
     $post->set_user_id($user_controller->get_user()->get_id());
     $nested_category_id = $nested_category_controller->get_nested_category()->get_id();
     $post->set_nested_category_id($nested_category_id);
     $post->save($db);
     $this->nested_category_id = $nested_category_id;
 }
 /**
  * Creates a new NestedCategory object and saves into database. If parent of new category contains posts, these posts are moved into new category
  * @param Db object <code>$db</code>, string <code>$name</code>, UserController object <code>$user_controller</code>
  */
 public function add(Db $db, $name, UserController $user_controller)
 {
     $user = $user_controller->get_user();
     $user_id = $user->get_id();
     $parent_id = $this->nested_category->get_id();
     $name_clean = filter_var($name, FILTER_SANITIZE_STRING);
     $nested_category = new NestedCategory();
     $nested_category->set_name($name_clean);
     $nested_category->set_user_id($user_id);
     if ($parent_id == null) {
         $max_droite = NestedCategory::max_droite($db, $user_id);
         if ($max_droite == null) {
             $max_droite = 0;
         }
         $nested_category->set_gauche($max_droite + 1);
         $nested_category->set_droite($max_droite + 2);
         $nested_category->save($db);
     } else {
         $nested_category->set_parent_id($parent_id);
         $nested_category->parent_gauche_droite($db);
         $parent_gauche = $nested_category->get_parent_gauche_droite()['gauche'];
         $parent_droite = $nested_category->get_parent_gauche_droite()['droite'];
         $nested_category->set_gauche((int) $parent_droite);
         $nested_category->set_droite($parent_droite + 1);
         $nested_category->save($db);
         // add posts to new category
         $post_results = Post::posts_in_nested_category($db, $parent_id);
         if (isset($post_results)) {
             $new_nested_category_id = NestedCategory::max_id($db, $user_id);
             foreach ($post_results as $post_result) {
                 $post = new Post();
                 $post->set_id((int) $post_result['id']);
                 $post->set_nested_category_id((int) $new_nested_category_id);
                 $post->save($db);
             }
         }
         // increase parent category droite value by two
         $parent_nested_category = new NestedCategory();
         $parent_nested_category->set_id($parent_id);
         $parent_nested_category->set_droite($parent_droite + 2);
         $parent_nested_category->save($db);
         // increase parent category's ancestors droite value by two
         $nested_category->parent_ancestors($db);
         if ($nested_category->get_parent_ancestors() !== null) {
             foreach ($nested_category->get_parent_ancestors() as $x) {
                 $parent_ancestor = new NestedCategory();
                 $parent_ancestor->set_id((int) $x['id']);
                 $parent_ancestor->set_droite($x['droite'] + 2);
                 $parent_ancestor->save($db);
             }
         }
         // for all categories where gauche is > parent category's droite value, increase their gauche and droite values by 2
         $nested_category->parent_siblings_and_parent_siblings_children($db);
         if ($nested_category->get_parent_siblings_and_parent_siblings_children() !== null) {
             foreach ($nested_category->get_parent_siblings_and_parent_siblings_children() as $y) {
                 $parent_sibling_or_parent_sibling_child = new NestedCategory();
                 $parent_sibling_or_parent_sibling_child->set_id((int) $y['id']);
                 $parent_sibling_or_parent_sibling_child->set_gauche($y['gauche'] + 2);
                 $parent_sibling_or_parent_sibling_child->set_droite($y['droite'] + 2);
                 $parent_sibling_or_parent_sibling_child->save($db);
             }
         }
     }
     $user->nested_categories($db);
     $_SESSION['user'] = serialize($user);
     // userProfileHome.php
     $this->username = $user->get_username();
     $this->user_nested_categories = $user->get_nested_categories();
 }
Esempio n. 3
0
 }
 if (isset($_SESSION['nested_category'])) {
     $nested_category = unserialize($_SESSION['nested_category']);
 } else {
     $nested_category = new NestedCategory();
 }
 // initialise controllers
 $user_controller = new UserController($user);
 $post_controller = new PostController($post);
 $nested_category_controller = new NestedCategoryController($nested_category);
 // route appropriate response as per values of <code>$controller</code> and <code>$action</code> set in index.php
 switch ($controller) {
     case 'user':
         switch ($action) {
             case 'login':
                 if ($user_controller->get_user()->get_auth()) {
                     // user already authorised. No need to recontact database
                     $user_login_view = new UserLoginView($user_controller);
                     $user_login_view->output();
                 } elseif (isset($_POST['ident']) && isset($_POST['pw'])) {
                     set_render_view($controller, $user_controller, $action, $db, $_POST['ident'], $_POST['pw']);
                 } else {
                     render($templates['404_error'], $user_controller);
                 }
                 break;
             case 'logout':
                 set_render_view($controller, $user_controller, $action);
                 break;
             case 'sign_up':
                 if (isset($_POST['username']) && isset($_POST['email_address']) && isset($_POST['new_pw'])) {
                     set_render_view($controller, $user_controller, $action, $db, $_POST['username'], $_POST['email_address'], $_POST['new_pw']);
Esempio n. 4
0
 /**
  * Sets value of <code>$write</code> property
  * @param Db object <code>$db</code>, UserController object <code>$user_controller</code>
  * Checks if given user can edit post
  */
 public function write(Db $db, UserController $user_controller)
 {
     $this->write = false;
     $user = $user_controller->get_user();
     if ($user->get_id() !== null && isset($this->id)) {
         $this->write = $user->post_belong_to_user($db, $this->id);
     }
 }