Exemple #1
0
 /**
  * Add Edit Layers (KML, KMZ, GeoRSS)
  */
 public function layers()
 {
     $this->template->content = new View('admin/layers');
     $this->template->content->title = Kohana::lang('ui_admin.layers');
     // Setup and initialize form field names
     $form = array('action' => '', 'layer_id' => '', 'layer_name' => '', 'layer_url' => '', 'layer_file' => '', 'layer_color' => '');
     // Copy the form as errors, so the errors will be stored with keys corresponding to the form field names
     $errors = $form;
     $form_error = FALSE;
     $form_saved = FALSE;
     $form_action = "";
     $parents_array = array();
     // Check, has the form been submitted, if so, setup validation
     if ($_POST) {
         // Fetch the submitted data
         $post_data = array_merge($_POST, $_FILES);
         // Layer instance for the actions
         $layer = (isset($post_data['layer_id']) and Layer_Model::is_valid_layer($post_data['layer_id'])) ? new Layer_Model($post_data['layer_id']) : new Layer_Model();
         // Check for action
         if ($post_data['action'] == 'a') {
             // Manually extract the primary layer data
             $layer_data = arr::extract($post_data, 'layer_name', 'layer_color', 'layer_url', 'layer_file_old');
             // Grab the layer file to be uploaded
             $layer_data['layer_file'] = isset($post_data['layer_file']['name']) ? $post_data['layer_file']['name'] : NULL;
             // Extract the layer file for upload validation
             $other_data = arr::extract($post_data, 'layer_file');
             // Set up validation for the layer file
             $post = Validation::factory($other_data)->pre_filter('trim', TRUE)->add_rules('layer_file', 'upload::valid', 'upload::type[kml,kmz]');
             // Test to see if validation has passed
             if ($layer->validate($layer_data) and $post->validate()) {
                 // Success! SAVE
                 $layer->save();
                 $path_info = upload::save("layer_file");
                 if ($path_info) {
                     $path_parts = pathinfo($path_info);
                     $file_name = $path_parts['filename'];
                     $file_ext = $path_parts['extension'];
                     if (strtolower($file_ext) == "kmz") {
                         // This is a KMZ Zip Archive, so extract
                         $archive = new Pclzip($path_info);
                         if (TRUE == ($archive_files = $archive->extract(PCLZIP_OPT_EXTRACT_AS_STRING))) {
                             foreach ($archive_files as $file) {
                                 $ext_file_name = $file['filename'];
                             }
                         }
                         if ($ext_file_name and $archive->extract(PCLZIP_OPT_PATH, Kohana::config('upload.directory')) == TRUE) {
                             // Okay, so we have an extracted KML - Rename it and delete KMZ file
                             rename($path_parts['dirname'] . "/" . $ext_file_name, $path_parts['dirname'] . "/" . $file_name . ".kml");
                             $file_ext = "kml";
                             unlink($path_info);
                         }
                     }
                     $layer->layer_file = $file_name . "." . $file_ext;
                     $layer->save();
                 }
                 $form_saved = TRUE;
                 array_fill_keys($form, '');
                 $form_action = strtoupper(Kohana::lang('ui_admin.added_edited'));
             } else {
                 // Validation failed
                 // Repopulate the form fields
                 $form = arr::overwrite($form, array_merge($layer_data->as_array(), $post->as_array()));
                 // Ropulate the error fields, if any
                 $errors = arr::overwrite($errors, array_merge($layer_data->errors('layer'), $post->errors('layer')));
                 $form_error = TRUE;
             }
         } elseif ($post_data['action'] == 'd') {
             // Delete action
             if ($layer->loaded) {
                 // Delete KMZ file if any
                 $layer_file = $layer->layer_file;
                 if (!empty($layer_file) and file_exists(Kohana::config('upload.directory', TRUE) . $layer_file)) {
                     unlink(Kohana::config('upload.directory', TRUE) . $layer_file);
                 }
                 $layer->delete();
                 $form_saved = TRUE;
                 $form_action = strtoupper(Kohana::lang('ui_admin.deleted'));
             }
         } elseif ($post_data['action'] == 'v') {
             // Show/Hide Action
             if ($layer->loaded == TRUE) {
                 $layer->layer_visible = $layer->layer_visible == 1 ? 0 : 1;
                 $layer->save();
                 $form_saved = TRUE;
                 $form_action = strtoupper(Kohana::lang('ui_admin.modified'));
             }
         } elseif ($post_data['action'] == 'i') {
             // Delete KML/KMZ action
             if ($layer->loaded == TRUE) {
                 $layer_file = $layer->layer_file;
                 if (!empty($layer_file) and file_exists(Kohana::config('upload.directory', TRUE) . $layer_file)) {
                     unlink(Kohana::config('upload.directory', TRUE) . $layer_file);
                 }
                 $layer->layer_file = null;
                 $layer->save();
                 $form_saved = TRUE;
                 $form_action = strtoupper(Kohana::lang('ui_admin.modified'));
             }
         }
     }
     // Pagination
     $pagination = new Pagination(array('query_string' => 'page', 'items_per_page' => $this->items_per_page, 'total_items' => ORM::factory('layer')->count_all()));
     $layers = ORM::factory('layer')->orderby('layer_name', 'asc')->find_all($this->items_per_page, $pagination->sql_offset);
     $this->template->content->errors = $errors;
     $this->template->content->form_error = $form_error;
     $this->template->content->form_saved = $form_saved;
     $this->template->content->form_action = $form_action;
     $this->template->content->pagination = $pagination;
     $this->template->content->total_items = $pagination->total_items;
     $this->template->content->layers = $layers;
     // Javascript Header
     $this->template->colorpicker_enabled = TRUE;
     $this->template->js = new View('admin/layers_js');
 }
