<commit_before><?php defined('SYSPATH') or die('No direct script access.');
/***********************************************************
* Menuedit.php - Controller
* This software is copy righted by Kobo 2013
* Writen by <NAME>, Etherton Technologies <http://ethertontech.com>
* Started on 2013-03-22
* Creating custom menu items
*************************************************************/

class Controller_Menuedit extends Controller_Loggedin {

	
	public function before()
	{
		parent::before();	
		
		$auth = Auth::instance();
		//is the user logged in?
		if($auth->logged_in('admin'))
		{
			$this->session = Session::instance();
			//if auto rendered set this up
			if ($this->auto_render)
			{
				$data = array(
						'text' => '',
						'image_url' => '',
						'item_url' => '',
						'id' => isset($_GET['id']) ? intval($_GET['id']) : '__HOME__',
						'menuString' => '',
				);
				
				$pages = ORM::factory('Custompage')->
				where('user_id', '=', $this->user->id)->
				find_all();

				$submenus = array();
				$menus = array();

				$menu = ORM::factory('Menus')->find_all();
				foreach($menu as $m){
					$menus[] = $m;
				}

				foreach($menus as $menu){
					$sub = ORM::factory('Menuitem')->
					where('menu', '=', $menu->id)->
					find_all();

					$submenuArray = array();

					foreach($sub as $s){
						$submenuArray[] = $s;
					}

					$submenus[$menu->id] = $submenuArray;
				}
				
				//grab all of the pages that are in the database from the start, such as main/about/help/support
				$default = ORM::factory('Custompage')->
				where('user_id', '=', 1)->
				find_all();

				//get the menus for the custompage
				$custompage = ORM::factory('Menus')->
				where('title', '=', 'custompage')->
				find_all();
				
				//default pages being parsed into selection field
				$page_array = array();
				foreach($default as $main){
					$page_array[$main->slug][0] = __('New Submenu in').' '.$main->slug;
						$main->slug = $this->flip($main->slug);

						$menu = ORM::factory('Menus')->
						where('title', '=', $main->slug)->
						find();

						$sub = ORM::factory('Menuitem')->
						where('menu', '=', $menu->id)->
						find_all();

						$main->slug = $this->flip($main->slug);
						
						foreach($sub as $s){
							$page_array[$main->slug][$s->id] = $s->text;
						}
				}
				//custom created pages being parsed into selection field
				foreach($pages as $page){
					$page_array[$page->slug][0] = __('New Submenu in').' '.$page->slug;
					$menu = ORM::factory('Menus')->
						where('title', '=', $page->slug)->
						find();

					$sub = ORM::factory('Menuitem')->
						where('menu', '=', $menu->id)->
						find_all();
					foreach($sub as $s){
							$page_array[$page->slug][$s->id] = $s->text;
					}
				}
				
				
				$this->template->header->menu_page = "custompage";
				//make messages roll up when done
				$this->template->html_head->messages_roll_up = true;
				$this->template->html_head->script_views[] = view::factory('js/messages');
				$this->template->content = new View('menuedit/main');
				$this->template->html_head->title = __("Menus Page");
				$this->template->html_head->script_views[] = new View('menuedit/main_js');
				$this->template->content->errors = array();
				$this->template->content->messages = array();
				$this->template->content->data = $data;
				$this->template->content->pages = $page_array;
				$this->template->content->menus = $menus;
				$this->template->content->submenus = $submenus;
			}
		}
	}
	/**
	* where users go to edit custom menus
	*/
	public function action_index()
	{
		$auth = Auth::instance();
		//only admins should be allowed to see the page in the first place, and if not, are redirected to mymaps
		if(!$auth->logged_in('admin'))
		{
			HTTP::redirect('mymaps');
		}

		if(!empty($_POST)){
		
			if($_POST['action'] == 'delete'){
				$sub = ORM::factory('Menuitem')->
				where('text', '=', $_POST['text'])->
				find();
				$response = Model_Menuitem::delete_menuitem($sub->id);
			
				$this->template->content->messages[] = __('Deleted the menu item').' '.$_POST['text'];
				unset($this->template->content->pages[$_POST['pages']]);
			}
			else{
			//if they are creating a new menu or menuitem
				if($_POST['text'] == '' || $_POST['item_url'] == ''){
					$this->template->content->errors[] = __('Title and link url cannot be empty.');
					$data['text'] = $_POST['text'];
					$data['item_url'] = $_POST['item_url'];
					$data['image_url'] = $_POST['image_url'];
					$data['id'] = $_POST['pages'];
					$data['menuString'] = '';
					$this->template->content->data = $data;
				}
				
				//if submenu doesn't exist
				if($_POST['pages'] == 0){
					$length = strlen(__('New Submenu in')) + 1;
					//string will be the menu in which to place this
					$string = substr($_POST['menuString'], $length);

					$menu = ORM::factory('Menus')->
					where('title', '=', $this->flip($string))->
					find();

					if($menu->loaded()){
						$sub = ORM::factory('Menuitem')->
						where('text', '=', $_POST['text'])->
						find();

						if(!$sub->loaded()){
							//load the image
							$sub->text = $_POST['text'];
							$sub->item_url = URL::base(TRUE, TRUE).$_POST['item_url'];
							$sub->menu = $menu->id;
							$sub->save();
							
							$_POST['image_url'] = $_FILES['file']['name'];
							if($_FILES['file']['name'] != '')
							{
								$filename = $this->_save_file($_FILES['file'], $menu, $sub);
							}
							

							if($filename !== false){
								$sub->image_url = $filename;
							}
							$sub->save();

							$this->template->content->pages[$menu->title] = $sub->text;
							$this->template->content->messages[] = __('Saved submenu').' '.$sub->text;
						}
						
					}
					//if menu doesn't exist
					else{
						$newMenu = ORM::factory('Menus');
						$newMenu->title = $this->flip($string);
						$newMenu->save();

						$sub = ORM::factory('Menuitem')->
						where('text', '=', $_POST['text'])->
						find();

						if(!$sub->loaded()){
							$sub->text = $_POST['text'];
							$sub->item_url = URL::base(TRUE, TRUE).$_POST['item_url'];
							$sub->menu = $menu->id;
							
							$_POST['image_url'] = $_FILES['file']['name'];
							if($_FILES['file']['name'] != '')
							{
								$filename = $this->_save_file($_FILES['file'], $menu, $sub);
							}
							
							$sub->image_url = $filename;
							$sub->save();

							$this->template->content->pages[$menu->title] = $sub->text;
							$this->template->content->messages[] = __('Saved submenu').' '.$sub->text;
						}
						else{
							$this->template->content->errors[] = $sub->text.' '.__('already exists.');
						}
					}
				}
				else{
					//the submenu exists and is being edited
					
					$sub = ORM::factory('Menuitem', $_POST['pages']);
					$menu = ORM::factory('Menus', $sub->menu);
					
					if($menu->loaded()){
						$_POST['image_url'] = $_FILES['file']['name'];
						if($_FILES['file']['name'] != '')
						{
							$filename = $this->_save_file($_FILES['file'], $menu, $sub);
							if($filename !== false){
								$sub->image_url = $filename;
							}
						}
						$sub->text = $_POST['text'];
						$sub->item_url = URL::base(TRUE, TRUE).$_POST['item_url'];
						$sub->save();
						$this->template->content->messages[] = __('Saved submenu').' '.$sub->text;
					}
				}
				
			}
		}
	}//end action_index
	
		 
	/**
	* used by the Menueidt controller to gather the data on the current menu that was selected in the page
	*/
	public function action_getmenu(){
		$this->auto_render = false;

		$sub = $_POST['sub'];
		$val = $_POST['val'];
		
		//if creating a new menu shouldn't return anything;
		if($val == 0){
			echo '{}';
			exit;
		}

		$menuitem = ORM::factory('Menuitem', $val);
		$menu = ORM::factory('Menus', $menuitem->menu);
		
		//find only the end of the url, after kobomaps
		$pos = strrpos($menuitem->item_url, '/kobomaps/');
		$len = strlen('/kobomaps/');
		
		$string = substr($menuitem->item_url, $pos + $len);

		echo '{';
		echo '"menu" : "'.$this->flip($menu->title).'",';
		echo '"text" : "'.$menuitem->text.'",';
		echo '"image" : "'.$menuitem->image_url.'",';
		echo '"url" : "'.$string.'"';
		echo '}';
	}
	

