<commit_before><?php defined('SYSPATH') or die('No direct script access.');
/***********************************************************
* Mymaps.php - Controller
* This software is copy righted by Kobo 2012
* Writen by <NAME> <<EMAIL>>, Etherton Technologies <http://ethertontech.com>
* Started on 2012-11-08
*************************************************************/

class Controller_Mymaps extends Controller_Loggedin {



	/**
	where users go to change their profiel
	*/
	public function action_index()
	{
		/***** initialize stuff****/
		//The title to show on the browser
		$this->template->html_head->title = __("My Maps");
		//make messages roll up when done
		$this->template->html_head->messages_roll_up = true;
		//the name in the menu
		$this->template->header->menu_page = "mymaps";
		$this->template->content = view::factory("mymaps");
		$this->template->content->errors = array();
		$this->template->content->messages = array();
		//set the JS
		$js = view::factory('mymaps_js');
		$this->template->html_head->script_views[] = $js;
		$this->template->html_head->script_views[] = view::factory('js/messages');
		
		
		/********Check if we're supposed to do something ******/
		if(!empty($_POST)) // They've submitted the form to update his/her wish
		{
			try
			{	
				if($_POST['action'] == 'delete')
				{
					Model_Map::delete_map($_POST['map_id']);
					$this->template->content->messages[] = __('Map Deleted');
				}
			}
			catch (ORM_Validation_Exception $e)
			{
				$errors_temp = $e->errors('register');
				if(isset($errors_temp["_external"]))
				{
					$this->template->content->errors = array_merge($errors_temp["_external"], $this->template->content->errors);
				}				
				else
				{
					foreach($errors_temp as $error)
					{
						if(is_string($error))
						{
							$this->template->content->errors[] = $error;							
						}
					}
				}
			}	
		}
		
		/*****Render the forms****/
		
		//get the forms that belong to this user
		$maps = ORM::factory("Map")
			->where('user_id', '=', $this->user->id)
			->order_by('title', 'ASC')
			->find_all();
		
		$this->template->content->maps = $maps;
		
		
		
	}//end action_index
	
	
	
