Your IP : 216.73.216.240


Current Path : /home/juvelize/www/administrator/components/com_zhgooglemap/controllers/
Upload File :
Current File : /home/juvelize/www/administrator/components/com_zhgooglemap/controllers/mapbufmrk.php

<?php
/*------------------------------------------------------------------------
# com_zhgooglemap - Zh GoogleMap
# ------------------------------------------------------------------------
# author:    Dmitry Zhuk
# copyright: Copyright (C) 2011 zhuk.cc. All Rights Reserved.
# license:   http://www.gnu.org/licenses/gpl-2.0.html GNU/GPLv2 or later
# website:   http://zhuk.cc
# Technical Support Forum: http://forum.zhuk.cc/
-------------------------------------------------------------------------*/
// No direct access to this file
defined('_JEXEC') or die('Restricted access');

use Joomla\Utilities\ArrayHelper;

// import Joomla controllerform library
jimport('joomla.application.component.controllerform');
jimport('joomla.filesystem.file');

/**
 * ZhGoogle MapBufmrk Controller
 */
class ZhGoogleMapControllerMapBufmrk extends JControllerForm
{

    /**
     * Method override to check if you can edit an existing record.
     *
     * @param   array   $data  An array of input data.
     * @param   string  $key   The name of the key for the primary key.
     *
     * @return  boolean
     *
     * @since   1.6
     */
    protected function allowEdit($data = array(), $key = 'id')
    {
        $recordId = (int) isset($data[$key]) ? $data[$key] : 0;
        $user = JFactory::getUser();
        $userId = $user->get('id');

        $canDo = ZhGoogleMapHelper::getBufmrkActions($recordId);
        
        $canEdit    = $canDo->get('core.edit');
        $isEnabledEditOwn = $canDo->get('core.edit.own');
        
        
        if ($canEdit || $canEditOwn)
        {
        }
        
        // Check general edit permission first.
        if ($canEdit)
        {
            return true;
        }

        // Fallback on edit.own.
        // First test if the permission is available.
        if ($isEnabledEditOwn)
        {
            // Now test the owner is the user.
            $ownerId = (int) isset($data['createdbyuser']) ? $data['createdbyuser'] : 0;
            if (empty($ownerId) && $recordId)
            {
                // Need to do a lookup from the model.
                $record = $this->getModel()->getItem($recordId);

                if (empty($record))
                {
                    return false;
                }

                $ownerId = $record->createdbyuser;
            }

            // If the owner matches 'me' then do the test.
            if ($ownerId == $userId)
            {
                return true;
            }
        }

        // Since there is no asset tracking, revert to the component permissions.
        return parent::allowEdit($data, $key);
    }

        
    function getFileSizeText($bytes, $precision = 2, $space = false)
    {
        $units = array('B', 'KB', 'MB', 'GB', 'TB', 'PB'); 

        $bytes = max($bytes, 0); 
        $pow = floor(($bytes ? log($bytes) : 0) / log(1024)); 
        $pow = min($pow, count($units) - 1); 

        return round($bytes, $precision) . ($space ? ' ' : '') . $units[$pow];
    }
    
    function getFileSizeBytes($size) 
    {
        $size_fix = str_replace(" ", "", $size);
        $units = array('B', 'KB', 'MB', 'GB', 'TB', 'PB');
        $numbers = substr($size_fix, 0, -2);
        $suffix = strtoupper(substr($size_fix,-2));

        //B or no suffix
        if(is_numeric(substr($suffix, 0, 1))) {
            return preg_replace('/[^\d]/', '', $size_fix);
        }

        $flipped = array_flip($units);
        
        $exponent = $flipped[$suffix];
        if($exponent === null) {
            return null;
        }

        return $numbers * (1024 ** $exponent);
    }

    
    
