Is it possible to put variables inside config files? - smarty

I use smarty to allow different languages on my site, which works OK so far. I store the texts in config files in different sections.
But then there are sentences like this:
"You have 6 new mails!", which would be in german "Sie haben 6 neue Mails!"
Now there's text before the number and behind the number, which is loaded from the database. And I would like to put it into the config file and just load the number on its own.
so I have this in my "text.conf"
[en]
mail_count = "You have $NUMBER new mails!"
[de]
mail_count = "Sie haben $NUMBER neue Mails!"
and this in my "show_text.php"
$smarty->assign('NUMBER', 6);
Is something like this possible? Maybe with Smarty 3.0?
Thanks in advance,
BH

You can use the sprintf sintax. This example comes from a pager-like thing:
results = "Results %s to %s of %s total"
{#results#|sprintf:$start:$end:$total}

I just tried this and it works, but it's rather ugly...
create a file "number.tpl" which contains
{$NUMBER}
in your conf file go like this
mail_count = "You have {include
file='number.tpl'} new mails!"
I guess it's because the smarty variables only work in tpl files.

When reading the config file, you need to open it using
$cfg = $smarty->fetch('path/to/file');
After that you have the whole files content ind the $cfg variable, with {$NUMBER} replaced.

I don't have Smarty to test this right now but it should work if you properly declare the variable in your config entry, like:
ail_count = "You have {$NUMBER} new mails!"

Related

Cannot change CSV delimiter to tab

