Welcome, Guest
Username: Password: Remember me
  • Page:
  • 1

TOPIC:

[SOLVED] Mod of jmodel.item.php upload class 06 Nov 2012 18:52 #5020

UPDATE: Fixed. See code changes below. This only applies if you are using a FILE data type for a DB table column AND NOT using the Cook uploader. For my example, added code is generating the file instead of it being uploaded.

Hello,

I have a file field in one of my tables. However, I am not using the uploader. My application is creating the file. It works just fine, but is generating this in the error log. Any suggestions on modifying the below function?


[06-Nov-2012 19:41:27 UTC] PHP Warning:  Invalid argument supplied for foreach() in components/com_skm/classes/jmodel.item.php on line 131

Line 131 in the error below is the line that contains this:
//Process a conversion to get the right datas
		foreach($files as $key => $params)
public function _upload($fieldName, $extensions = null, $options = array(), $dir = null)
	{
		//Send the id for eventual name or path parsing in upload
		$options['id'] = $this->getId();

		cimport('classes.upload');
		$config	= JComponentHelper::getParams( 'com_skm' );

		if (!$dir)
			$dir = $config->get('upload_dir_' . $this->view_list . '_' . $fieldName, '[COM_SITE]' .DS. 'files' .DS. $this->view_list . '_' . $fieldName);

		$jinput = JFactory::getApplication()->input;

		//Get the submited files if exists
		$fileInput = new JInput($_FILES);
		$files = $fileInput->get('jform', null, 'array');

		$uploadFile = array();



		//Process a conversion to get the right datas
		foreach($files as $key => $params)
			$uploadFile[$key] = $params[$fieldName];



		$post = $jinput->get('jform', null, 'array');

		// Remove parameter
		$removeVar = "__" . $fieldName . "_remove";
		$remove	= (isset($post[$removeVar])?$post[$removeVar]:null);

		// Previous value parameter
		$previousVar = "__$fieldName";
		$previous = (isset($post[$previousVar])?$post[$previousVar]:null);

		// Upload file name
		$upload = (isset($uploadFile['name'])?$uploadFile['name']:null);

		// New value
		$fileName = null;

		//Check method
		$method = '';
		$changed = false;
		if (!empty($upload))
		{
			$method = 'upload';
			$changed = ($upload != $previous);
		}

		//Check if needed to delete files
		if (in_array($remove, array('remove', 'delete', 'thumbs', 'trash')))
		{
			$fileName = "";		//Clear DB link (remove)
			$changed = true;

			//Process physical removing of the files (All, only thumbs, Move to trash)
			if (in_array($remove, array('delete', 'thumbs', 'trash')))
			{
				$f = (preg_match("/\[.+\]/", $previous)?"":$dir.DS) . $previous;
				if (!SkmFile::deleteFile($f, $remove)){
					JError::raiseWarning( 4101, JText::_("SKM_TASK_RESULT_IMPOSSIBLE_TO_DELETE") );
				}
			}
		}

		switch($method)
		{
			case 'upload':

				// Process Upload
				$uploadClass = new SkmUpload($dir);
				$uploadClass->setAllowed($extensions);

				if (!$result = $uploadClass->uploadFile($uploadFile, $options))
				{
					JError::raiseWarning( 4100, JText::sprintf("SKM_TASK_RESULT_IMPOSSIBLE_TO_UPLOAD_FILE", $uploadFile['name']) );
					return false;
				}

				$fileName = $result->filename;
				$changed = true;

				break;
		}

		if ($changed)
		{
			//Store the image file name with path pattern
			if (!$this->save(array($fieldName => $fileName)))
				return false;
		}
		return true;
	}
--
Jerry Benton

Please Log in or Create an account to join the conversation.

Last edit: by jcbenton.

Re: Modification of jmodel.item.php upload class 06 Nov 2012 18:59 #5022

Oh yeah. I don't even need this function to handle the upload. I just need it to handle deletes.
--
Jerry Benton

Please Log in or Create an account to join the conversation.

Re: Modification of jmodel.item.php upload class 06 Nov 2012 21:04 #5025

This gets rid of the PHP Warnings.


OLD classes/jmodel.item.php - line 131
foreach($files as $key => $params)
$uploadFile[$key] = $params[$fieldName];


NEW classes/jmodel.item.php - line 131
if(!empty($files)){
	foreach($files as $key => $params)
	$uploadFile[$key] = $params[$fieldName];
}


OLD classes/jcontroller.item.php - line 188
$value = implode(",", $value);
$varName .= "[]";

NEW classes/jcontroller.item.php - line 188
if(!empty($value)){
	$value = implode(",", $value);
	$varName .= "[]";
}
--
Jerry Benton
The following user(s) said Thank You: JoomGuy

Please Log in or Create an account to join the conversation.

Last edit: by jcbenton.
  • Page:
  • 1
Time to create page: 0.089 seconds

Although I found this tool some time ago I just started learning experimenting and building this first component on Monday and now on Thursday afternoon it's already online. Although I already had a working version done with another tool this is much easier to build and at the end you have the freedom to own your own component. Once you know how to use Cook you will boost your productivity as the learning curve is really moderate compared to many other tools.
Giori (Forum)

Get Started