	/**
	 * the function for editing a form
	 * super exciting
	 */
	 public function action_add1()
	 {
		//initialize data
		$data = array(
			'id'=>'0',
			'title'=>'',
			'description'=>'',
			'file'=>'',
			'CSS'=>'',
			'lat'=>'0',
			'lon'=>'0',
			'zoom'=>'1',
			'map_style'=>Model_Map::$style_default,
			'user_id'=>$this->user->id,
			'is_private'=>0,
			'private_password'=><PASSWORD>,
			'map_creation_progress'=>1
			);
		
		$map = null;
		
		//was an id given?		
		$map_id = isset($_GET['id']) ? intval($_GET['id']) : 0;
		//something when wrong, kick them back to add1
		if($map_id != 0)
		{
			$map = ORM::factory('Map', $map_id);
			$data['id'] = $map_id;
			$data['title'] = $map->title;
			$data['description'] = $map->description;
			$data['CSS'] = $map->CSS;
			$data['lat'] = $map->lat;
			$data['lon'] = $map->lon;
			$data['zoom'] = $map->zoom;
			$data['map_style'] = $map->map_style;
			$data['user_id'] = $map->user_id;
			$data['is_private'] = $map->is_private;
			$data['private_password'] = $map->private_password;
		}
		
				
		
		 
		
		/***Now that we have the form, lets initialize the UI***/
		//The title to show on the browser
		$header =  __("Add Map - Page 1") ;
		$this->template->html_head->title = $header;		
		//make messages roll up when done
		$this->template->html_head->messages_roll_up = true;
		//the name in the menu
		$this->template->header->menu_page = "mymaps";
		$this->template->content = view::factory("addmap/add1");
		$this->template->content->data = $data;
		$this->template->content->errors = array();
		$this->template->content->messages = array();
		$this->template->content->header = $header;
		//set the JS
		
		//$js = view::factory('add1_js/form_edit_js');
		$js = view::factory('addmap/add1_js');
		//$js->is_add = $is_add;
		$this->template->html_head->script_views[] = $js;		
		$this->template->html_head->script_views[] = view::factory('js/messages');
		$this->template->html_head->script_views[] = view::factory('js/gspreadsheetselect');
		
		//get the status
		$status = isset($_GET['status']) ? $_GET['status'] : null;
		if($status == 'saved')
		{
				$this->template->content->messages[] = __('changes saved');
		}
		
		/******* Handle incoming data*****/
		if(!empty($_POST)) // They've submitted the form to update his/her wish
		{
			try
			{
				
				//save to the DB
				
				if($map == null)
				{				
					$map = ORM::factory('Map');
					//check if they're using Excel files or Google Docs
					if($_POST['filetype'] == 'excel')
					{
						$_POST['file'] = $_FILES['file']['name'];
					}
					else
					{
						//make sure they did select a google doc
						if(!isset($_POST['googleid']) OR $_POST['googleid'] == '')
						{
							$this->template->content->errors[] = __('You must specify an Excel file or a Google Doc to use as the data source.'); 
							$data = array_merge($data,$_POST);
							$this->template->content->data = $data;
							return;
						}
						$_POST['file'] = $_POST['googleid'];
					}
					$_POST['template_id'] = 0;
					$_POST['json_file'] = '0';
				}
				else
				{
					if($_FILES['file']['name'] == '' AND $_POST['googleid'] == '')
					{
						$_POST['file'] = $map->file; 
					}
					$_POST['template_id'] = $map->template_id;
					$_POST['json_file'] = $map->json_file;
				}
				//this handles is private
				$_POST['is_private'] = isset($_POST['is_private']) ? 1 : 0;
				$_POST['map_creation_progress'] = 1;
				$map->update_map($_POST);
				
				
				//handle the xls file, or Google Doc, if there's something to save
				if(($_FILES['file']['name'] != '' AND $_POST['filetype'] == 'excel')  OR ($_POST['googleid'] != '' AND $_POST['filetype'] == 'google'))
				{					
					//now if we need to save a file
					if($_FILES['file']['name'] != '' AND $_POST['filetype'] == 'excel')
					{
						$filename = $this->_save_file($_FILES['file'], $map);
					}
					//else we need to save a google doc
					else 
					{
						$filename = $this->_save_google_doc($_POST['googlelink'], $map);
					}
					$map->file = $filename;
					$map->save();
				
					//blow away all existing map sheets for this map if there are any
					$map_sheets = ORM::factory('Mapsheet')
						->where('map_id', '=', $map->id)
						->find_all();
					foreach($map_sheets as $map_sheet)
					{
						$map_sheet->delete();
					}
				
					//now we need to figure out what sheets there are
					//read the xls file and parse if
					$file_path = DOCROOT.'uploads/data/'. $map->file;
					 
					//get the PHPExcel classes on stand by:
					require_once Kohana::find_file('PHPExcel', 'Classes/PHPExcel');
					 
					$excel = PHPExcel_IOFactory::load($file_path);
					$sheet_names = $excel->getSheetNames();
					$i = 0;
					foreach($sheet_names as $sheet_name)
					{
						$sheet = $excel->getSheetByName($sheet_name);
						$map_sheet = ORM::factory('Mapsheet');
						$map_sheet->position = $i;
						$map_sheet->name = $sheet_name;
						$map_sheet->map_id = $map->id;
						$map_sheet->save(); 
						$i++;
					
					}
				}
				
				
				HTTP::redirect('mymaps/add2?id='.$map->id);	
							
				
			}
			catch (ORM_Validation_Exception $e)
			{
				$errors_temp = $e->errors('register');
				if(isset($errors_temp["_external"]))
				{
					$this->template->content->errors = array_merge($errors_temp["_external"], $this->template->content->errors);
				}				
				else
				{
					foreach($errors_temp as $error)
					{
						if(is_string($error))
						{
							$this->template->content->errors[] = $error;							
						}
					}
				}
				$data = array_merge($data,$_POST);
				$this->template->content->data = $data;
			}
		}
	 }//end action_add1
	 
	 
	 
	
	 
	 
	 /**
	  * the function for editing a form
	  * super exciting
	  */
	 public function action_add2()
	 {
	 	//get the id
	 	$map_id = isset($_GET['id']) ? intval($_GET['id']) : 0;
	 	//something when wrong, kick them back to add1
	 	if($map_id == 0)
	 	{
	 		HTTP::redirect('mymaps/add1');
	 	}
	 	
	 	//pull the map object from the DB
	 	$map = ORM::factory('Map', $map_id);
	 	
	 	if($map->map_creation_progress < 1)
	 	{
	 		$this->template->content->messages[] = __('Map stage missing. Complete this page first.');
	 		HTTP::redirect('mymaps/add1/?id='.$map_id);
	 	}
	 	
	 	//grab all the sheet objects too
	 	$sheets = ORM::factory('Mapsheet')
	 		->where('map_id', '=', $map->id)
	 		->order_by('position', 'ASC')
	 		->find_all();
	 	
	 	//create a look-up table for use later
	 	$sheet_name_to_db_id = array();
	 	foreach($sheets as $s)
	 	{
	 		$sheet_name_to_db_id[$s->name] = $s->id;
	 	}

	 	//initialize the data array for this form
	 	$data = array('row'=>array(), 'column'=>array());
	 	
	 	//now we need to figure out what is on the sheets
	 	//read the xls file and parse if
	 	$file_path = DOCROOT.'uploads/data/'. $map->file;
	 		
	 	//get the PHPExcel classes on stand by:
	 	require_once Kohana::find_file('PHPExcel', 'Classes/PHPExcel');
	 		
	 	$excel = PHPExcel_IOFactory::load($file_path);
	 	$sheet_names = $excel->getSheetNames();
	 	$sheet_data = array();
	 	//now all of this is to init the $data array for the form.
	 	foreach($sheet_names as $sheet_name)
	 	{
	 		$sheet = $excel->getSheetByName($sheet_name);
	 		$sheet_data[$sheet_name] = $sheet->toArray(null, true, true, true);
	 		//now use that look-up table to get the DB id of this sheet
	 		$sheet_db_id = $sheet_name_to_db_id[$sheet_name];
	 		$data['row'][$sheet_db_id] = array();
	 		$data['column'][$sheet_db_id] = array();
	 		foreach($sheet_data[$sheet_name] as $row_index=>$row_data)
	 		{<commit_msg>corrected a typo on a comment<commit_after><?php defined('SYSPATH') or die('No direct script access.');
/***********************************************************
* Mymaps.php - Controller
* This software is copy righted by Kobo 2012
* Writen by <NAME> <<EMAIL>>, Etherton Technologies <http://ethertontech.com>
* Started on 2012-11-08
*************************************************************/

class Controller_Mymaps extends Controller_Loggedin {



	/**
	where users go to change their profiel
	*/
	public function action_index()
	{
		/***** initialize stuff****/
		//The title to show on the browser
		$this->template->html_head->title = __("My Maps");
		//make messages roll up when done
		$this->template->html_head->messages_roll_up = true;
		//the name in the menu
		$this->template->header->menu_page = "mymaps";
		$this->template->content = view::factory("mymaps");
		$this->template->content->errors = array();
		$this->template->content->messages = array();
		//set the JS
		$js = view::factory('mymaps_js');
		$this->template->html_head->script_views[] = $js;
		$this->template->html_head->script_views[] = view::factory('js/messages');
		
		
		/********Check if we're supposed to do something ******/
		if(!empty($_POST)) // They've submitted the form to update his/her wish
		{
			try
			{	
				if($_POST['action'] == 'delete')
				{
					Model_Map::delete_map($_POST['map_id']);
					$this->template->content->messages[] = __('Map Deleted');
				}
			}
			catch (ORM_Validation_Exception $e)
			{
				$errors_temp = $e->errors('register');
				if(isset($errors_temp["_external"]))
				{
					$this->template->content->errors = array_merge($errors_temp["_external"], $this->template->content->errors);
				}				
				else
				{
					foreach($errors_temp as $error)
					{
						if(is_string($error))
						{
							$this->template->content->errors[] = $error;							
						}
					}
				}
			}	
		}
		
		/*****Render the forms****/
		
		//get the forms that belong to this user
		$maps = ORM::factory("Map")
			->where('user_id', '=', $this->user->id)
			->order_by('title', 'ASC')
			->find_all();
		
		$this->template->content->maps = $maps;
		
		
		
	}//end action_index
	
	
	
	/**
	 * the function for editing a form
	 * super exciting
	 */
	 public function action_add1()
	 {
		//initialize data
		$data = array(
			'id'=>'0',
			'title'=>'',
			'description'=>'',
			'file'=>'',
			'CSS'=>'',
			'lat'=>'0',
			'lon'=>'0',
			'zoom'=>'1',
			'map_style'=>Model_Map::$style_default,
			'user_id'=>$this->user->id,
			'is_private'=>0,
			'private_password'=><PASSWORD>,
			'map_creation_progress'=>1
			);
		
		$map = null;
		
		//was an id given?		
		$map_id = isset($_GET['id']) ? intval($_GET['id']) : 0;
		//something when wrong, kick them back to add1
		if($map_id != 0)
		{
			$map = ORM::factory('Map', $map_id);
			$data['id'] = $map_id;
			$data['title'] = $map->title;
			$data['description'] = $map->description;
			$data['CSS'] = $map->CSS;
			$data['lat'] = $map->lat;
			$data['lon'] = $map->lon;
			$data['zoom'] = $map->zoom;
			$data['map_style'] = $map->map_style;
			$data['user_id'] = $map->user_id;
			$data['is_private'] = $map->is_private;
			$data['private_password'] = $map->private_password;
		}
		
				
		
		 
		
		/***Now that we have the form, lets initialize the UI***/
		//The title to show on the browser
		$header =  __("Add Map - Page 1") ;
		$this->template->html_head->title = $header;		
		//make messages roll up when done
		$this->template->html_head->messages_roll_up = true;
		//the name in the menu
		$this->template->header->menu_page = "mymaps";
		$this->template->content = view::factory("addmap/add1");
		$this->template->content->data = $data;
		$this->template->content->errors = array();
		$this->template->content->messages = array();
		$this->template->content->header = $header;
		//set the JS
		
		//$js = view::factory('add1_js/form_edit_js');
		$js = view::factory('addmap/add1_js');
		//$js->is_add = $is_add;
		$this->template->html_head->script_views[] = $js;		
		$this->template->html_head->script_views[] = view::factory('js/messages');
		$this->template->html_head->script_views[] = view::factory('js/gspreadsheetselect');
		
		//get the status
		$status = isset($_GET['status']) ? $_GET['status'] : null;
		if($status == 'saved')
		{
				$this->template->content->messages[] = __('changes saved');
		}
		
		/******* Handle incoming data*****/
		if(!empty($_POST)) // They've submitted the form to update his/her wish
		{
			try
			{
				
				//save to the DB
				
				if($map == null)
				{				
					$map = ORM::factory('Map');
					//check if they're using Excel files or Google Docs
					if($_POST['filetype'] == 'excel')
					{
						$_POST['file'] = $_FILES['file']['name'];
					}
					else
					{
						//make sure they did select a google doc
						if(!isset($_POST['googleid']) OR $_POST['googleid'] == '')
						{
							$this->template->content->errors[] = __('You must specify an Excel file or a Google Doc to use as the data source.'); 
							$data = array_merge($data,$_POST);
							$this->template->content->data = $data;
							return;
						}
						$_POST['file'] = $_POST['googleid'];
					}
					$_POST['template_id'] = 0;
					$_POST['json_file'] = '0';
				}
				else
				{
					if($_FILES['file']['name'] == '' AND $_POST['googleid'] == '')
					{
						$_POST['file'] = $map->file; 
					}
					$_POST['template_id'] = $map->template_id;
					$_POST['json_file'] = $map->json_file;
				}
				//this handles is private
				$_POST['is_private'] = isset($_POST['is_private']) ? 1 : 0;
				$_POST['map_creation_progress'] = 1;
				$map->update_map($_POST);
				
				
				//handle the xls file, or Google Doc, if there's something to save
				if(($_FILES['file']['name'] != '' AND $_POST['filetype'] == 'excel')  OR ($_POST['googleid'] != '' AND $_POST['filetype'] == 'google'))
				{					
					//now if we need to save a file
					if($_FILES['file']['name'] != '' AND $_POST['filetype'] == 'excel')
					{
						$filename = $this->_save_file($_FILES['file'], $map);
					}
					//else we need to save a google doc
					else 
					{
						$filename = $this->_save_google_doc($_POST['googlelink'], $map);
					}
					$map->file = $filename;
					$map->save();
				
					//blow away all existing map sheets for this map if there are any
					$map_sheets = ORM::factory('Mapsheet')
						->where('map_id', '=', $map->id)
						->find_all();
					foreach($map_sheets as $map_sheet)
					{
						$map_sheet->delete();
					}
				
					//now we need to figure out what sheets there are
					//read the xls file and parse it
					$file_path = DOCROOT.'uploads/data/'. $map->file;
					 
					//get the PHPExcel classes on stand by:
					require_once Kohana::find_file('PHPExcel', 'Classes/PHPExcel');
					 
					$excel = PHPExcel_IOFactory::load($file_path);
					$sheet_names = $excel->getSheetNames();
					$i = 0;
					foreach($sheet_names as $sheet_name)
					{
						$sheet = $excel->getSheetByName($sheet_name);
						$map_sheet = ORM::factory('Mapsheet');
						$map_sheet->position = $i;
						$map_sheet->name = $sheet_name;
						$map_sheet->map_id = $map->id;
						$map_sheet->save(); 
						$i++;
					
					}
				}
				
				
				HTTP::redirect('mymaps/add2?id='.$map->id);	
							
				
			}
			catch (ORM_Validation_Exception $e)
			{
				$errors_temp = $e->errors('register');
				if(isset($errors_temp["_external"]))
				{
					$this->template->content->errors = array_merge($errors_temp["_external"], $this->template->content->errors);
				}				
				else
				{
					foreach($errors_temp as $error)
					{
						if(is_string($error))
						{
							$this->template->content->errors[] = $error;							
						}
					}
				}
				$data = array_merge($data,$_POST);
				$this->template->content->data = $data;
			}
		}
	 }//end action_add1
	 
	 
	 
	
	 
	 
	 /**
	  * the function for editing a form
	  * super exciting
	  */
	 public function action_add2()
	 {
	 	//get the id
	 	$map_id = isset($_GET['id']) ? intval($_GET['id']) : 0;
	 	//something when wrong, kick them back to add1
	 	if($map_id == 0)
	 	{
	 		HTTP::redirect('mymaps/add1');
	 	}
	 	
	 	//pull the map object from the DB
	 	$map = ORM::factory('Map', $map_id);
	 	
	 	if($map->map_creation_progress < 1)
	 	{
	 		$this->template->content->messages[] = __('Map stage missing. Complete this page first.');
	 		HTTP::redirect('mymaps/add1/?id='.$map_id);
	 	}
	 	
	 	//grab all the sheet objects too
	 	$sheets = ORM::factory('Mapsheet')
	 		->where('map_id', '=', $map->id)
	 		->order_by('position', 'ASC')
	 		->find_all();
	 	
	 	//create a look-up table for use later
	 	$sheet_name_to_db_id = array();
	 	foreach($sheets as $s)
	 	{
	 		$sheet_name_to_db_id[$s->name] = $s->id;
	 	}

	 	//initialize the data array for this form
	 	$data = array('row'=>array(), 'column'=>array());
	 	
	 	//now we need to figure out what is on the sheets
	 	//read the xls file and parse if
	 	$file_path = DOCROOT.'uploads/data/'. $map->file;
	 		
	 	//get the PHPExcel classes on stand by:
	 	require_once Kohana::find_file('PHPExcel', 'Classes/PHPExcel');
	 	$excel = PHPExcel_IOFactory::load($file_path);
	 	$sheet_names = $excel->getSheetNames();
	 	$sheet_data = array();
	 	//now all of this is to init the $data array for the form.
	 	foreach($sheet_names as $sheet_name)
	 	{
	 		$sheet = $excel->getSheetByName($sheet_name);
	 		$sheet_data[$sheet_name] = $sheet->toArray(null, true, true, true);
	 		//now use that look-up table to get the DB id of this sheet
	 		$sheet_db_id = $sheet_name_to_db_id[$sheet_name];
	 		$data['row'][$sheet_db_id] = array();
	 		$data['column'][$sheet_db_id] = array();
	 		foreach($sheet_data[$sheet_name] as $row_index=>$row_data)
	 		{
{'fraction_non_alphanumeric': 0.1997228921845333, 'fraction_numerical': 0.004207933494124288, 'mean_word_length': 2.9553480819971587, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 337, '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': '2126937', 'n_tokens_mistral': 8428, 'n_tokens_neox': 6576, 'n_words': 1413}