I'm going to export all of my data in mySQL using PHPExcel with CSV Format. Here is my code:
function exportHartatoCSV() {
$memberid = $this->input->post('memberid');
$tahun = $this->input->post('tahunpajak');
$this->db->where('taxYear', $tahun);
$this->db->where('memberID', $memberid);
$query = $this->db->get('list_harta');
$this->load->library("excel");
$objPHPExcel = new PHPExcel();
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1', 'Tahun Pajak')
->setCellValue('B1', 'Pembetulan')
->setCellValue('C1', 'Kode Harta')
->setCellValue('D1', 'Jenis Harta')
->setCellValue('E1', 'Tahun Perolehan')
->setCellValue('F1', 'Harga Perolehan')
->setCellValue('G1', 'Keterangan');
$row = 2;
$workbookName = $this->MemberModel->getNPWP($memberid);
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'.$workbookName.'.csv"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'CSV');
$objWriter->setDelimiter("\t");
$objWriter->setEnclosure('');
$total = 0;
foreach ($query->result() as $key) {
$objPHPExcel->getActiveSheet()->setCellValue('A'.$row, $key->taxYear);
$objPHPExcel->getActiveSheet()->setCellValue('B'.$row, $key->pembetulan);
$objPHPExcel->getActiveSheet()->setCellValue('C'.$row, $key->hartaID);
$objPHPExcel->getActiveSheet()->setCellValue('D'.$row, $key->hartaName);
$objPHPExcel->getActiveSheet()->setCellValue('E'.$row, $key->hartaYear);
$objPHPExcel->getActiveSheet()->setCellValue('F'.$row, $key->hartaPrice);
$objPHPExcel->getActiveSheet()->setCellValue('G'.$row, $key->hartaDesc);
$total += $key->hartaPrice;
$row++;
}
$objPHPExcel->getActiveSheet()->setCellValue('E'.$row, "Total Harta:");
$objPHPExcel->getActiveSheet()->setCellValue('F'.$row, $total);
$objWriter->save('php://output');
}
I want to change the delimiter from , to tab (like writing data to the next column. But it doesn't work. The strings are stick to each other. The output is like this:
Output
But I want the output is like this:
Expected Output
What I really want is a normal-looking excel table, but with .csv format.
So you need tab-delimited text (csv) file or you just want "normal"-looking excel table?
You must remember that there is a system list delimiter that is used in excel by default. In windows you can set that delimiter in your Region and Language, in Additional settings. It's called as List separator there. In some countries default value is ";", in others ",".
If you don't want to change your default delimiter you have an option to choose delimiter in Excel when you open file.
Update
You can use both options, but it looks like it's easier to set the delimiter in PHPExcel export settings to that delimiter that is set by default in your client's OS, than explain them to set custom delimiter in excel when opening file.
$objWriter->setDelimiter(";");
or
$objWriter->setDelimiter(",");
Update 2
If you have clients from different countries and/or they use different operating systems (mac/linux, windows), so they may have different system separators, then you'll need to save 2 versions of each file (with both , and ; delimiters). Or you'll need to explain them how to work with it.
I'm putting this system setting screenshots just for example:
Mac OS
Windows
As #teeyo mentioned, you might need to make sure you import it using the delimiter in Excel (or other spreadsheet editor). It might already be correct in the data generated by CI:
To verify the data is correct in the file itself, you can open it in eg. Notepad++ and use the Show All Characters feature:

Print smarty var in link file param

One simple thing:
I want to combine the {s name="*"} and {link file="*"} blocks.
src="{link file='{s name='sFooterPaymentsIcon'}{/s}'}"
The problems should be the
'
signs.
How can I do that?
You can try assign a new variable and pass that on file parameter, like:
{assign var="my_file" value="{s name='sFooterPaymentsIcon'}{/s}"}
and then
src="{link file="$my_file"}"
You can do it this way:
//Assign snippet value to variable $snippetLink, in case variable is empty - assign LinkInCaseSnippetEmpty
{assign var='snippetLink' value='LinkInCaseSnippetEmpty'|snippet:'TheNameOfSnippet':"Namespace/If/Need"}
//assign source from variable $snippetLink
src="{link file=$my_file}"
In one line:
src="{link file='LinkInCaseSnippetEmpty'|snippet:'TheNameOfSnippet':'Namespace/If/Need'}"
{s} is for for text-snippets and should not be used for configuration-variables. If you need to make an include configurable, you should create a plugin for that.
The plugin should have a frontend-subscriber and make the file-include configurable via backend configuration-form. In the subscriber you can pass the configuration-value for the file-include to the frontend-view.

How to put command include ("/path/public_html/file.php"); inside a variable?

I have something like
$example['description']="print this text: *** " ;
and instead of * I like to include a file /home/path/public_html/list.php where is stored the html text (php formatted).
How to implement the function
include ("/home/path/public_html/list.php");
inside the variable? Thank you for help.
One way would be:
ob_start();
include "/home/path/public_html/list.php";
$text = ob_get_clean();
$example['description'] = "print this text: $text";

Read image IPTC data

I'm having some trouble with reading out the IPTC data of some images, the reason why I want to do this, is because my client has all the keywords already in the IPTC data and doesn't want to re-enter them on the site.
So I created this simple script to read them out:
$size = getimagesize($image, $info);
if(isset($info['APP13'])) {
$iptc = iptcparse($info['APP13']);
print '<pre>';
var_dump($iptc['2#025']);
print '</pre>';
}
This works perfectly in most cases, but it's having trouble with some images.
Notice: Undefined index: 2#025
While I can clearly see the keywords in photoshop.
Are there any decent small libraries that could read the keywords in every image? Or am I doing something wrong here?
I've seen a lot of weird IPTC problems. Could be that you have 2 APP13 segments. I noticed that, for some reasons, some JPEGs have multiple IPTC blocks. It's possibly the problem with using several photo-editing programs or some manual file manipulation.
Could be that PHP is trying to read the empty APP13 or even embedded "thumbnail metadata".
Could be also problem with segments lenght - APP13 or 8BIM have lenght marker bytes that might have wrong values.
Try HEX editor and check the file "manually".
I have found that IPTC is almost always embedded as xml using the XMP format, and is often not in the APP13 slot. You can sometimes get the IPTC info by using iptcparse($info['APP1']), but the most reliable way to get it without a third party library is to simply search through the image file from the relevant xml string (I got this from another answer, but I haven't been able to find it, otherwise I would link!):
The xml for the keywords always has the form "<dc:subject>...<rdf:Seq><rdf:li>Keyword 1</rdf:li><rdf:li>Keyword 2</rdf:li>...<rdf:li>Keyword N</rdf:li></rdf:Seq>...</dc:subject>"
So you can just get the file as a string using file_get_contents(get_attached_file($attachment_id)), use strpos() to find each opening (<rdf:li>) and closing (</rdf:li>) XML tag, and grab the keyword between them using substr().
The following snippet works for all jpegs I have tested it on. It will fill the array $keys with IPTC tags taken from an image on wordpress with id $attachment_id:
$content = file_get_contents(get_attached_file($attachment_id));
// Look for xmp data: xml tag "dc:subject" is where keywords are stored
$xmp_data_start = strpos($content, '<dc:subject>') + 12;
// Only proceed if able to find dc:subject tag
if ($xmp_data_start != FALSE) {
$xmp_data_end = strpos($content, '</dc:subject>');
$xmp_data_length = $xmp_data_end - $xmp_data_start;
$xmp_data = substr($content, $xmp_data_start, $xmp_data_length);
// Look for tag "rdf:Seq" where individual keywords are listed
$key_data_start = strpos($xmp_data, '<rdf:Seq>') + 9;
// Only proceed if able to find rdf:Seq tag
if ($key_data_start != FALSE) {
$key_data_end = strpos($xmp_data, '</rdf:Seq>');
$key_data_length = $key_data_end - $key_data_start;
$key_data = substr($xmp_data, $key_data_start, $key_data_length);
// $ctr will track position of each <rdf:li> tag, starting with first
$ctr = strpos($key_data, '<rdf:li>');
// Initialize empty array to store keywords
$keys = Array();
// While loop stores each keyword and searches for next xml keyword tag
while($ctr != FALSE && $ctr < $key_data_length) {
// Skip past the tag to get the keyword itself
$key_begin = $ctr + 8;
// Keyword ends where closing tag begins
$key_end = strpos($key_data, '</rdf:li>', $key_begin);
// Make sure keyword has a closing tag
if ($key_end == FALSE) break;
// Make sure keyword is not too long (not sure what WP can handle)
$key_length = $key_end - $key_begin;
$key_length = (100 < $key_length ? 100 : $key_length);
// Add keyword to keyword array
array_push($keys, substr($key_data, $key_begin, $key_length));
// Find next keyword open tag
$ctr = strpos($key_data, '<rdf:li>', $key_end);
}
}
}
I have this implemented in a plugin to put IPTC keywords into WP's "Description" field, which you can find here.
ExifTool is very robust if you can shell out to that (from PHP it looks like?)

How do I get the suffix (in code) that is being used for urls?

Magento can add a suffix that is defined by the user to append onto urls. I want to get that suffix from my code. Does anyone know an easy way to do this?
If it's stored in the configuration area, then you access it just as you would any other configuration value, by using Mage::getStoreConfig($config_path) where $config_path is defined in the system.xml of the module that defines it.
If you're not sure of the $config_path, then I usually cheat and inspect the textbox/dropdown in the configuration section, take a look at the id, e.g. dev_log_file, and translate it to dev/log/file. You'll need to use some intelligence when there are multiple _ though :)
Nick's answer is good but the actual answer to this question is:
$suffix = Mage::helper('catalog/category')->getCategoryUrlSuffix();
If I am not mistaken, here is the code ( because I don't understand what you want with URL )
<?php
$currentUrl = $this->helper('core/url')->getCurrentUrl();
$url_parts = split('[/.-]', $currentUrl); // escape characters should change based your url
echo $url_parts[0]; //check here
?>
complete product url:
$productId = ***;
$productUrl = Mage::getBaseUrl().Mage::getResourceSingleton('catalog/product')->getAttributeRawValue($productId, 'url_key', Mage::app()->getStore()).Mage::helper('catalog/product')->getProductUrlSuffix();

Resources