PageRenderTime 25ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/mylocations.php

https://github.com/anushshetty/migrantwatch
PHP | 432 lines | 388 code | 14 blank | 30 comment | 37 complexity | ad642ad79a870fbb85fb7b47d631d8be MD5 | raw file
  1. <?php
  2. /**
  3. * The my locations script
  4. *
  5. * This page lists the all locations :
  6. *
  7. * 1. Added by this user
  8. * 2. Marked as 'my location' from all the locations available
  9. * 3. All locations for which the user has added entries till date.
  10. *
  11. * Additionally, allows the user to add new locations to 'My Locations'
  12. *
  13. * @author Jatin Chimote <jatin@sanisoft.com>
  14. * @version 1.0
  15. * @package migwatch
  16. */
  17. session_start();
  18. // unset the sessions for other files used for sorting
  19. unset($_SESSION['locsightings']);
  20. include('db.php');
  21. include('functions.php');
  22. include('header.php');
  23. // Some style for this page and js code
  24. echo '
  25. <style>
  26. .error {
  27. font-weight : bold;
  28. color: red;
  29. }
  30. .success {
  31. font-weight : bold;
  32. color: green
  33. }
  34. </style>
  35. <script language="JavaScript" type="text/javascript" src="jquery.js"></script>
  36. <script language="javascript">
  37. function addloc() {
  38. $("#addloc").toggle();
  39. $("#locDetails").hide();
  40. $("#addCurrentLocation").val("");
  41. $("#footer").toggle();
  42. }
  43. function useext() {
  44. $("#addloc").hide();
  45. $("#existloc").toggle();
  46. }
  47. // BOI, followed by one or more whitespace characters, followed by EOI.
  48. var reWhitespace = /^\s+$/
  49. var decimals = /\d.+\d/
  50. // Returns true if string s is empty or
  51. // whitespace characters only.
  52. function isEmpty(s)
  53. { return ((s == null) || (s.length == 0))
  54. }
  55. function isWhitespace (s)
  56. { // Is s empty?
  57. return (isEmpty(s) || reWhitespace.test(s));
  58. }
  59. function addLocation(){
  60. if(isWhitespace(document.frm_level1.nlname.value)){
  61. alert("Please enter location name.");
  62. return;
  63. }
  64. if (document.frm_level1.nlstate.value == -1){
  65. alert("Please select a state for new location");
  66. return;
  67. }
  68. if (document.frm_level1.nllat.value !="" && (!decimals.test(document.frm_level1.nllat.value) || isNaN(document.frm_level1.nllat.value))){
  69. alert("Please enter latitude and longitude in decimals");
  70. return false;
  71. }
  72. if (document.frm_level1.nllong.value !="" && (!decimals.test(document.frm_level1.nllong.value) || isNaN(document.frm_level1.nllong.value))) {
  73. alert("Please enter latitude and longitude in decimals");
  74. return false;
  75. }
  76. document.frm_level1.cmd.value = "newlocation";
  77. document.frm_level1.action="process_level1.php";
  78. document.frm_level1.submit();
  79. }
  80. function stateChanged() {
  81. document.frm.cmd.value = "statechanged";
  82. document.frm.action = "mylocations.php#a";
  83. document.frm.submit();
  84. }
  85. function locSelected(){
  86. document.frm.cmd.value = "locselected";
  87. document.frm.action = "mylocations.php#a";
  88. document.frm.submit();
  89. }
  90. function addToMyLocations() {
  91. /*if (document.frm.state.value == "-1") {
  92. alert("Please select the name of the state from the drop-down");
  93. return false;
  94. }*/
  95. if (document.frm.addCurrentLocation.value == "") {
  96. alert("Please enter a location");
  97. document.frm.addCurrentLocation.focus();
  98. return false;
  99. }
  100. document.frm.cmd.value = "newmylocation";
  101. document.frm.action = "process_level1.php";
  102. document.frm.submit();
  103. }
  104. </script>
  105. ';
  106. if ($_GET['cmd'] == 'insertsuccess') {
  107. echo '<p class="success">Your sighting(s) have been entered successfully. Thank you.</p>';
  108. }
  109. /**
  110. * Success message if the record was entered successfully, and error message
  111. * if something went wrong, if we get a duplicate in the mysql_error. It means
  112. * the user tries to add a location which is already added to the 'My Locations'
  113. * Show an error about the same
  114. */
  115. if ($_GET['extlocadded'] == 1) {
  116. echo '<p class="success">The location has been added to the \'My Locations\' list</p>';
  117. unset($_POST);
  118. unset($_GET);
  119. } else if ($_GET['error'] == 1) {
  120. if ($_GET['duplicate'] == 1) {
  121. $error = 'This location already exists in your \'My Locations\'.';
  122. } else {
  123. $error = 'There was a problem entering the location. Please check if you have filled in all details correctly.';
  124. }
  125. echo "<p class='error'>$error</p>";
  126. }
  127. // The User Id
  128. $user_id = $_SESSION['userid'];
  129. // Set the sorting field
  130. if(!isset($_SESSION['mylocations']['sort']) || $_SESSION['mylocations']['sort'] == '') {
  131. $_SESSION['mylocations']['sort'] = 'state';
  132. $_SESSION['mylocations']['sort_order'] = 'ASC';
  133. } elseif(isset($_POST['fieldSort'])) {
  134. $_SESSION['mylocations']['sort'] = $_POST['fieldSort'];
  135. $_SESSION['mylocations']['sort_order'] = $_POST['fieldOrder'];
  136. }
  137. $sortBy = $_SESSION['mylocations']['sort'];
  138. $sortOrder = $_SESSION['mylocations']['sort_order'];
  139. //print($sortBy."<br/>".$sortOrder);
  140. // Get this user's locations.
  141. $locations = getAllMyLocationData($user_id,$connect,$sortBy,$sortOrder);
  142. // get $myloctionIds here somewhere
  143. $message = '';
  144. if($_POST['remove_loc'] != '') {
  145. $sql = "DELETE FROM migwatch_user_locs WHERE location_id='".$_POST['remove_loc']."' AND user_id='".$user_id."'";
  146. mysql_query($sql);
  147. $message = "The location $locs[location_name] has been removed from 'My Locations'";
  148. // the locations array may have changed after the deletion. Get it again.
  149. $locations = getAllMyLocationData($user_id,$connect,$sortBy,$sortOrder);
  150. }
  151. ?>
  152. <script src="jquery.js" language="javascript"></script>
  153. <script src="jquery.autocomplete.js" language="javascript"></script>
  154. <script src="jquery.bgiframe.min.js" language="javascript"></script>
  155. <script src="jquery.autocomplete.js" language="javascript"></script>
  156. <link href="jquery.autocomplete.css" rel="stylesheet" type="text/css" />
  157. <script language="javascript">
  158. function setSorting(field, order) {
  159. document.frm_sortfield.fieldSort.value = field;
  160. if(order != '') {
  161. document.frm_sortfield.fieldOrder.value = order;
  162. } else {
  163. var sortBy = '<?php print($sortBy);?>';
  164. var sortOrder = '<?php print($sortOrder);?>';
  165. if(field == sortBy) {
  166. document.frm_sortfield.fieldOrder.value = (sortOrder=='ASC') ? 'DESC': 'ASC';
  167. } else {
  168. document.frm_sortfield.fieldOrder.value = 'ASC';
  169. }
  170. }
  171. document.frm_sortfield.submit();
  172. }
  173. function removeLocation(del_id, creator) {
  174. userId = '<?php print($user_id); ?>';
  175. var confirmed = confirm("You are about to remove the record from My Locations. Continue?");
  176. if(confirmed) {
  177. document.frm_sortfield.remove_loc.value = del_id;
  178. document.frm_sortfield.submit();
  179. }
  180. }
  181. $().ready(function() {
  182. $('#addCurrentLocation').autocomplete("auto_miglocations.php", {
  183. width: 400,
  184. selectFirst: false,
  185. matchSubset :0,
  186. formatItem:formatLocation
  187. });
  188. $("#addCurrentLocation").result(function(event, data, formatted) {
  189. $('#location').val(data[1]);
  190. $('#locDetails').show();
  191. $('#addloc').hide();
  192. $('#details_loc').html(data[2]);
  193. $('#details_city').html(data[3]);
  194. $('#details_dist').html(data[4]);
  195. $('#details_state').html(data[5]);
  196. $('#details_lat').html(data[6]);
  197. $('#details_long').html(data[7]);
  198. });
  199. });
  200. function formatLocation(row) {
  201. var completeRow;
  202. completeRow = new String(row);
  203. splittedStr = completeRow.split(",");
  204. var newrow = '';
  205. for(var i=0; i< splittedStr.length; i++) {
  206. if(isNaN(splittedStr[i])) {
  207. newrow += splittedStr[i] +",";
  208. } else if(splittedStr[i] > 0 ){
  209. break;
  210. }
  211. }
  212. newrow = newrow.substr(0, newrow.lastIndexOf(","));
  213. return newrow;
  214. }
  215. </script>
  216. <table cellspacing="0" cellpadding="4" width="770">
  217. <tbody>
  218. <tr bgcolor="#dedede">
  219. <td align='left'>
  220. <b>My Locations</b>
  221. </td>
  222. <td align="right"><a href='main.php'><img style="border-width: 0px;" src="images/back.gif"/></a></td>
  223. </tr>
  224. </tbody>
  225. </table>
  226. <?php
  227. if (!empty($locations)) {
  228. if($message != '') {
  229. print("<p class='error'>$message</p>");
  230. }
  231. ?>
  232. <p>
  233. The following locations have been marked as 'My Locations': <br/><br/>
  234. </p>
  235. <form name="frm_sortfield" method="POST" action="mylocations.php">
  236. <table cellspacing="0" cellpadding="3" width="770" style="border: 1px solid rgb(222, 222, 222);">
  237. <tbody>
  238. <tr style="font-weight: bold; font-size: x-small; background-color: rgb(239, 239, 239);">
  239. <td>SNo.</td>
  240. <td><a href="javascript:setSorting('location_name', '')">Location</a> &nbsp;<img src="images/s_asc.png" title="sort ascending" onclick="setSorting('location_name', 'ASC')">&nbsp;<img src="images/s_desc.png" title="sort descending" onclick="setSorting('location_name', 'DESC')"></td>
  241. <td><a href="javascript:setSorting('city', '')">Town</a> &nbsp;<img src="images/s_asc.png" title="sort ascending" onclick="setSorting('city', 'ASC')">&nbsp;<img src="images/s_desc.png" title="sort descending" onclick="setSorting('city', 'DESC')"></td>
  242. <!--<td><a href="javascript:setSorting('district', '')">District</a> &nbsp;<img src="images/s_asc.png" title="sort ascending" onclick="setSorting('district', 'ASC')">&nbsp;<img src="images/s_desc.png" title="sort descending" onclick="setSorting('district', 'DESC')"></td>-->
  243. <td><a href="javascript:setSorting('state', '')">State / UT</a> &nbsp;<img src="images/s_asc.png" title="sort ascending" onclick="setSorting('state', 'ASC')">&nbsp;<img src="images/s_desc.png" title="sort descending" onclick="setSorting('state', 'DESC')"></td>
  244. <td colspan="2">Report Sightings</td>
  245. <td>Details</td>
  246. </tr>
  247. <?php
  248. $i = 1;
  249. foreach ($locations as $location) {
  250. $delete_this = "&nbsp;&nbsp;<img src='images/delete.gif' title='Remove location' onclick='removeLocation($location[location_id],$location[created_by_user_id])'>";
  251. print "<tr bgcolor=".($i % 2 == 0?"#efefef":"#ffffff").">";
  252. echo "<td>$i</td>";
  253. echo "<td>$location[location_name]</td>";
  254. echo empty($location['city']) ? '<td>--</td>' : "<td>$location[city]</td>";
  255. //echo empty($location['district']) ? '<td>--</td>' : "<td>$location[district]</td>";
  256. echo "<td>$location[state]</td>";
  257. echo "<td><a href='trackspecies.php?loc_id=$location[location_id]' title='Report first sighting at $location[location_name]'>first</a></td>";
  258. echo "<td><a href='trackspecies.php?last=1&loc_id=$location[location_id]' title='Report last sighting at $location[location_name]'>last</a></td> ";
  259. echo "<td><a href='locsightings.php?id=$location[location_id]'><img src='images/view.gif' style='border-width:0px;' title=\"View all sightings at $location[location_name]\"></a> $delete_this</td>";
  260. "</tr>";
  261. $i++;
  262. }
  263. ?>
  264. </table>
  265. <input type="hidden" name="fieldSort" value="<?php print($sortBy); ?>">
  266. <input type="hidden" name="fieldOrder" value="<?php print($sortOrder); ?>">
  267. <input type="hidden" name="remove_loc" value="">
  268. </form>
  269. <?php
  270. } else {
  271. echo "<br/><p class='error'>There are currently no locations marked as your locations. Please use the links below to add.</p>";
  272. }
  273. ?>
  274. <br />
  275. <table cellspacing="0" cellpadding="4" width="770">
  276. <tbody>
  277. <tr bgcolor="#dedede">
  278. <td>
  279. <b>Add to My Locations</b>
  280. </td>
  281. </tr>
  282. </tbody>
  283. </table>
  284. <p style="width:770px;">
  285. Type <strong>part of a location name</strong> in the text box, <strong>choose from the matches</strong>
  286. and click on <strong>"Add to My Locations"</strong>. Please try common variants on the
  287. location name and spelling if at first you do not find matches. <strong>If
  288. your location does not exist</strong> in our database, please enter the details
  289. of the location below and click on <strong>"Add new location"</strong>. <br /><br>
  290. - <strong>Add a location to My Locations</strong>
  291. <form method="POST" name="frm">
  292. <input type="text" name="addCurrentLocation" id="addCurrentLocation" style="width:400px;" />&nbsp;<input type="reset" value="Clear" onclick="$('#locDetails').hide()"><br />
  293. <input type="hidden" name="cmd" value="" />
  294. <input type="hidden" name="location" id="location" value="" />
  295. <table id="locDetails" width="770px" style="border:1px solid rgb(222, 222, 222);display:none;">
  296. <tr width="100%">
  297. <td colspan="2" bgcolor="#efefef"><strong>Location Details</strong></td>
  298. </tr>
  299. <tr>
  300. <td width="25%">Location Name</td>
  301. <td><div id="details_loc"><div></td>
  302. </tr>
  303. <tr bgcolor="#efefef">
  304. <td>City</td>
  305. <td><div id="details_city"></div></td>
  306. </tr>
  307. <tr>
  308. <td>District</td>
  309. <td><div id="details_dist"></div></td>
  310. </tr>
  311. <tr bgcolor="#efefef">
  312. <td>State / UT</td>
  313. <td><div id="details_state"></div></td>
  314. </tr>
  315. <td>Latitude</td>
  316. <td><div id="details_lat"></div></td>
  317. <tr>
  318. </tr>
  319. <tr bgcolor="#efefef">
  320. <td>Longitude</td>
  321. <td><div id="details_long"></div></td>
  322. </tr>
  323. </table><br />
  324. <input type="button" name="sub" value="Add to 'My Locations'" onClick='addToMyLocations();'/>
  325. </form>
  326. </p>
  327. - <a style="background-color:#dedede;" href="#a" onClick="javascript:addloc();"><strong>Add new Location</strong></a><br />
  328. <!-- <a style="background-color:#dedede;" href="#a" onClick="javascript:useext();"><b>Choose From Existing Locations</a></b><br />-->
  329. <form method="post" name='frm_level1'>
  330. <table width="770" style="border:1px solid rgb(222, 222, 222);display:none;" align='left' id='addloc'>
  331. <tr>
  332. <td width=20%>Location Name:</td>
  333. <td><input type=text name=nlname style="width:250px;"/>
  334. (eg. Okhla Bird Sanctuary)
  335. </td>
  336. </tr>
  337. <tr>
  338. <td>City/Town:</td>
  339. <td><input type=text name=nlcity style="width:250px;"/></td>
  340. </tr>
  341. <tr>
  342. <td>District:</td>
  343. <td><input type=text name=nldist style="width:250px;"/></td>
  344. </tr>
  345. <tr>
  346. <td>State:</td>
  347. <td>
  348. <SELECT name=nlstate style="width:250">
  349. <option value=-1>--Select State--</option>
  350. <?php
  351. $result = mysql_query("SELECT state_id, state FROM migwatch_states order by state");
  352. if($result){
  353. while ($row = mysql_fetch_array($result, MYSQL_ASSOC)){
  354. if($row['state'] != 'Not In India') {
  355. print "<option value=".$row{'state_id'};
  356. print ">".$row{'state'}."</option>\n";
  357. } else {
  358. $other_id = $row['state_id'];
  359. $other = $row['state'];
  360. }
  361. }
  362. print("<option value=".$other_id.">".$other."</option>\n");
  363. }
  364. ?>
  365. </select>
  366. </td>
  367. </tr>
  368. <tr>
  369. <td>Latitude (eg, 26.98):</td>
  370. <td><input type=text name=nllat style="width:250px;"/>&nbsp;(If known)</td>
  371. </tr>
  372. <tr>
  373. <td>Longitude (eg, 74.70):</td>
  374. <td><input type=text name=nllong style="width:250px;"/>&nbsp;(If known)
  375. </td>
  376. </tr>
  377. <tr>
  378. <td colspan=2>
  379. If you don't know the geographical coordinates,
  380. please describe the location in detail (eg, distance and direction
  381. from the nearest town)
  382. </td>
  383. </tr>
  384. <tr>
  385. <td colspan=2>
  386. <textarea name=nladditional rows=5 cols=40></textarea>
  387. </td>
  388. </tr>
  389. <tr>
  390. <td colspan=2>
  391. <input type=hidden name=cmd value="newlocation"/>
  392. <input type=hidden name=currentfile value="mylocations.php"/>
  393. &nbsp;<input type=button value="Add" class=buttonstyle onclick="addLocation();" />
  394. &nbsp;<input type=button value="Cancel" class=buttonstyle onclick="javascript:addloc(0);"/>
  395. </td>
  396. </tr>
  397. </table>
  398. </form><br /><br />
  399. <a name='a'></a>
  400. <br />
  401. <div id="footer">
  402. <?php
  403. include('footer.php');
  404. ?>
  405. </div>
  406. </body>
  407. </html>