I would like to know if there is a way to check if a config item exists.
I have a case where I refer to some config items in the config/custom.php file, and others in a database table.
The concept is to make use of existing config items that exist in config/custom.php, and when they don't exist, I pull them from my database.
$config = Config::get($configtype . '.' . $configname);
if (!$config){
// if config not found, then get it from the database
$configrecord = Newconfigs::where(['name' => $configname])->get()->first();
if (!$configrecord){
$config = false;
} else{
$config = $configrecord->value;
}
}
return ($config);
As you can see, doing it this way will not cater for config values of NULL of FALSE.
I would like to do something like this in my very first line to check if the config "exists" in the file...
If(Config::exists($configtype . '.' . $configname)){ } else{ //get from database }
Is there such a thing?
After searching found a solution. Here is the solution that can help
if (config()->has('some.key')) {
// Get configuration value from config file
} else {
// Get configuration value from database
}
Related
I trying to keep original file name when using System/Models/File, I got following code to extend this model:
namespace System\Models;
class NewFile extends File { public function fromPost($uploadedFile) { if ($uploadedFile === null) { return; }
$this->file_name = $uploadedFile->getClientOriginalName();
$this->file_size = $uploadedFile->getClientSize();
$this->content_type = $uploadedFile->getMimeType();
$this->disk_name = $this->getDiskName();
/*
* getRealPath() can be empty for some environments (IIS)
*/
$realPath = empty(trim($uploadedFile->getRealPath()))
? $uploadedFile->getPath() . DIRECTORY_SEPARATOR . $uploadedFile->getFileName()
: $uploadedFile->getRealPath();
//$this->putFile($realPath, $this->disk_name);
$this->putFile($realPath, $this->file_name);
return $this;
It works with file itself, it keeps original name but problem is link to attached file is still being generated. Broke my mind but cant get this work. Can anyone elaborate how to fix it?
Oh I see it seems its try to use disk_name to generate URL
so you did well for saving an image
//$this->putFile($realPath, $this->disk_name);
$this->putFile($realPath, $this->file_name);
but you just need to replace one line .. just undo your changes and make this one change
$this->file_name = $uploadedFile->getClientOriginalName();
$this->file_size = $uploadedFile->getClientSize();
$this->content_type = $uploadedFile->getMimeType();
// $this->disk_name = $this->getDiskName();
$this->disk_name = $this->file_name;
// use same file_name for disk ^ HERE
Link logic ( for referance only ) vendor\october\rain\src\Database\Attach\File.php and modules\system\models\File.php
/**
* Returns the public address to access the file.
*/
public function getPath()
{
return $this->getPublicPath() . $this->getPartitionDirectory() . $this->disk_name;
}
/**
* Define the public address for the storage path.
*/
public function getPublicPath()
{
$uploadsPath = Config::get('cms.storage.uploads.path', '/storage/app/uploads');
if ($this->isPublic()) {
$uploadsPath .= '/public';
}
else {
$uploadsPath .= '/protected';
}
return Url::asset($uploadsPath) . '/';
}
Just make disk_name also same as file_name so when file saved on disk it will use original name and when the link is generated it also use disk_name which is original file_name
now your link and file name are synced and will be same always.
if any doubt please comment.
Is it possible to clear all cell comments in PHPExcel before adding a new one? I have working code that adds a comment but I would like to clear all existing comments in a particular cell first:
$pexr = PHPExcel_IOFactory::createReader('Excel2007');
try {
$pex = $pexr->load($fn);
} catch (Exception $e) {
//...
return;
}
}
// ...
sheet = $pex->getSheetByName($curMed);
...
$sheet->setCellValue($col . $row, $r[7]);
$sheet->getStyle($col . $row)->getNumberFormat()->setFormatCode('#,##0');
$sheet->getComment($col . $row)->getText()->createText("My lovely comment\r\n");
Unfortunately I could not find anything like "clear" in the documentation.
Comments are stored as an array, indexed by cell address in the worksheet object; and it provides methods to get and set the entire array; so it's perfectly possible to retrieve the array, unset comments for the cell that you want to clear, then put the array back again overwriting the original:
$comments = $sheet->getComments();
if (isset($comments[$col . $row])) {
unset($comments[$col . $row]);
$sheet->setComments($comments);
}
In magento as we use the REST url to access the data,as http://localhost/magemto/api/rest/products it returns in XML format.
But as my team requirement, I should send the data in JSON format to access AJAX calls easily.. I have used REST client to include a header as 'Content-Type:appilcation/json'.. Then it returns in JSON format.. But I want it as defaultly by the magento API..
Hey, I do have a solution for this, I would like to share with you.
First go to your magento root folder then go to following path
\app\code\core\Mage\Api2\Model\Request.php
Go to the method getAccepTypes() and change with this code below it will fulfill your requirement.
public function getAcceptTypes()
{
$qualityToTypes = array();
$orderedTypes = array();
foreach (preg_split('/,\s*/', $this->getHeader('Accept')) as $definition) {
$typeWithQ = explode(';', $definition);
$mimeType = trim(array_shift($typeWithQ));
// check MIME type validity
if (!preg_match('~^([0-9a-z*+\-]+)(?:/([0-9a-z*+\-\.]+))?$~i', $mimeType)) {
continue;
}
$quality = '1.0'; // default value for quality
if ($typeWithQ) {
$qAndValue = explode('=', $typeWithQ[0]);
if (2 == count($qAndValue)) {
$quality = $qAndValue[1];
}
}
$qualityToTypes[$quality][$mimeType] = true;
}
krsort($qualityToTypes);
foreach ($qualityToTypes as $typeList) {
$orderedTypes += $typeList;
}
unset($orderedTypes);
$orderedTypes=Array
("application/json" => 1);
return array_keys($orderedTypes);
}
Hope this help you.
I've created a J2.5 component with some config fields using config.xml in the admin folder of the component.
How can I set parameters in the config programatically?
I've tried the code bellow, but it obviously doesn't save the result to the DB:
$params = & JComponentHelper::getParams('com_mycomponent');
$params->set('myvar', $the_value);
Could anyone please show some examples of how to achieve this?
The safest way to do this would be to include com_config/models/component.php and use it to validate and save the params. However, if you can somehow validate the data params yourself I would stick with the following (much more simple solution):
// Get the params and set the new values
$params = JComponentHelper::getParams('com_mycomponent');
$params->set('myvar', $the_value);
// Get a new database query instance
$db = JFactory::getDBO();
$query = $db->getQuery(true);
// Build the query
$query->update('#__extensions AS a');
$query->set('a.params = ' . $db->quote((string)$params));
$query->where('a.element = "com_mycomponent"');
// Execute the query
$db->setQuery($query);
$db->query();
Notice how I cast the params to a string (when building the query), it will convert the JRegistry object to a JSON formatted string.
If you get any caching problems, you might want to run the following after editing the params:
From a model:
$this->cleanCache('_system');
Or, else where:
$conf = JFactory::getConfig();
$options = array(
'defaultgroup' => '_system',
'cachebase' => $conf->get('cache_path', JPATH_SITE . '/cache')
);
$cache = JCache::getInstance('callback', $options);
$cache->clean();
The solution is here...
http://www.webtechriser.com/tutorials/82-joomla-3-0/86-how-to-save-component-parameters-to-database-programmatically
You can replace in Joomla 2.5+ the
// check for error
if (!$table->check()) {
$this->setError('lastcreatedate: check: ' . $table->getError());
return false;
}
if (!$table->store()) {
$this->setError('lastcreatedate: store: ' . $table->getError());
return false;
}
with
if (!$table->save()) {
$this->setError('Save Error: ' . $table->getError());
return false;
}
So I just started using CodeIgniter... and got stuck in setting config item
I have read that replacing the value of a config item is as easy as this:
$this->config->set_item('item_name', 'item_value');
But what if I want to set a config item that is part of an array of config items... like this one:
$api_config = $this->config->load('api'); //loading the created configuration under config folder
api.php
$facebook['app_id'] = '123213213';
$facebook['api_key'] = '123123WERWERWE123123';
$config['facebook'] = $facebook;
and I want to dynamically replace app_id.
Sure you can do it, but you'll need to manually unwrap/rewrap the config item. For example, given this config file:
$f['a'] = "1";
$f['b'] = "2";
$config['t'] = $f;
And this controller function:
function t()
{
var_dump($this->config->item("t"));
echo "<br>";
$v = $this->config->item("t");
$v['a'] = "3";
$this->config->set_item('t', $v);
var_dump($this->config->item("t"));
}
You will get this output:
array(2) { ["a"]=> string(1) "1" ["b"]=> string(1) "2" }
array(2) { ["a"]=> string(1) "3" ["b"]=> string(1) "2" }
One thing to note: the actual config value in the file hasn't changed: you will need to re-apply your change on each request.
Unfortunately, you can't do that as you have things right now.
A look at the code in question:
// from CI 2, CI 1 has no differences which will effect the current situation
include($file_path);
if ( ! isset($config) OR ! is_array($config))
{
if ($fail_gracefully === TRUE)
{
return FALSE;
}
show_error('Your '.$file_path.' file does not appear to contain a valid configuration array.');
}
if ($use_sections === TRUE)
{
if (isset($this->config[$file]))
{
$this->config[$file] = array_merge($this->config[$file], $config);
}
else
{
$this->config[$file] = $config;
}
}
else
{
$this->config = array_merge($this->config, $config);
}
As you can see, the only value which is picked up from the config file is $config. CI pretty much discards everything else. You should not be able to access that value through config for reading or writing.
Your options are that you can have a facebook config file, you can store the facebook array as a value in the $config variable in the api config file, or you can store the value as some special key like 'facebook_app_id' in the same file. You'll have to decide which option is best for your needs, but I would be inclined to store the value as 'facebook_app_id'.
$this->config->load('api', true);//load config first
//begin set new value
$this->config->set_item('app_Ckey',‘your new value’);
$this->config->set_item('app_id',‘your new value’);
$this->load->library('api');
echo $this->config->item('app_Ckey');