Exemple #2
0
 function layers()
 {
     $this->template->content = new View('admin/layers');
     $this->template->content->title = Kohana::lang('ui_admin.layers');
     // setup and initialize form field names
     $form = array('action' => '', 'layer_id' => '', 'layer_name' => '', 'layer_url' => '', 'layer_file' => '', 'layer_color' => '');
     // copy the form as errors, so the errors will be stored with keys corresponding to the form field names
     $errors = $form;
     $form_error = FALSE;
     $form_saved = FALSE;
     $form_action = "";
     $parents_array = array();
     // check, has the form been submitted, if so, setup validation
     if ($_POST) {
         // Instantiate Validation, use $post, so we don't overwrite $_POST fields with our own things
         $post = Validation::factory(array_merge($_POST, $_FILES));
         //  Add some filters
         $post->pre_filter('trim', TRUE);
         if ($post->action == 'a') {
             // Add some rules, the input field, followed by a list of checks, carried out in order
             $post->add_rules('layer_name', 'required', 'length[3,80]');
             $post->add_rules('layer_color', 'required', 'length[6,6]');
             $post->add_rules('layer_url', 'url');
             $post->add_rules('layer_file', 'upload::valid', 'upload::type[kml,kmz]');
             if (empty($_POST['layer_url']) && empty($_FILES['layer_file']['name']) && empty($_POST['layer_file_old'])) {
                 $post->add_error('layer_url', 'atleast');
             }
             if (!empty($_POST['layer_url']) && (!empty($_FILES['layer_file']['name']) || !empty($_POST['layer_file_old']))) {
                 $post->add_error('layer_url', 'both');
             }
         }
         // Test to see if things passed the rule checks
         if ($post->validate()) {
             $layer_id = $post->layer_id;
             $layer = new Layer_Model($layer_id);
             if ($post->action == 'd') {
                 // Delete Action
                 // Delete KMZ file if any
                 $layer_file = $layer->layer_file;
                 if (!empty($layer_file) && file_exists(Kohana::config('upload.directory', TRUE) . $layer_file)) {
                     unlink(Kohana::config('upload.directory', TRUE) . $layer_file);
                 }
                 $layer->delete($layer_id);
                 $form_saved = TRUE;
                 $form_action = strtoupper(Kohana::lang('ui_admin.deleted'));
             } else {
                 if ($post->action == 'v') {
                     // Show/Hide Action
                     if ($layer->loaded == true) {
                         if ($layer->layer_visible == 1) {
                             $layer->layer_visible = 0;
                         } else {
                             $layer->layer_visible = 1;
                         }
                         $layer->save();
                         $form_saved = TRUE;
                         $form_action = strtoupper(Kohana::lang('ui_admin.modified'));
                     }
                 } else {
                     if ($post->action == 'i') {
                         // Delete KMZ/KML Action
                         if ($layer->loaded == true) {
                             $layer_file = $layer->layer_file;
                             if (!empty($layer_file) && file_exists(Kohana::config('upload.directory', TRUE) . $layer_file)) {
                                 unlink(Kohana::config('upload.directory', TRUE) . $layer_file);
                             }
                             $layer->layer_file = null;
                             $layer->save();
                             $form_saved = TRUE;
                             $form_action = strtoupper(Kohana::lang('ui_admin.modified'));
                         }
                     } else {
                         if ($post->action == 'a') {
                             // Save Action
                             $layer->layer_name = $post->layer_name;
                             $layer->layer_url = $post->layer_url;
                             $layer->layer_color = $post->layer_color;
                             $layer->save();
                             // Upload KMZ/KML
                             $path_info = upload::save("layer_file");
                             if ($path_info) {
                                 $path_parts = pathinfo($path_info);
                                 $file_name = $path_parts['filename'];
                                 $file_ext = $path_parts['extension'];
                                 if (strtolower($file_ext) == "kmz") {
                                     // This is a KMZ Zip Archive, so extract
                                     $archive = new Pclzip($path_info);
                                     if (TRUE == ($archive_files = $archive->extract(PCLZIP_OPT_EXTRACT_AS_STRING))) {
                                         foreach ($archive_files as $file) {
                                             $ext_file_name = $file['filename'];
                                         }
                                     }
                                     if ($ext_file_name && $archive->extract(PCLZIP_OPT_PATH, Kohana::config('upload.directory')) == TRUE) {
                                         // Okay, so we have an extracted KML - Rename it and delete KMZ file
                                         rename($path_parts['dirname'] . "/" . $ext_file_name, $path_parts['dirname'] . "/" . $file_name . ".kml");
                                         $file_ext = "kml";
                                         unlink($path_info);
                                     }
                                 }
                                 $layer->layer_file = $file_name . "." . $file_ext;
                                 $layer->save();
                             }
                             $form_saved = TRUE;
                             $form_action = strtoupper(Kohana::lang('ui_admin.added_edited'));
                         }
                     }
                 }
             }
         } else {
             // repopulate the form fields
             $form = arr::overwrite($form, $post->as_array());
             // populate the error fields, if any
             $errors = arr::overwrite($errors, $post->errors('layer'));
             $form_error = TRUE;
         }
     }
     // Pagination
     $pagination = new Pagination(array('query_string' => 'page', 'items_per_page' => (int) Kohana::config('settings.items_per_page_admin'), 'total_items' => ORM::factory('layer')->count_all()));
     $layers = ORM::factory('layer')->orderby('layer_name', 'asc')->find_all((int) Kohana::config('settings.items_per_page_admin'), $pagination->sql_offset);
     $this->template->content->errors = $errors;
     $this->template->content->form_error = $form_error;
     $this->template->content->form_saved = $form_saved;
     $this->template->content->form_action = $form_action;
     $this->template->content->pagination = $pagination;
     $this->template->content->total_items = $pagination->total_items;
     $this->template->content->layers = $layers;
     // Javascript Header
     $this->template->colorpicker_enabled = TRUE;
     $this->template->js = new View('admin/layers_js');
 }
