link_id = explode(",", $_POST['link_id']); $Links->user_id = $_SESSION['user']['id']; try { $Links->delete_link(); $links_count = count($Links->link_id); $msg = ($links_count > 1) ? $links_count.' links have been' : '1 link has been'; $msg .= ' deleted successfully'; $json_string = '{ "errors" : "'.$msg.'", "updated_category_id" : '.$_POST['category_id'].'}'; } catch (PAException $e) { $json_string = '{ "is_error" : true, "errors" : "'.$e->message.'"}'; } print $json_string; /** * Creating a new link under the list */ } else if (!empty($_POST) && $_POST['form_action'] == 'create_link') { //TODO: check for empty fields $Links = new Links(); $Links->user_id = $_SESSION['user']['id']; $Links->category_id = $_POST['category_id']; $Links->title = $_POST['title']; $Links->url = Validation::validate_url($_POST['url']); try { $Links->save_link(); $json_string = '{ "errors" : "Link has been added successfully", "updated_category_id" : '.$_POST['category_id'].'}'; } catch (PAException $e) { $json_string = '{ "is_error" : true, "errors" : "'.$e->message.'"}'; } print $json_string; } else if (!empty($_POST) && $_POST['form_action'] == 'remove_list') { $Links = new Links(); $Links->user_id = $_SESSION['user']['id']; $Links->category_id = $_POST['category_id']; try { $Links->delete_category(true); $json_string = '{ "errors" : "List has been deleted successfully"}'; } catch (PAException $e) { $json_string = '{ "is_error" : true, "errors" : "'.$e->message.'"}'; } print $json_string; /** * Updating the list or category of links */ } else if (!empty($_POST) && $_POST['form_action'] == 'edit_list') { if (!empty($_POST['updated_category_name'])) { $Links = new Links(); $Links->user_id = $_SESSION['user']['id']; $Links->category_name = $_POST['updated_category_name']; $Links->category_id = $_POST['category_id']; try { $Links->update_category(); $json_string = '{ "errors" : "List has been updated successfully", "updated_category_id" : '.$_POST['category_id'].'}'; } catch (PAException $e) { $json_string = '{ "is_error" : true, "errors" : "List with specified name already exists"}'; } print $json_string; } } else if (!empty($_POST) && $_POST['form_action'] == 'create_list') { $json_string = null; if (!empty($_POST['category_name'])) { $Links = new Links(); $Links->user_id = $_SESSION['user']['id']; $Links->category_name = $_POST['category_name']; try { $category_id = $Links->save_category(); $json_string = '{ "errors" : "List has saved successfully", "updated_category_id" : '.$category_id.'}'; } catch (PAException $e) { $json_string = '{ "is_error" : true, "errors" : "List with specified name already exists"}'; } print $json_string; } /** * Code for updating the selected links */ } else if (!empty($_POST) && $_POST['form_action'] == 'update_links') { $link_count = count($_POST['link_id_updated']); $Links = new Links(); $updated_links_count = 0; $Links->user_id = $_SESSION['user']['id']; $Links->category_id = $_POST['category_id']; for ($counter = 0; $counter < $link_count; $counter++) { $Links->title = $_POST['title_updated'][$counter]; $Links->link_id = $_POST['link_id_updated'][$counter]; $Links->url = $_POST['url_updated'][$counter]; try { $Links->update_link(); ++$updated_links_count; } catch (PAException $e) { $message[] = 'Link with title '.$_POST['title_updated'][$counter].' already exists'; } } $json_string = '{ "errors" : "'.uihelper_plural($updated_links_count, ' link').' out of '.$link_count.' updated successfully,'.implode(",", $message).'", "updated_category_id" : '.$_POST['category_id'].'}'; $message[] = array('category_id'=> $_POST['category_id']); array_unshift($message, uihelper_plural($updated_links_count, ' link').' out of '.$link_count.' updated successfully'); print_r($json_string); /** * Code for displaying the selected links for editing. */ } else if (!empty($_GET['action']) && $_GET['action'] == 'edit_list_link') { $Links = new Links(); $Links->user_id = $_SESSION['user']['id']; $Links->link_id = explode("_", $_GET['link_ids']); $links = $Links->load_link(); $edit_form = '
Edit links'; $links_count = count($links); $category_id = null; if ($links_count > 0) { foreach ($links as $link) { $category_id = $link->category_id; $edit_form .= '

'; } } $edit_form .= '
'; print $edit_form; } else if (!empty($_GET['action']) && $_GET['action'] == 'edit_list') { $Links = new Links(); $params = array('category_id'=>$_GET['category_id']); $category = $Links->load_category($params); $edit_form = '
Edit list

'; print $edit_form; } else if (!empty($_GET['category_id'])) { $condition = array('category_id'=> $_GET['category_id'], 'is_active'=> 1); $params_array = array('user_id'=> $_SESSION['user']['id']); $Links = new Links(); $Links->set_params ($params_array); $result_array = $Links->load_link ($condition); $return_string = ''; if(count($result_array) > 0) { for($counter =0; $counter < count($result_array); $counter++) { $return_string .= '

'.$result_array[$counter]->title.'

'.$result_array[$counter]->url.'
'; } $return_string .= '
'; } else { $return_string = "
There are no links under this list. Click here to add
"; } print $return_string; } ?>