    public function marker_load_all()
    {
            // Check for request forgeries.
            JSession::checkToken() or jexit(JText::_('JINVALID_TOKEN'));

            // Get the model
            $model = $this->getModel();
            
            $app = JFactory::getApplication(); 
        $input = $app->input; 
            // get the data from the HTTP POST request
            $data  = $input->get('jform', array(), 'array');

            // Get the current URI to set in redirects. As we're handling a POST, 
            // this URI comes from the <form action="..."> attribute in the layout file above
            $currentUri = (string)JUri::getInstance();
          
            $mapid = $data["mapid"];
          
            $flg_validate_text = "";
            $flg_validate_error = false;
            if (!isset($mapid) || $mapid == "" || (int)$mapid == 0)
            {
                $flg_validate_error = true;
                $flg_validate_text .= JText::_('COM_ZHGOOGLEMAP_MAPBUFMRKS_IMPORT_ERROR_INVALID')." ".JText::_('COM_ZHGOOGLEMAP_MAPMARKER_DETAIL_MAPID_LABEL')."<br />";
            }
                
            if ($flg_validate_error)
            {
                $app->enqueueMessage(JText::_('COM_ZHGOOGLEMAP_MAPBUFMRKS_IMPORT_ERROR_REQUIRED'), 'error');
                $app->enqueueMessage("<br />".$flg_validate_text, 'error');
                return false;
            }
            else
            {      
            
                $return = $model->marker_load($mapid, null);
                
                echo $return;
            }

            return true;
            
            // Redirect to the list screen.
            //$this->setRedirect(JRoute::_('index.php?option=com_zhgooglemap&view=mapbufmrks', false));

    }    
        
    public function marker_delete_all()
    {
            // Check for request forgeries.
            JSession::checkToken() or jexit(JText::_('JINVALID_TOKEN'));
                
            // Get the model
            $model = $this->getModel();

            $return = $model->marker_delete_all();
            
            if (!$return)
            {
                    JError::raiseWarning(500, $model->getError());
            }
            else
            {
                    $this->setMessage(JText::_('COM_ZHGOOGLEMAP_MAPBUFMRKS_MARKERS_DELETED'));
            }
            
            // Redirect to the list screen.
            $this->setRedirect(JRoute::_('index.php?option=com_zhgooglemap&view=mapbufmrks', false));

    }        

    public function marker_delete_processed()
    {
            // Check for request forgeries.
            JSession::checkToken() or jexit(JText::_('JINVALID_TOKEN'));

            // Get the model
            $model = $this->getModel();

            $return = $model->marker_delete_processed();
            
            if (!$return)
            {
                    JError::raiseWarning(500, $model->getError());
            }
            else
            {
                    $this->setMessage(JText::_('COM_ZHGOOGLEMAP_MAPBUFMRKS_PROCESSED_MARKERS_DELETED'));
            }


            // Redirect to the list screen.
            $this->setRedirect(JRoute::_('index.php?option=com_zhgooglemap&view=mapbufmrks', false));

    }    

    public function marker_log()
    {
            // Check for request forgeries.
            JSession::checkToken() or jexit(JText::_('JINVALID_TOKEN'));

            $model = $this->getModel();

            $return = $model->checkLog("csv_file_marker");
            
            if ($return == 0)
            {
                $this->setMessage(JText::_('COM_ZHGOOGLEMAP_MAPBUFMRKS_LOG_NO_DATA_FOUND'));
                
                // Redirect to the list screen.
                $this->setRedirect(JRoute::_('index.php?option=com_zhgooglemap&view=mapbufmrks', false));
            }
            else
            {
                // Redirect to the list screen.
                $this->setRedirect(JRoute::_('index.php?option=com_zhgooglemap&view=mapbufmrklogs', false));
            }

            

    }

    public function marker_delete_log()
    {
            // Check for request forgeries.
            JSession::checkToken() or jexit(JText::_('JINVALID_TOKEN'));

            // Get the model
            $model = $this->getModel();

            $return = $model->deleteLog("csv_file_marker");
            
            if (!$return)
            {
                    JError::raiseWarning(500, $model->getError());
            }
            else
            {
                    $this->setMessage(JText::_('COM_ZHGOOGLEMAP_MAPBUFMRKS_LOG_DELETED'));
            }

            $this->setRedirect(JRoute::_('index.php?option=com_zhgooglemap&view=mapbufmrks', false));
            
    }      
               

