Post by AvanOsch » Mon Jul 09, 2012 5:06 am

As the "config library" has a function to load settings from files in "DIR_CONFIG" ($this->config->load()), I think it would be handy to have a function that saves such files to that directory.

I often use an external file for saving settings, and found the built-in support for this very useful.

I add the following function to a lot of my projects, to take care of saving to files.
It works very much the same as the "editSetting" function, in that it serializes any arrays.
Upon loading the file, those values are automagically unserialized.

Code: Select all

// BOF - Zappo - PDF Print - Save Settings (to File)
	public function saveSetting($filename, $data) {
		$returner = "<?php\r\n".
					"/*\r\n".
					"* OpenCart Shopping Cart\r\n\r\n".
					"***********************************************************\r\n".
					"****      THIS FILE IS AUTOMATICALLY GENERATED!!!      ****\r\n".
					"****     USE ADMIN AREA TO CHANGE YOUR SETTINGS!!!     ****\r\n".
					"***********************************************************\r\n".
					"*/\r\n\r\n";
		foreach ($data as $key => $value) {
			$returner .= "  \$_['" . $key . "'] = ";
			if (is_array($value)) {
				$returner .= "unserialize('" . serialize($value) . "')";
			} else {
				$returner .= (is_numeric($value)) ? $value : "'" . $value . "'";
			}
			$returner .= ";\r\n";
		}
		$returner .= "?>";
		if (substr($filename,-4) != '.php') $filename .= '.php';
		if (@file_put_contents(DIR_CONFIG . $filename, $returner)) {
			return true;
		}
		return false;
	}
// EOF - Zappo - PDF Print - Save Settings (to File)
I'd love to see something like this in the core.

User avatar
Active Member

Posts

Joined
Sat Sep 03, 2011 9:17 pm
Who is online

Users browsing this forum: No registered users and 2 guests