Exemple #3
0
 /**
  * Upgrades files over time based on users hitting the site
  *
  * @return bool TRUE if the upgrade is successful, FALSE otherwise
  */
 public static function gradual_upgrade()
 {
     self::connection();
     if (!self::$cdn) {
         throw new Kohana_Exception('CDN provider not specified');
     }
     // Get the table prefix
     $table_prefix = Kohana::config('database.default.table_prefix');
     // Select at random since admin may not want every user to initiate a CDN upload
     if (rand(1, intval(Kohana::config('cdn.cdn_gradual_upgrade'))) == 1) {
         $query = 'SELECT id, media_link, media_medium, media_thumb ' . 'FROM ' . $table_prefix . 'media ' . 'WHERE media_type = 1 AND media_link NOT LIKE \'http%\' LIMIT 1';
         // Database instance for fetch and update operations
         $db = Database::instance();
         $media_links = $db->query($query);
         $uploaded_flag = false;
         foreach ($media_links as $row) {
             // Upload files to the CDN
             $new_large = self::$cdn->upload($row->media_link);
             $new_medium = self::$cdn->upload($row->media_medium);
             $new_thumb = self::$cdn->upload($row->media_thumb);
             // Update the entry for the media file in the DB
             $db->update('media', array('media_link' => $new_large, 'media_medium' => $new_medium, 'media_thumb' => $new_thumb), array('id' => $row->id));
             // Remove old files
             $local_directory = Kohana::config('upload.directory', TRUE);
             $local_directory = rtrim($local_directory, '/') . '/';
             unlink($local_directory . $row->media_link);
             unlink($local_directory . $row->media_medium);
             unlink($local_directory . $row->media_thumb);
             $uploaded_flag = true;
         }
         // If we didn't have any more user uploaded images to upload, move on to category images
         if ($uploaded_flag == false) {
             $query = 'SELECT id, category_image, category_image_thumb ' . 'FROM ' . $table_prefix . 'category ' . 'WHERE category_image NOT LIKE \'http%\' LIMIT 1';
             // Fetch
             $category_images = $db->query($query);
             foreach ($category_images as $row) {
                 // Upload files to the CDN
                 $new_category_image = $this->cdn->upload($row->category_image);
                 $new_category_image_thumb = $this->cdn->upload($row->category_image_thumb);
                 // Update the entry for the media file in the DB
                 $db->update('category', array('category_image' => $new_category_image, 'category_image_thumb' => $new_category_image_thumb), array('id' => $row->id));
                 // Remove old files
                 $local_directory = Kohana::config('upload.directory', TRUE);
                 $local_directory = rtrim($local_directory, '/') . '/';
                 unlink($local_directory . $row['category_image']);
                 unlink($local_directory . $row['category_image_thumb']);
                 $uploaded_flag = true;
             }
         }
         // If we are done with category images, move on to KML layers
         if ($uploaded_flag == false) {
             // Grab a KML file we have locally that isn't linking to an external URL
             $query = 'SELECT id, layer_file ' . 'FROM ' . $table_prefix . 'layer ' . 'WHERE layer_url = \'\' LIMIT 1';
             $layers = $db->query($query);
             foreach ($layers as $row) {
                 $layer_file = $row->layer_file;
                 // Upload the file to the CDN
                 $layer_url = cdn::upload($layer_file);
                 // We no longer need the files we created on the server. Remove them.
                 $local_directory = rtrim(Kohana::config('upload.directory', TRUE), '/') . '/';
                 unlink($local_directory . $layer_file);
                 $layer = new Layer_Model($row->id);
                 $layer->layer_url = $layer_url;
                 $layer->layer_file = '';
                 $layer->save();
             }
         }
         // Garbage collection
         unset($db, $media_links, $category_images);
         return TRUE;
     }
     return FALSE;
 }