    public function file_load()
    {
            // Check for request forgeries.
            JSession::checkToken() or jexit(JText::_('JINVALID_TOKEN'));

            $maxSize = JFilesystemHelper::fileUploadMaxSize();
            $maxSizeBytes = $this->getFileSizeBytes($maxSize.'B');
            
            $model = $this->getModel();
            
            $app = JFactory::getApplication(); 
        $input = $app->input; 
            // get the data from the HTTP POST request
            $data  = $input->get('jform', array(), 'array');
            $files = $input->files->get('jform', array(), 'array');

            // Get the current URI to set in redirects. As we're handling a POST, 
            // this URI comes from the <form action="..."> attribute in the layout file above
            $currentUri = (string)JUri::getInstance();
          
            $icon = $data["icontype"];
            $markergroup = $data["markergroup"];
            $catid = $data["catid"];
            $published = $data["published"];
            $delimiter = $data["delimiter"];
            
            $flg_files = false;
            foreach ($files as $file)
            {
                if ($file['name'] != "")
                {
                    $flg_files = true;
                }
            }
            
            $flg_validate_text = "";
            $flg_validate_error = false;
            
            if (!isset($files) || empty($files) || count($files) == 0 || !$flg_files)
            {
                $flg_validate_error = true;
                $flg_validate_text .= JText::_('COM_ZHGOOGLEMAP_MAPBUFMRKS_UPLOAD_ERROR_INVALID')." ".JText::_('COM_ZHGOOGLEMAP_MAPBUFMRKS_FILE_LABEL')."<br />";
            }
            if (!isset($delimiter) || $delimiter == "")
            {
                $flg_validate_error = true;
                $flg_validate_text .= JText::_('COM_ZHGOOGLEMAP_MAPBUFMRKS_UPLOAD_ERROR_INVALID')." ".JText::_('COM_ZHGOOGLEMAP_MAPBUFMRKS_DELIM_LABEL')."<br />";
            }
                
            if ($flg_validate_error)
            {
                $app->enqueueMessage(JText::_('COM_ZHGOOGLEMAP_MAPBUFMRKS_UPLOAD_ERROR_REQUIRED'), 'error');
                $app->enqueueMessage("<br />".$flg_validate_text, 'error');
                return false;
            }
            else
            {
                // Clean up filename to get rid of strange characters like spaces etc
                foreach ($files as $file)
                {
                    $filename = JFile::makeSafe($file['name']);

                    // Set up the source and destination of the file
                    $src = $file['tmp_name'];
                    $dest = JPATH_COMPONENT . DS . "uploads" . DS . $filename;
                    
                    $size = (int)$file['size'];
                    //echo "upload:".$src ."<br />";
                    //echo "to:".$dest ."<br />";
                    // First check if the file has the right extension, we need jpg only
                    if ($size < $maxSizeBytes) 
                    {
                        // TODO: Add security checks
 
                        if (JFile::upload($src, $dest))
                        {
                            $return = $model->file_load($icon, $markergroup, $catid, $published, $delimiter, $filename);
                            
                            JFile::delete($dest);

                            echo $return;  
                        } 
                        else
                        {
                            $app->enqueueMessage(JText::_('COM_ZHGOOGLEMAP_MAPBUFMRKS_UPLOAD_ERROR_NOTUPLOADED'), 'error');
                            return false;
                        }
                    }
                    else
                    {
                        $app->enqueueMessage(JText::_('COM_ZHGOOGLEMAP_MAPBUFMRKS_UPLOAD_ERROR_FILE_SIZE').": ".$this->getFileSizeText($size)." (".$maxSize.")", 'error');
                        return false;
                    }

                }
                
                return true;
            }

            
            //$this->setMessage($return);

            // Redirect to the list screen.
            //$this->setRedirect(JRoute::_('index.php?option=com_zhgooglemap&view=mapbufmrks', false));
            
    }      
    
                 
            
    public function back()
    {
            // Check for request forgeries.
            JSession::checkToken() or jexit(JText::_('JINVALID_TOKEN'));

            // Redirect to the list screen.
            $this->setRedirect(JRoute::_('index.php?option=com_zhgooglemap&view=utils', false));

    }
}