Codeigniter Config file, load a get function from model - php

I have a custom config file setup for twitter, which has some vars in but the data is saved in the database, so therefore i need to call something like:
$this->wc_settings->get()->row_array('consumer_key')
Here's the config file
$config['tweets'] = array(
'_tokens' => array(
'consumer_key' => $this->wc_settings->get()->row_array('consumer_key'),
//'consumer_secret' => '',
//'access_key' => '',
//'access_secret' => '',
),
This doesn't work but I need it to in order for the config file to work, but need to get the data from the database like shown above.
Help greatly appreciated.

You can dynamically set the config in your controller or model using:
$this->config->load('tweets');
$dynamic_config = $this->wc_settings->get()->row_array('consumer_key');
$this->config->set_item('_tokens['consumer_key']', $dynamic_config);

Related

Getting array from generated php document

I am currently generating config.php file that has an array w user details in it ,but I have a problem with getting the array back out to be used on the page, the config.php looks like this
Array
(
[DBLocation] => localhost
[DBName] => name
[DBUsername] => name
[DBPassword] => 123456
)
How can I use this array later?
Convert your config.php file like this:-
<?php
$arr = Array
(
'DBLocation' => 'localhost',
'DBName' => 'TAK14_Ostermann',
'DBUsername' => 'TAK14_Ostermann',
'DBPassword' => '123456'
);
?>
Now include this file into others with code:-include 'config.php' and use $arr variable.
Note:
Better to do a complete configuration code in the file (config.php) itself and create a database connection object. Now use that object by including the file. It will remove db-connection code redundancy in each page.
If the array you posted is the exact content of the config.php file you are not generating it in the right way.
The posted "array" is what print_r() outputs. As the documentation says:
print_r — Prints human-readable information about a variable.
The key here is "human-readable". The purpose of print_r() is to produce an output that is easy to read and understand by the programmer. It is a debug function, not meant to be used in the production code.
The function you need to generate the content of config.php is var_export(). It produces correct PHP code and it is specifically crafted for this purpose.
Assuming your configuration data is stored in the $config array, the code that generates config.php should be like this:
file_put_contents('config.php', '<?php return '.var_export($config, TRUE).";\n");
The generated config.php file will look like this:
<?php return array (
'DBLocation' => 'localhost',
'DBName' => 'name',
'DBUsername' => 'name',
'DBPassword' => '123456',
);
In order to load the configuration use include:
$config = include 'config.php';

File upload in module configuration panel, Prestashop

I'm trying to create a XML import module that will convert given file to CSV format and then use that CSV to import categories and products.
I have a working configuration page made with getContent() it basically calls a method that generates this form via $helper->generateForm(). $helper is a HelperForm() object.
protected function getConfigForm()
{
return array(
'form' => array(
'legend' => array(
'title' => $this->l('Settings'),
'icon' => 'icon-cogs',
),
'input' => array(
array(
'type' => 'file',
'label' => $this->l('XML file'),
'name' => 'XMLIMPORT_XML_FILE',
'desc' => $this->l('Select file you wish to import.'),
'required' => true
),
array(
'col' => 3,
'type' => 'text',
'prefix' => '<i class="icon icon-envelope"></i>',
'desc' => $this->l('Enter a valid email address'),
'name' => 'XMLIMPORT_LINES',
'label' => $this->l('Records per file'),
),
),
'submit' => array(
'title' => $this->l('Save'),
),
),
);
}
I need to get this data to my XML converter. How do I upload a file (around 10-20MB) to Prestashop to be then able to do other stuff with it? How to save it permanently on the server?
I tried doing this:
return array(
'XMLIMPORT_XML_FILE' => Configuration::get('XMLIMPORT_XML_FILE', null),
'XMLIMPORT_LINES' => Configuration::get('XMLIMPORT_LINES', 1000)
);
And after that this:
$form_values = $this->getConfigFormValues(); // returned array from above
foreach (array_keys($form_values) as $key)
Configuration::updateValue($key, Tools::getValue($key));
And later using my own class for XML conversion like this, hoping that it will give me file handle.
$xml_converter = new XMLToCSVConverter(Configuration::get('XMLIMPORT_XML_FILE'), 'output', 'example_products.php');
Apparently it didn't as nothing happens. The class itself is working fine outside of Prestashop module. The constructor is __construct($xml_file, $csv_filename, $template_file).
I need to pass the file I upload to that constructor. I've been struggling for days now.
#edit: I can see the contents of the file inside the HTTP call when submit is clicked. But how do I pass that file to my class?
As far as I remember 'type' => 'file', doesn't actually save any values in the database. This type is only meant to output a file field in your form.
After submitting, you should then do you custom processing with $_FILES['XMLIMPORT_XML_FILE'] : move to upload/ or whereever you want.
'XMLIMPORT_XML_FILE' => Configuration::get('XMLIMPORT_XML_FILE', null), won't return you anything. After uploading you my wish to save the uploaded file path here, but it won't show up next time in the form unless you build the output yourself.
Module configratuon is meant to save text config values. Handling files is trickier and you have to do them yourself each time.
EDIT:
To intercept the saving process, look up the submit button name and make an if statement:
public function getContent() {
if(Tools::isSubmit('submitButtonName')) {
error_log(print_r($_FILES, 1));
}
}
There's probably a function postProcess which does the same (it looks like you copied the methods from a default module).
prestashop handles the image upload with ImageManager class, this class contains more methods which are useful for handling image upload, resize etc.. so its better refer the default homeslider module for the image upload using a module. This module is handling the image upload process in postProcess method with the help of ImageManager class, this class methods will do the all the processes related to upload.

cakephp upload plugin - define what directory to save files in

I'm using cakephp-upload plugin of jose gonzalez to upload images to our app. By default, they're being saved in a directory like this: webroot/files/user/photo/{user_id} which is fine, except for when we want to display such images using $this->Html->image() which searches for images in the webroot/img directory.
I have already tried to display the images with
echo $this->Html->image('../files/user/photo/' .
$user['User']['photo_dir'] . '/' .
$user['User']['photo']);
which works but I was wondering if there's some way to tell this plugin to save into the img directory? The documentation doesn't mention any of that.
And also, is there any way to tell the $this->Form->input('User.photo', array('type' => 'file')); to accept only image files?
as you can see in this file, path is set like:
public $defaults = array(
'rootDir' => null,
'pathMethod' => 'primaryKey',
'path' => '{ROOT}webroot{DS}files{DS}{model}{DS}{field}{DS}',
...
you could change it to make:
'path' => '{ROOT}webroot{DS}img{DS}'
and for your second question, you could use accept attribute, like:
$this->Form->input('User.photo',
array(
'type' => 'file',
'options' => array('accept' => 'image/*')
)
);

Yii - how to access the base URL in main config

I am working on a Yii application. I am trying to set some paths in my main config params like this:
// application-level parameters that can be accessed
// using Yii::app()->params['paramName']
'params'=>array(
'paths' => array(
'imageTemp'=> Yii::getPathOfAlias('webroot').'/files/temp-',
'image'=> Yii::getPathOfAlias('webroot').'/files/',
...
),
'urls' => array(
'imageTemp'=> Yii::app()->getBaseUrl().'/files/temp-',
'image'=> Yii::app()->getBaseUrl().'/files/',
...
),
But I am getting this error:
Fatal error: Call to a member function getBaseUrl() on a non-object in ..blahblah../base/CApplication.php on line 553
I think I cannot use Yii::app() in config file since the app is not initialized yet here, or something like this. So, how can I replace Yii::app()->getBaseUrl() in the config file and get the same results?
You're right, you can't use the Yii::app() methods inside the config's return array, but you can use Yii::getPathOfAlias() outside. Something like this might work:
$webroot = Yii::getPathOfAlias('webroot');
return array(
...
'params'=>array(
'paths' => array(
'imageTemp'=> $webroot.'/files/temp-',
'image'=> $webroot.'/files/',
...
),
),
);
Assuming webroot is defined beforehand.
As for baseUrl... I'll come back to you on that one!
[Back...]
If you need a url, it all depends where your images files are being kept, relative to the yii path, or relative to the base of the web root?
If the base of the web root, you could just use:
return array(
...
'urls'=>array(
'paths' => array(
'imageTemp'=> '/files/temp-',
'image'=> '/files/',
...
),
),
);

Custom field in Drupal 7 with multiple file fields

I'm making a custom "video" field that is supposed to accept several files (for different video formats) and a caption. So far the schema is fine, but I can't get it to upload and store the actual files.
My code in hook_field_widget_form looks like this (only pasting relevant bits):
$element['mp4'] = array(
'#type' => 'file',
'#title' => 'MP4 file',
'#delta' => $delta,
);
$element['ogg'] = ... /* similar to the mp4 one */
$element['caption'] = array(
'#type' => 'textfield',
'#title' => 'Caption',
'#delta' => $delta,
);
Also, in my .install file:
function customvideofield_field_schema($field) {
return array(
'columns' => array(
'mp4' => array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
),
'ogg' => ... /* similar to mp4 */
'caption' => array(
'type' => 'varchar',
'length' => 255,
),
)
);
}
And the error I'm getting is when I try to store data. I get the form ok, and the database looks fine (the fields Drupal generates at least), but when it tries to do an INSERT, it fails because the value it tries to get into those integer fields is an empty string.
From what I understand, they have to be integers, right? (fids?) But I'm guessing the files are not being uploaded, even though I do get the right interface for uploading files.
Drupal shows you the INSERT query it tries to do, which is too long to post here, but I can see there that the value for the caption field (which is just a text field), is fine in the query, so it's only a problem with the file fields.
You probably want to use the managed_file field type instead, it handles uploading the file and registering it in the managed_files table for you. Then you would just add a submit function to your widget form and put the following code (from the FAPI page linked to above):
// Load the file via file.fid.
$file = file_load($form_state['values']['mp4']);
// Change status to permanent.
$file->status = FILE_STATUS_PERMANENT;
// Save.
file_save($file);
// Record that the module (in this example, user module) is using the file.
file_usage_add($file, 'customvideofield', 'customvideofield', $file->fid);
Hope that helps
EDIT
The core file module handles the actual submission for this using hook_field_presave(), my best guess is that this code would work:
function customvideofield_field_presave($entity_type, $entity, $field, $instance, $langcode, &$items) {
// Make sure that each file which will be saved with this object has a
// permanent status, so that it will not be removed when temporary files are
// cleaned up.
foreach ($items as $item) {
$file = file_load($item['mp4']);
if (!$file->status) {
$file->status = FILE_STATUS_PERMANENT;
file_save($file);
}
}
}
That assumes the file ID column for your field is the one called mp4.
Remember to clear Drupal's caches when you implement the new hook or it won't be registered.
I haven't tried file uploading in my Drupal modules yet, but can you check does your form tag have the attribute enctype
="multipart/form-data"?
I would expect Drupal ought to include this automatically, but without it the file fields won't work, which seems to be what you're experiencing.
James

Categories