	/**
	* Used to convert static names of default custompages
	* @param string $slug name to be converted
	*/
	public function flip($slug){
		if($slug == '__HOME__'){
				return __('home');
			} 
		if($slug == '__HELP__'){
				return __('help');<commit_msg>Small edit to substring in menuedi<commit_after><?php defined('SYSPATH') or die('No direct script access.');
/***********************************************************
* Menuedit.php - Controller
* This software is copy righted by Kobo 2013
* Writen by <NAME>, Etherton Technologies <http://ethertontech.com>
* Started on 2013-03-22
* Creating custom menu items
*************************************************************/

class Controller_Menuedit extends Controller_Loggedin {

	
	public function before()
	{
		parent::before();	
		
		$auth = Auth::instance();
		//is the user logged in?
		if($auth->logged_in('admin'))
		{
			$this->session = Session::instance();
			//if auto rendered set this up
			if ($this->auto_render)
			{
				$data = array(
						'text' => '',
						'image_url' => '',
						'item_url' => '',
						'id' => isset($_GET['id']) ? intval($_GET['id']) : '__HOME__',
						'menuString' => '',
				);
				
				$pages = ORM::factory('Custompage')->
				where('user_id', '=', $this->user->id)->
				find_all();

				$submenus = array();
				$menus = array();

				$menu = ORM::factory('Menus')->find_all();
				foreach($menu as $m){
					$menus[] = $m;
				}

				foreach($menus as $menu){
					$sub = ORM::factory('Menuitem')->
					where('menu', '=', $menu->id)->
					find_all();

					$submenuArray = array();

					foreach($sub as $s){
						$submenuArray[] = $s;
					}

					$submenus[$menu->id] = $submenuArray;
				}
				
				//grab all of the pages that are in the database from the start, such as main/about/help/support
				$default = ORM::factory('Custompage')->
				where('user_id', '=', 1)->
				find_all();

				//get the menus for the custompage
				$custompage = ORM::factory('Menus')->
				where('title', '=', 'custompage')->
				find_all();
				
				//default pages being parsed into selection field
				$page_array = array();
				foreach($default as $main){
					$page_array[$main->slug][0] = __('New Submenu in').' '.$main->slug;
						$main->slug = $this->flip($main->slug);

						$menu = ORM::factory('Menus')->
						where('title', '=', $main->slug)->
						find();

						$sub = ORM::factory('Menuitem')->
						where('menu', '=', $menu->id)->
						find_all();

						$main->slug = $this->flip($main->slug);
						
						foreach($sub as $s){
							$page_array[$main->slug][$s->id] = $s->text;
						}
				}
				//custom created pages being parsed into selection field
				foreach($pages as $page){
					$page_array[$page->slug][0] = __('New Submenu in').' '.$page->slug;
					$menu = ORM::factory('Menus')->
						where('title', '=', $page->slug)->
						find();

					$sub = ORM::factory('Menuitem')->
						where('menu', '=', $menu->id)->
						find_all();
					foreach($sub as $s){
							$page_array[$page->slug][$s->id] = $s->text;
					}
				}
				
				
				$this->template->header->menu_page = "custompage";
				//make messages roll up when done
				$this->template->html_head->messages_roll_up = true;
				$this->template->html_head->script_views[] = view::factory('js/messages');
				$this->template->content = new View('menuedit/main');
				$this->template->html_head->title = __("Menus Page");
				$this->template->html_head->script_views[] = new View('menuedit/main_js');
				$this->template->content->errors = array();
				$this->template->content->messages = array();
				$this->template->content->data = $data;
				$this->template->content->pages = $page_array;
				$this->template->content->menus = $menus;
				$this->template->content->submenus = $submenus;
			}
		}
	}
	/**
	* where users go to edit custom menus
	*/
	public function action_index()
	{
		$auth = Auth::instance();
		//only admins should be allowed to see the page in the first place, and if not, are redirected to mymaps
		if(!$auth->logged_in('admin'))
		{
			HTTP::redirect('mymaps');
		}

		if(!empty($_POST)){
		
			if($_POST['action'] == 'delete'){
				$sub = ORM::factory('Menuitem')->
				where('text', '=', $_POST['text'])->
				find();
				$response = Model_Menuitem::delete_menuitem($sub->id);
			
				$this->template->content->messages[] = __('Deleted the menu item').' '.$_POST['text'];
				unset($this->template->content->pages[$_POST['pages']]);
			}
			else{
			//if they are creating a new menu or menuitem
				if($_POST['text'] == '' || $_POST['item_url'] == ''){
					$this->template->content->errors[] = __('Title and link url cannot be empty.');
					$data['text'] = $_POST['text'];
					$data['item_url'] = $_POST['item_url'];
					$data['image_url'] = $_POST['image_url'];
					$data['id'] = $_POST['pages'];
					$data['menuString'] = '';
					$this->template->content->data = $data;
				}
				
				//if submenu doesn't exist
				if($_POST['pages'] == 0){
					$length = strlen(__('New Submenu in')) + 1;
					//string will be the menu in which to place this
					$string = substr($_POST['menuString'], $length);

					$menu = ORM::factory('Menus')->
					where('title', '=', $this->flip($string))->
					find();

					if($menu->loaded()){
						$sub = ORM::factory('Menuitem')->
						where('text', '=', $_POST['text'])->
						find();

						if(!$sub->loaded()){
							//load the image
							$sub->text = $_POST['text'];
							$sub->item_url = URL::base(TRUE, TRUE).$_POST['item_url'];
							$sub->menu = $menu->id;
							$sub->save();
							
							$_POST['image_url'] = $_FILES['file']['name'];
							if($_FILES['file']['name'] != '')
							{
								$filename = $this->_save_file($_FILES['file'], $menu, $sub);
							}
							

							if($filename !== false){
								$sub->image_url = $filename;
							}
							$sub->save();

							$this->template->content->pages[$menu->title] = $sub->text;
							$this->template->content->messages[] = __('Saved submenu').' '.$sub->text;
						}
						
					}
					//if menu doesn't exist
					else{
						$newMenu = ORM::factory('Menus');
						$newMenu->title = $this->flip($string);
						$newMenu->save();

						$sub = ORM::factory('Menuitem')->
						where('text', '=', $_POST['text'])->
						find();

						if(!$sub->loaded()){
							$sub->text = $_POST['text'];
							$sub->item_url = URL::base(TRUE, TRUE).$_POST['item_url'];
							$sub->menu = $menu->id;
							
							$_POST['image_url'] = $_FILES['file']['name'];
							if($_FILES['file']['name'] != '')
							{
								$filename = $this->_save_file($_FILES['file'], $menu, $sub);
							}
							
							$sub->image_url = $filename;
							$sub->save();

							$this->template->content->pages[$menu->title] = $sub->text;
							$this->template->content->messages[] = __('Saved submenu').' '.$sub->text;
						}
						else{
							$this->template->content->errors[] = $sub->text.' '.__('already exists.');
						}
					}
				}
				else{
					//the submenu exists and is being edited
					
					$sub = ORM::factory('Menuitem', $_POST['pages']);
					$menu = ORM::factory('Menus', $sub->menu);
					
					if($menu->loaded()){
						$_POST['image_url'] = $_FILES['file']['name'];
						if($_FILES['file']['name'] != '')
						{
							$filename = $this->_save_file($_FILES['file'], $menu, $sub);
							if($filename !== false){
								$sub->image_url = $filename;
							}
						}
						$sub->text = $_POST['text'];
						$sub->item_url = URL::base(TRUE, TRUE).$_POST['item_url'];
						$sub->save();
						$this->template->content->messages[] = __('Saved submenu').' '.$sub->text;
					}
				}
				
			}
		}
	}//end action_index
	
		 
	/**
	* used by the Menueidt controller to gather the data on the current menu that was selected in the page
	*/
	public function action_getmenu(){
		$this->auto_render = false;

		$sub = $_POST['sub'];
		$val = $_POST['val'];
		
		//if creating a new menu shouldn't return anything;
		if($val == 0){
			echo '{}';
			exit;
		}

		$menuitem = ORM::factory('Menuitem', $val);
		$menu = ORM::factory('Menus', $menuitem->menu);
		
		//find only the end of the url, after kobomaps
		$pos = strpos($menuitem->item_url, '/kobomaps/');
		$len = strlen('/kobomaps/');
		
		$string = substr($menuitem->item_url, $pos + $len);

		echo '{';
		echo '"menu" : "'.$this->flip($menu->title).'",';
		echo '"text" : "'.$menuitem->text.'",';
		echo '"image" : "'.$menuitem->image_url.'",';
		echo '"url" : "'.$string.'"';
		echo '}';
	}
	

	/**
	* Used to convert static names of default custompages
	* @param string $slug name to be converted
	*/
	public function flip($slug){
		if($slug == '__HOME__'){
				return __('home');
			} 
		if($slug == '__HELP__'){
				return __('help');
{'fraction_non_alphanumeric': 0.23108698217868523, 'fraction_numerical': 0.0021243951374955742, 'mean_word_length': 2.844600725952813, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 345, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3620178', 'n_tokens_mistral': 7454, 'n_tokens_neox': 5615, 'n_words': 1134}