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

/application/controllers/admin/manage.php

https://github.com/fayazv/Taarifa_Web
PHP | 1112 lines | 771 code | 161 blank | 180 comment | 102 complexity | a97358acfb0eedbcf294ebe3895b38fe MD5 | raw file
Possible License(s): AGPL-1.0, LGPL-3.0, BSD-3-Clause, LGPL-2.1
  1. <?php defined('SYSPATH') or die('No direct script access.');
  2. /**
  3. * This controller is used to add/ remove categories, forms, pages,
  4. * news feeds, layers and managing the scheduler and public listings
  5. *
  6. * PHP version 5
  7. * LICENSE: This source file is subject to LGPL license
  8. * that is available through the world-wide-web at the following URI:
  9. * http://www.gnu.org/copyleft/lesser.html
  10. * @author Ushahidi Team <team@ushahidi.com>
  11. * @package Ushahidi - http://source.ushahididev.com
  12. * @subpackage Admin
  13. * @copyright Ushahidi - http://www.ushahidi.com
  14. * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License (LGPL)
  15. */
  16. /**
  17. * Feed type value for news
  18. */
  19. define('FEED_TYPE_TEXT', 'text');
  20. /**
  21. * Feed type value for videos
  22. */
  23. define('FEED_TYPE_VIDEO', 'video');
  24. /**
  25. * Feed type value for photos
  26. */
  27. define('FEED_TYPE_PHOTO', 'photo');
  28. class Manage_Controller extends Admin_Controller
  29. {
  30. public function __construct()
  31. {
  32. parent::__construct();
  33. $this->template->this_page = 'manage';
  34. // If user doesn't have access, redirect to dashboard
  35. if ( ! admin::permissions($this->user, "manage"))
  36. {
  37. url::redirect(url::site().'admin/dashboard');
  38. }
  39. }
  40. /**
  41. * Add Edit Categories
  42. */
  43. public function index()
  44. {
  45. $this->template->content = new View('admin/categories');
  46. $this->template->content->title = Kohana::lang('ui_admin.categories');
  47. // Locale (Language) Array
  48. $locales = ush_locale::get_i18n();
  49. // Setup and initialize form field names
  50. $form = array
  51. (
  52. 'action' => '',
  53. 'category_id' => '',
  54. 'parent_id' => '',
  55. 'category_title' => '',
  56. 'category_description' => '',
  57. 'category_color' => '',
  58. 'category_image' => '',
  59. 'category_image_thumb' => ''
  60. );
  61. // Add the different language form keys for fields
  62. foreach ($locales as $lang_key => $lang_name)
  63. {
  64. $form['category_title_'.$lang_key] = '';
  65. }
  66. // Copy the form as errors, so the errors will be stored with keys corresponding to the form field names
  67. $errors = $form;
  68. $form_error = FALSE;
  69. $form_saved = FALSE;
  70. $form_action = "";
  71. $parents_array = array();
  72. // Check, has the form been submitted, if so, setup validation
  73. if ($_POST)
  74. {
  75. // Fetch the post data
  76. $post_data = array_merge($_POST, $_FILES);
  77. // Extract category-specific information
  78. $category_data = arr::extract($post_data, 'parent_id', 'category_title', 'category_description', 'category_color');
  79. // Extract category image and category languages for independent validation
  80. $secondary_data = arr::extract($post_data, 'category_image', 'category_title_lang', 'action');
  81. // Setup validation for the secondary data
  82. $post = Validation::factory($secondary_data)
  83. ->pre_filter('trim', TRUE);
  84. // Add validation for the add/edit action
  85. if ($post->action == 'a')
  86. {
  87. $post->add_rules('category_image', 'upload::valid', 'upload::type[gif,jpg,png]', 'upload::size[50K]');
  88. // Add the different language form keys for fields
  89. foreach ($locales as $lang_key => $lang_name)
  90. {
  91. $post->add_rules('category_title_lang['.$lang_key.']','length[3,80]');
  92. }
  93. }
  94. // Category instance for the operation
  95. $category = (! empty($_POST['category_id']) AND Category_Model::is_valid_category($_POST['category_id']))
  96. ? new Category_Model($_POST['category_id'])
  97. : new Category_Model();
  98. // Check the specified action
  99. if ($post->action == 'a')
  100. {
  101. // Test to see if things passed the rule checks
  102. if ($category->validate($category_data) AND $post->validate())
  103. {
  104. // Save the category
  105. $category->save();
  106. // Get the category localization
  107. $languages = ($category->loaded) ? Category_Lang_Model::category_langs($category->id) : FALSE;
  108. $category_lang = (isset($languages[$category->id])) ? $languages[$category->id] : FALSE;
  109. // Save localizations
  110. foreach ($post->category_title_lang as $lang_key => $localized_category_name)
  111. {
  112. $cl = (isset($category_lang[$lang_key]['id']))
  113. ? ORM::factory('category_lang',$category_lang[$lang_key]['id'])
  114. : ORM::factory('category_lang');
  115. $cl->category_title = $localized_category_name;
  116. $cl->locale = $lang_key;
  117. $cl->category_id = $category->id;
  118. $cl->save();
  119. }
  120. // Upload Image/Icon
  121. $filename = upload::save('category_image');
  122. if ($filename)
  123. {
  124. $new_filename = "category_".$category->id."_".time();
  125. // Name the files for the DB
  126. $cat_img_file = $new_filename.".png";
  127. $cat_img_thumb_file = $new_filename."_16x16.png";
  128. // Resize Image to 32px if greater
  129. Image::factory($filename)->resize(32,32,Image::HEIGHT)
  130. ->save(Kohana::config('upload.directory', TRUE) . $cat_img_file);
  131. // Create a 16x16 version too
  132. Image::factory($filename)->resize(16,16,Image::HEIGHT)
  133. ->save(Kohana::config('upload.directory', TRUE) . $cat_img_thumb_file);
  134. // Okay, now we have these three different files on the server, now check to see
  135. // if we should be dropping them on the CDN
  136. if(Kohana::config("cdn.cdn_store_dynamic_content"))
  137. {
  138. $cat_img_file = cdn::upload($cat_img_file);
  139. $cat_img_thumb_file = cdn::upload($cat_img_thumb_file);
  140. // We no longer need the files we created on the server. Remove them.
  141. $local_directory = rtrim(Kohana::config('upload.directory', TRUE), '/').'/';
  142. unlink($local_directory.$new_filename.".png");
  143. unlink($local_directory.$new_filename."_16x16.png");
  144. }
  145. // Remove the temporary file
  146. unlink($filename);
  147. // Delete Old Image
  148. $category_old_image = $category->category_image;
  149. if ( ! empty($category_old_image))
  150. {
  151. if(file_exists(Kohana::config('upload.directory', TRUE).$category_old_image))
  152. {
  153. unlink(Kohana::config('upload.directory', TRUE).$category_old_image);
  154. }elseif(Kohana::config("cdn.cdn_store_dynamic_content") AND valid::url($category_old_image)){
  155. cdn::delete($category_old_image);
  156. }
  157. }
  158. // Save
  159. $category->category_image = $cat_img_file;
  160. $category->category_image_thumb = $cat_img_thumb_file;
  161. $category->save();
  162. }
  163. $form_saved = TRUE;
  164. $form_action = strtoupper(Kohana::lang('ui_admin.added_edited'));
  165. // Empty $form array
  166. array_fill_keys($form, '');
  167. }
  168. else
  169. {
  170. // Validation failed
  171. // Repopulate the form fields
  172. $form = arr::overwrite($form, array_merge($category_data->as_array(), $post->as_array()));
  173. // populate the error fields, if any
  174. $errors = arr::overwrite($errors,
  175. array_merge($category_data->errors('category'), $post->errors('category')));
  176. $form_error = TRUE;
  177. }
  178. }
  179. elseif ($post->action == 'd')
  180. {
  181. // Delete action
  182. if ($category->loaded)
  183. {
  184. ORM::factory('category_lang')
  185. ->where(array('category_id' => $category->id))
  186. ->delete_all();
  187. // @todo Delete the category image
  188. // Delete category itself - except if it is trusted
  189. ORM::factory('category')
  190. ->where('category_trusted != 1')
  191. ->delete($category->id);
  192. $form_saved = TRUE;
  193. $form_action = strtoupper(Kohana::lang('ui_admin.deleted'));
  194. }
  195. }
  196. elseif( $post->action == 'v')
  197. {
  198. // Show/Hide Action
  199. if ($category->loaded)
  200. {
  201. $category->category_visible = ($category->category_visible == 1)? 0 : 1;
  202. $category->save();
  203. $form_saved = TRUE;
  204. $form_action = strtoupper(Kohana::lang('ui_admin.modified'));
  205. }
  206. }
  207. elseif( $post->action == 'i')
  208. {
  209. // Delete Image/Icon Action
  210. if ($category->loaded)
  211. {
  212. $category_image = $category->category_image;
  213. $category_image_thumb = $category->category_image_thumb;
  214. if ( ! empty($category_image)
  215. AND file_exists(Kohana::config('upload.directory', TRUE).$category_image))
  216. {
  217. unlink(Kohana::config('upload.directory', TRUE) . $category_image);
  218. }
  219. if ( ! empty($category_image_thumb)
  220. AND file_exists(Kohana::config('upload.directory', TRUE).$category_image_thumb))
  221. {
  222. unlink(Kohana::config('upload.directory', TRUE) . $category_image_thumb);
  223. }
  224. $category->category_image = NULL;
  225. $category->category_image_thumb = NULL;
  226. $category->save();
  227. $form_saved = TRUE;
  228. $form_action = strtoupper(Kohana::lang('ui_admin.modified'));
  229. }
  230. }
  231. }
  232. // Pagination
  233. $pagination = new Pagination(array(
  234. 'query_string' => 'page',
  235. 'items_per_page' => $this->items_per_page,
  236. 'total_items' => ORM::factory('category')
  237. ->where('parent_id','0')
  238. ->count_all()
  239. ));
  240. $categories = ORM::factory('category')
  241. ->with('category_lang')
  242. ->where('parent_id','0')
  243. ->orderby('category_position', 'asc')
  244. ->orderby('category_title', 'asc')
  245. ->find_all($this->items_per_page, $pagination->sql_offset);
  246. $parents_array = ORM::factory('category')
  247. ->where('parent_id','0')
  248. ->select_list('id', 'category_title');
  249. // add none to the list
  250. $parents_array[0] = "--- Top Level Category ---";
  251. // Put "--- Top Level Category ---" at the top of the list
  252. ksort($parents_array);
  253. $this->template->content->form = $form;
  254. $this->template->content->errors = $errors;
  255. $this->template->content->form_error = $form_error;
  256. $this->template->content->form_saved = $form_saved;
  257. $this->template->content->form_action = $form_action;
  258. $this->template->content->pagination = $pagination;
  259. $this->template->content->total_items = $pagination->total_items;
  260. $this->template->content->categories = $categories;
  261. $this->template->content->parents_array = $parents_array;
  262. // Javascript Header
  263. $this->template->colorpicker_enabled = TRUE;
  264. $this->template->tablerowsort_enabled = TRUE;
  265. $this->template->js = new View('admin/categories_js');
  266. $this->template->form_error = $form_error;
  267. $this->template->content->locale_array = $locales;
  268. $this->template->js->locale_array = $locales;
  269. }
  270. /**
  271. * Sorts categories
  272. */
  273. public function category_sort()
  274. {
  275. $this->auto_render = FALSE;
  276. $this->template = "";
  277. if ($_POST)
  278. {
  279. if (isset($_POST['categories'])
  280. AND ! empty($_POST['categories'])
  281. )
  282. {
  283. $categories = array_map('trim', explode(',', $_POST['categories']));
  284. $i = 0;
  285. $parent_id = 0;
  286. foreach ($categories as $category_id)
  287. {
  288. if ($category_id)
  289. {
  290. $category = ORM::factory('category', $category_id);
  291. if ($category->loaded)
  292. {
  293. if ($i == 0 AND $category->parent_id != 0)
  294. { // ERROR!!!!!!!! WHY ARE YOU TRYING TO PLACE A SUBCATEGORY ABOVE A CATEGORY???
  295. echo "ERROR";
  296. return;
  297. }
  298. if ($category->parent_id == 0)
  299. {
  300. // Set Parent ID
  301. $parent_id = $category->id;
  302. }
  303. else
  304. {
  305. if ($parent_id)
  306. {
  307. $category->parent_id = $parent_id;
  308. }
  309. }
  310. $category->category_position = $i;
  311. $category->save();
  312. }
  313. }
  314. $i++;
  315. }
  316. }
  317. }
  318. }
  319. /**
  320. * Manage Public Listing for External Applications
  321. */
  322. public function publiclisting()
  323. {
  324. $this->template->content = new View('admin/publiclisting');
  325. $settings = ORM::factory('settings', 1);
  326. $this->template->content->encoded_stat_id = base64_encode($settings->stat_id);
  327. $this->template->content->encoded_stat_key = base64_encode($settings->stat_key);
  328. }
  329. /**
  330. * Add Edit Pages
  331. */
  332. public function pages()
  333. {
  334. $this->template->content = new View('admin/pages');
  335. // setup and initialize form field names
  336. $form = array
  337. (
  338. 'action' => '',
  339. 'page_id' => '',
  340. 'page_title' => '',
  341. 'page_tab' => '',
  342. 'page_description' => ''
  343. );
  344. // Copy the form as errors, so the errors will be stored with keys corresponding to the form field names
  345. $errors = $form;
  346. $form_error = FALSE;
  347. $form_saved = FALSE;
  348. $form_action = "";
  349. // check, has the form been submitted, if so, setup validation
  350. if ($_POST)
  351. {
  352. $page = (isset($_POST['page_id']) AND Page_Model::is_valid_page($_POST['page_id']))
  353. ? ORM::factory('page', $_POST['page_id'])
  354. : new Page_Model();
  355. $post = array_merge($_POST, $_FILES);
  356. $post = array_merge($post, array("id"=>$page->id));
  357. Event::run('ushahidi_action.page_submit', $post);
  358. // Check for the specified action
  359. if ($_POST['action'] == 'a')
  360. {
  361. // Manually extract the data to be validated from $_POST
  362. $data = arr::extract($_POST, 'page_id', 'page_title', 'page_description', 'page_tab');
  363. if ($page->validate($data))
  364. {
  365. $page->save();
  366. Event::run('ushahidi_action.page_edit', $page);
  367. $form_saved = TRUE;
  368. $form_action = strtoupper(Kohana::lang('ui_admin.added_edited'));
  369. array_fill_keys($form, '');
  370. }
  371. else
  372. {
  373. // Repopulate the form fields
  374. $form = arr::overwrite($form, $data->as_array());
  375. // Populate the error fields, if any
  376. $errors = arr::overwrite($errors, $data->errors('page'));
  377. $form_error = TRUE;
  378. }
  379. }
  380. elseif ($_POST['action'] == 'd')
  381. {
  382. // Delete action
  383. if ($page->loaded)
  384. {
  385. $page->delete();
  386. $form_saved = TRUE;
  387. $form_action = strtoupper(Kohana::lang('ui_admin.deleted'));
  388. }
  389. }
  390. elseif ($_POST['action'] == 'v')
  391. {
  392. // Show/Hide Action
  393. if ($page->loaded)
  394. {
  395. $page->page_active = ($page->page_active == 1)? 0 : 1;
  396. $page->save();
  397. $form_saved = TRUE;
  398. $form_action = strtoupper(Kohana::lang('ui_admin.modified'));
  399. }
  400. }
  401. }
  402. // Pagination
  403. $pagination = new Pagination(array(
  404. 'query_string' => 'page',
  405. 'items_per_page' => $this->items_per_page,
  406. 'total_items' => ORM::factory('page')->count_all()
  407. ));
  408. $pages = ORM::factory('page')
  409. ->orderby('page_title', 'asc')
  410. ->find_all($this->items_per_page, $pagination->sql_offset);
  411. $this->template->content->form = $form;
  412. $this->template->content->form_error = $form_error;
  413. $this->template->content->form_saved = $form_saved;
  414. $this->template->content->form_action = $form_action;
  415. $this->template->content->pagination = $pagination;
  416. $this->template->content->total_items = $pagination->total_items;
  417. $this->template->content->pages = $pages;
  418. $this->template->content->errors = $errors;
  419. // Javascript Header
  420. $this->template->editor_enabled = TRUE;
  421. $this->template->js = new View('admin/pages_js');
  422. }
  423. /**
  424. * Add Edit News Feeds
  425. */
  426. public function feeds()
  427. {
  428. $this->template->content = new View('admin/feeds');
  429. // setup and initialize form field names
  430. $form = array
  431. (
  432. 'action' => '',
  433. 'feed_id' => '',
  434. 'feed_name' => '',
  435. 'feed_url' => '',
  436. 'feed_active' => ''
  437. );
  438. // copy the form as errors, so the errors will be stored with keys corresponding to the form field names
  439. $errors = $form;
  440. $form_error = FALSE;
  441. $form_saved = FALSE;
  442. $form_action = "";
  443. if( $_POST )
  444. {
  445. // Feed_Model instance
  446. $feed = (isset($_POST['feed_id']) AND Feed_Model::is_valid_feed($_POST['feed_id']))
  447. ? new Feed_Model($_POST['feed_id'])
  448. : new Feed_Model();
  449. if ($_POST['action'] == 'a') // Add Action
  450. {
  451. // Manually extract the data to be validated
  452. $data = arr::extract($_POST, 'feed_name', 'feed_url');
  453. // Test validation
  454. if ($feed->validate($data))
  455. {
  456. $feed->save();
  457. $form_saved = TRUE;
  458. $form_action = strtoupper(Kohana::lang('ui_admin.added_edited'));
  459. }
  460. else
  461. {
  462. // Repopulate the form fields
  463. $form = arr::overwrite($form, $data->as_array());
  464. // Populate the error fields, if any
  465. $errors = arr::overwrite($errors, $data->errors('feeds'));
  466. $form_error = TRUE;
  467. }
  468. }
  469. elseif ($_POST['action'] == 'd')
  470. {
  471. // Delete Action
  472. if ($feed->loaded == TRUE)
  473. {
  474. ORM::factory('feed_item')->where('feed_id', $feed->id)->delete_all();
  475. $feed->delete();
  476. $form_saved = TRUE;
  477. $form_action = strtoupper(Kohana::lang('ui_admin.deleted'));
  478. }
  479. }
  480. elseif($_POST['action'] == 'v')
  481. {
  482. // Active/Inactive Action
  483. if ($feed->loaded == TRUE)
  484. {
  485. $feed->feed_active = ($feed->feed_active == 1) ? 0 : 1;
  486. $feed->save();
  487. $form_saved = TRUE;
  488. $form_action = strtoupper(Kohana::lang('ui_admin.modified'));
  489. }
  490. }
  491. elseif ($_POST['action'] == 'r')
  492. {
  493. $this->_parse_feed();
  494. }
  495. }
  496. // Pagination
  497. $pagination = new Pagination(array(
  498. 'query_string' => 'page',
  499. 'items_per_page' => $this->items_per_page,
  500. 'total_items' => ORM::factory('feed')->count_all()
  501. ));
  502. $feeds = ORM::factory('feed')
  503. ->orderby('feed_name', 'asc')
  504. ->find_all($this->items_per_page, $pagination->sql_offset);
  505. $this->template->content->form_error = $form_error;
  506. $this->template->content->form_saved = $form_saved;
  507. $this->template->content->form_action = $form_action;
  508. $this->template->content->pagination = $pagination;
  509. $this->template->content->total_items = $pagination->total_items;
  510. $this->template->content->feeds = $feeds;
  511. $this->template->content->errors = $errors;
  512. // Javascript Header
  513. $this->template->colorpicker_enabled = TRUE;
  514. $this->template->js = new View('admin/feeds_js');
  515. }
  516. /**
  517. * View/Edit News Feed Items
  518. */
  519. public function feeds_items()
  520. {
  521. $this->template->content = new View('admin/feeds_items');
  522. // Check if the last segment of the URI is numeric and grab it
  523. $feed_id = is_numeric($this->uri->last_segment())
  524. ? $this->uri->last_segment()
  525. : "";
  526. // SQL filter
  527. $filter = (isset($feed_id) AND !empty($feed_id))
  528. ? " feed_id = '" . $feed_id . "' "
  529. : " 1=1";
  530. $form_error = FALSE;
  531. $form_saved = FALSE;
  532. $form_action = "";
  533. // Check for form submission
  534. if ( $_POST )
  535. {
  536. $post = Validation::factory($_POST);
  537. // Add some filters
  538. $post->pre_filter('trim', TRUE);
  539. if( $post->validate() )
  540. {
  541. $item_id = $this->input->post('item_id');
  542. ORM::factory('feed_item')->delete($item_id);
  543. $form_saved = TRUE;
  544. $form_action = strtoupper(Kohana::lang('ui_admin.deleted'));
  545. }
  546. }
  547. // Pagination
  548. $pagination = new Pagination(array(
  549. 'query_string' => 'page',
  550. 'items_per_page' => $this->items_per_page,
  551. 'total_items' => ORM::factory('feed_item')
  552. ->where($filter)
  553. ->count_all()
  554. ));
  555. $feed_items = ORM::factory('feed_item')
  556. ->where($filter)
  557. ->orderby('item_date','desc')
  558. ->find_all($this->items_per_page, $pagination->sql_offset);
  559. $this->template->content->feed_items = $feed_items;
  560. $this->template->content->pagination = $pagination;
  561. $this->template->content->form_error = $form_error;
  562. $this->template->content->form_saved = $form_saved;
  563. $this->template->content->form_action = $form_action;
  564. // Total Reports
  565. $this->template->content->total_items = $pagination->total_items;
  566. // Javascript Header
  567. $this->template->js = new View('admin/feeds_items_js');
  568. }
  569. /**
  570. * Add Edit Layers (KML, KMZ, GeoRSS)
  571. */
  572. public function layers()
  573. {
  574. $this->template->content = new View('admin/layers');
  575. $this->template->content->title = Kohana::lang('ui_admin.layers');
  576. // Setup and initialize form field names
  577. $form = array
  578. (
  579. 'action' => '',
  580. 'layer_id' => '',
  581. 'layer_name' => '',
  582. 'layer_url' => '',
  583. 'layer_file' => '',
  584. 'layer_color' => ''
  585. );
  586. // Copy the form as errors, so the errors will be stored with keys corresponding to the form field names
  587. $errors = $form;
  588. $form_error = FALSE;
  589. $form_saved = FALSE;
  590. $form_action = "";
  591. $parents_array = array();
  592. // Check, has the form been submitted, if so, setup validation
  593. if ($_POST)
  594. {
  595. // Fetch the submitted data
  596. $post_data = array_merge($_POST, $_FILES);
  597. // Layer instance for the actions
  598. $layer = (isset($post_data['layer_id']) AND Layer_Model::is_valid_layer($post_data['layer_id']))
  599. ? new Layer_Model($post_data['layer_id'])
  600. : new Layer_Model();
  601. // Check for action
  602. if ($post_data['action'] == 'a')
  603. {
  604. // Manually extract the primary layer data
  605. $layer_data = arr::extract($post_data, 'layer_name', 'layer_color', 'layer_url', 'layer_file_old');
  606. // Grab the layer file to be uploaded
  607. $layer_data['layer_file'] = isset($post_data['layer_file']['name'])? $post_data['layer_file']['name'] : NULL;
  608. // Extract the layer file for upload validation
  609. $other_data = arr::extract($post_data, 'layer_file');
  610. // Set up validation for the layer file
  611. $post = Validation::factory($other_data)
  612. ->pre_filter('trim', TRUE)
  613. ->add_rules('layer_file', 'upload::valid','upload::type[kml,kmz]');
  614. // Test to see if validation has passed
  615. if ($layer->validate($layer_data) AND $post->validate())
  616. {
  617. // Success! SAVE
  618. $layer->save();
  619. $path_info = upload::save("layer_file");
  620. if ($path_info)
  621. {
  622. $path_parts = pathinfo($path_info);
  623. $file_name = $path_parts['filename'];
  624. $file_ext = $path_parts['extension'];
  625. $layer_file = $file_name.".".$file_ext;
  626. $layer_url = '';
  627. if (strtolower($file_ext) == "kmz")
  628. {
  629. // This is a KMZ Zip Archive, so extract
  630. $archive = new Pclzip($path_info);
  631. if (TRUE == ($archive_files = $archive->extract(PCLZIP_OPT_EXTRACT_AS_STRING)))
  632. {
  633. foreach ($archive_files as $file)
  634. {
  635. $ext_file_name = $file['filename'];
  636. }
  637. }
  638. if ($ext_file_name AND $archive->extract(PCLZIP_OPT_PATH, Kohana::config('upload.directory')) == TRUE)
  639. {
  640. // Okay, so we have an extracted KML - Rename it and delete KMZ file
  641. rename($path_parts['dirname']."/".$ext_file_name,
  642. $path_parts['dirname']."/".$file_name.".kml");
  643. $file_ext = "kml";
  644. unlink($path_info);
  645. }
  646. }
  647. // Upload the KML to the CDN server if configured
  648. if (Kohana::config("cdn.cdn_store_dynamic_content"))
  649. {
  650. // Upload the file to the CDN
  651. $layer_url = cdn::upload($layer_file);
  652. // We no longer need the files we created on the server. Remove them.
  653. $local_directory = rtrim(Kohana::config('upload.directory', TRUE), '/').'/';
  654. unlink($local_directory.$layer_file);
  655. // We no longer need to store the file name for the local file since it's gone
  656. $layer_file = '';
  657. }
  658. // Set the final variables for the DB
  659. $layer->layer_url = $layer_url;
  660. $layer->layer_file = $layer_file;
  661. $layer->save();
  662. }
  663. $form_saved = TRUE;
  664. array_fill_keys($form, '');
  665. $form_action = strtoupper(Kohana::lang('ui_admin.added_edited'));
  666. }
  667. else
  668. {
  669. // Validation failed
  670. // Repopulate the form fields
  671. $form = arr::overwrite($form, array_merge($layer_data->as_array(), $post->as_array()));
  672. // Ropulate the error fields, if any
  673. $errors = arr::overwrite($errors, array_merge($layer_data->errors('layer'), $post->errors('layer')));
  674. $form_error = TRUE;
  675. }
  676. }
  677. elseif ($post_data['action'] == 'd')
  678. {
  679. // Delete action
  680. if ($layer->loaded)
  681. {
  682. // Delete KMZ file if any
  683. $layer_file = $layer->layer_file;
  684. if ( ! empty($layer_file) AND file_exists(Kohana::config('upload.directory', TRUE).$layer_file))
  685. {
  686. unlink(Kohana::config('upload.directory', TRUE) . $layer_file);
  687. }
  688. $layer->delete();
  689. $form_saved = TRUE;
  690. $form_action = strtoupper(Kohana::lang('ui_admin.deleted'));
  691. }
  692. }
  693. elseif ($post_data['action'] == 'v')
  694. {
  695. // Show/Hide Action
  696. if ($layer->loaded == TRUE)
  697. {
  698. $layer->layer_visible = ($layer->layer_visible == 1)? 0 : 1;
  699. $layer->save();
  700. $form_saved = TRUE;
  701. $form_action = strtoupper(Kohana::lang('ui_admin.modified'));
  702. }
  703. }
  704. elseif ($post_data['action'] == 'i')
  705. {
  706. // Delete KML/KMZ action
  707. if ($layer->loaded == TRUE)
  708. {
  709. $layer_file = $layer->layer_file;
  710. if ( ! empty($layer_file) AND file_exists(Kohana::config('upload.directory', TRUE).$layer_file))
  711. {
  712. unlink(Kohana::config('upload.directory', TRUE) . $layer_file);
  713. }
  714. $layer->layer_file = null;
  715. $layer->save();
  716. $form_saved = TRUE;
  717. $form_action = strtoupper(Kohana::lang('ui_admin.modified'));
  718. }
  719. }
  720. }
  721. // Pagination
  722. $pagination = new Pagination(array(
  723. 'query_string' => 'page',
  724. 'items_per_page' => $this->items_per_page,
  725. 'total_items' => ORM::factory('layer')->count_all()
  726. ));
  727. $layers = ORM::factory('layer')
  728. ->orderby('layer_name', 'asc')
  729. ->find_all($this->items_per_page, $pagination->sql_offset);
  730. $this->template->content->errors = $errors;
  731. $this->template->content->form_error = $form_error;
  732. $this->template->content->form_saved = $form_saved;
  733. $this->template->content->form_action = $form_action;
  734. $this->template->content->pagination = $pagination;
  735. $this->template->content->total_items = $pagination->total_items;
  736. $this->template->content->layers = $layers;
  737. // Javascript Header
  738. $this->template->colorpicker_enabled = TRUE;
  739. $this->template->js = new View('admin/layers_js');
  740. }
  741. /**
  742. * Add Edit Reporter Levels
  743. */
  744. public function levels()
  745. {
  746. $this->template->content = new View('admin/levels');
  747. $this->template->content->title = Kohana::lang('ui_admin.reporter_levels');
  748. // setup and initialize form field names
  749. $form = array
  750. (
  751. 'level_id' => '',
  752. 'level_title' => '',
  753. 'level_description' => '',
  754. 'level_weight' => ''
  755. );
  756. // Copy the form as errors, so the errors will be stored with keys corresponding to the form field names
  757. $errors = $form;
  758. $form_error = FALSE;
  759. $form_saved = FALSE;
  760. $form_action = "";
  761. // Check, has the form been submitted, if so, setup validation
  762. if ($_POST)
  763. {
  764. // Level_Model instance for the opertation
  765. $level = (isset($_POST['level_id']) AND Level_Model::is_valid_level($_POST['level_id']))
  766. ? new Level_Model($_POST['level_id'])
  767. : new Level_Model();
  768. if ($_POST['action'] == 'a')
  769. {
  770. // Manually extract the data to be validated
  771. $data = arr::extract($_POST, 'level_title', 'level_description', 'level_weight');
  772. if ($level->validate($data))
  773. {
  774. $level->save();
  775. $form_saved = TRUE;
  776. $form_action = strtoupper(Kohana::lang('ui_admin.added_edited'));
  777. }
  778. // No! We have validation errors, we need to show the form again, with the errors
  779. else
  780. {
  781. // Repopulate the form fields
  782. $form = arr::overwrite($form, $data->as_array());
  783. // Ropulate the error fields, if any
  784. $errors = arr::overwrite($errors, $data->errors('level'));
  785. $form_error = TRUE;
  786. }
  787. }
  788. elseif ($_POST['action'] == 'd')
  789. {
  790. if ($level->loaded)
  791. {
  792. // Levels are tied to reporters, therefore nullify
  793. // @todo Reporter_Model::delink_level($level_id)
  794. $level->delete();
  795. $form_saved = TRUE;
  796. $form_action = strtoupper(Kohana::lang('ui_admin.deleted'));
  797. }
  798. }
  799. }
  800. // Pagination
  801. $pagination = new Pagination(array(
  802. 'query_string' => 'page',
  803. 'items_per_page' => $this->items_per_page,
  804. 'total_items' => ORM::factory('level')->count_all()
  805. ));
  806. $levels = ORM::factory('level')
  807. ->orderby('level_weight', 'asc')
  808. ->find_all($this->items_per_page, $pagination->sql_offset);
  809. $this->template->content->errors = $errors;
  810. $this->template->content->form_error = $form_error;
  811. $this->template->content->form_saved = $form_saved;
  812. $this->template->content->form_action = $form_action;
  813. $this->template->content->pagination = $pagination;
  814. $this->template->content->total_items = $pagination->total_items;
  815. $this->template->content->levels = $levels;
  816. }
  817. /**
  818. * get the feed type of the feed item.
  819. */
  820. private function _get_feed_type( $feed_item )
  821. {
  822. @$enclosures = $feed_item->get_enclosures();
  823. if($enclosures and ($enclosures[0]->medium == 'video' || strstr($enclosures[0]->type,'video')))
  824. {
  825. return FEED_TYPE_VIDEO;
  826. }
  827. if($enclosures and strstr($enclosures[0]->type,'image') || $enclosures[0]->medium == 'image')
  828. {
  829. return FEED_TYPE_PHOTO;
  830. }
  831. $categories = $feed_item->get_categories();
  832. if(!$categories || empty($categories))
  833. {
  834. return FEED_TYPE_TEXT;
  835. }
  836. // go through categories for the label having Report Type
  837. foreach($categories as $key => $category)
  838. {
  839. if ( ! empty($category->label))
  840. {
  841. $matched = strstr($category->label,'Report type');
  842. if ( ! empty($matched))
  843. {
  844. $split_array = split(':', $category->label);
  845. return trim($split_array[1]);
  846. }
  847. }
  848. }
  849. return FEED_TYPE_TEXT;
  850. }
  851. /**
  852. * parse feed and send feed items to database
  853. */
  854. private function _parse_feed()
  855. {
  856. // Max number of feeds to keep
  857. $max_feeds = 100;
  858. // Today's Date
  859. $today = strtotime('now');
  860. // Get All Feeds From DB
  861. $feeds = ORM::factory('feed')->find_all();
  862. foreach ($feeds as $feed)
  863. {
  864. $last_update = $feed->feed_update;
  865. // Has it been more than 24 hours since the last update?
  866. // Since its a manual refresh, we don't need to set a time
  867. if ( ((int)$today - (int)$last_update) > 0 ) // 86400 = 24 hours
  868. {
  869. // Parse Feed URL using Feed Helper
  870. $feed_data = feed::simplepie( $feed->feed_url );
  871. foreach ($feed_data->get_items(0,50) as $feed_data_item)
  872. {
  873. $title = $feed_data_item->get_title();
  874. $link = $feed_data_item->get_link();
  875. $description = $feed_data_item->get_description();
  876. $date = $feed_data_item->get_date();
  877. $latitude = $feed_data_item->get_latitude();
  878. $longitude = $feed_data_item->get_longitude();
  879. // Make Sure Title is Set (Atleast)
  880. if (isset($title) AND !empty($title ))
  881. {
  882. // We need to check for duplicates!!!
  883. // Maybe combination of Title + Date? (Kinda Heavy on the Server :-( )
  884. $dupe_count = ORM::factory('feed_item')->where('item_title',$title)->where('item_date',date("Y-m-d H:i:s",strtotime($date)))->count_all();
  885. if ($dupe_count == 0)
  886. {
  887. // Does this feed have a location??
  888. $location_id = 0;
  889. // STEP 1: SAVE LOCATION
  890. if ($latitude AND $longitude)
  891. {
  892. $location = new Location_Model();
  893. $location->location_name = Kohana::lang('ui_admin.unknown');
  894. $location->latitude = $latitude;
  895. $location->longitude = $longitude;
  896. $location->location_date = date("Y-m-d H:i:s",time());
  897. $location->save();
  898. $location_id = $location->id;
  899. }
  900. $newitem = new Feed_Item_Model();
  901. $newitem->feed_id = $feed->id;
  902. $newitem->location_id = $location_id;
  903. $newitem->item_title = $title;
  904. if (isset($description) AND !empty($description))
  905. {
  906. $newitem->item_description = $description;
  907. }
  908. if (isset($link) AND !empty($link))
  909. {
  910. $newitem->item_link = $link;
  911. }
  912. if (isset($date) AND !empty($date))
  913. {
  914. $newitem->item_date = date("Y-m-d H:i:s",strtotime($date));
  915. }
  916. // Set todays date
  917. else
  918. {
  919. $newitem->item_date = date("Y-m-d H:i:s",time());
  920. }
  921. if (isset($feed_type) AND ! empty($feed_type))
  922. {
  923. $newitem->feed_type = $feed_type;
  924. }
  925. $newitem->save();
  926. }
  927. }
  928. }
  929. // Get Feed Item Count
  930. $feed_count = ORM::factory('feed_item')->where('feed_id', $feed->id)->count_all();
  931. if ($feed_count > $max_feeds)
  932. {
  933. // Excess Feeds
  934. $feed_excess = $feed_count - $max_feeds;
  935. // Delete Excess Feeds
  936. foreach (ORM::factory('feed_item')
  937. ->where('feed_id', $feed->id)
  938. ->orderby('id', 'ASC')
  939. ->limit($feed_excess)
  940. ->find_all() as $del_feed)
  941. {
  942. $del_feed->delete($del_feed->id);
  943. }
  944. }
  945. // Set feed update date
  946. $feed->feed_update = strtotime('now');
  947. $feed->save();
  948. }
  949. }
  950. }
  951. }