I'm using a script to programmatically create attribute options - the code is as follows:
echo "Creating option ....\n";
$attr_model = Mage::getModel('catalog/resource_eav_attribute');
$attr = $attr_model->loadByCode('catalog_product', $key);
$attr_id = $attr->getAttributeId();
if (!$attr_id){
echo "Cannot find Attribute $code\n";
return 0;
}else{
$option['attribute_id'] = $attr_id;
$option['value'][$value][0] = $value;
$option['value'][$value][1] = $value;
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
$setup->addAttributeOption($option);
}
And for most values it's working absolutely fine.
However certain values are not being created - it's not throwing any errors, but it doesn't create the options.
One example is 55 mg/kg, which makes me suspect that it's an escaping issue.
Are there any particular escaping rules that I need to follow for this?
Having dug into this further, I've found out that if the array key you use in the $option['value'] array starts with a number, Magento treats this differently as part of the addAttributeOption() call (casting it as an (int), and then assuming you are editing an existing attribute option)
Ensuring the array key doesn't start with a number (I did this by pre-pending 'opt' onto the key) solves the issue.
Related
Have been trying to get a variable percentage from two helper functions inside .phtml file in magento
basically i have two variables based on two helper functions that on their own output/echo static numbers. Problem is in the below php they are just displaying as the value and not dividing/multiplying. So like i said the helper data & module work is using the data as variables to do/complete the equation within the .phtml file. See code below
get->FunctionA() just equals a round number value (from a collection)
get->FunctionB() just equals a round number value as well (from a collection)
probs not the best way but this just outputs two values from helper data and not dividing.
echo Mage::helper('module/data')->getFunctionA() / Mage::helper('module/data')->getFunctionB();
Also this does not work either, just produces the same result and is probs the best/easiest way
$dataA = Mage::helper('module/data')->getFunctionA();
$dataB = Mage::helper('module/data')->getFunctionB();
$result = ($dataA / $dataB) * 100;
echo $result;
Like i said above the values can be echoed (in either helper function or phtml files but the actual calculation does not wont to work
Any help would be great
Ok sorted the problem out. What was happening was that in the helper file the getFunctionA() & getFunctionB() value was being echoed when i should have been just returning the value then echoing the helper function in the .phtml file. Doh! See below the helper function example that is now working Woohoo!!!
public function getFunctionA()
{
$FunctionA = Mage::getModel('module/collection')->getCollection();
$FunctionA->addFieldToFilter('attribute', 'value_to_filter');
$FunctionA->addFieldToFilter('status','1');
return ''.count($FunctionA) . ''; //this line was the problem cause i was echoing & not returning the value
}
Now the value can be echoed in the phtml & the math equation is confirmed working
$dataA = Mage::helper('module/data')->getFunctionA();
$dataB = Mage::helper('module/core')->getFunctionB();
$result = ($dataA / $dataB) * 100;
echo $result;
this code does the math and now i can rest.
If you open the properties of a file in Windows, there usually is a Details tab. I want to access the information on this tab, but I don't know how.
Is there a module for it? Does someone has a code sniplet?
I tried to work with Win32::File's GetAttributes, but these are not the attributes I was looking for.
use Win32::OLE;
my $objShell = Win32::OLE->new("Shell.Application") or die;
my $objFolder = $objShell->NameSpace($myDir) or die;
my $objFile = $objFolder->ParseName($fileName) or die;
while ( $i <= 34 )
{
my $propertyName = $objFolder->GetDetailsOf($fileName,$i);
my $propertyValue = $objFolder->GetDetailsOf($objFile,$i);
print "$i -- $propertyName -- $propertyValue\n";
$i++;
}
You can instantiate a COM "Shell.Application" object. It exposes a .NameSpace(folder) method that returns a reference to the name space of the indicated folder, which holds the information you need. The retrieved instance holds a Items collection with references to each of the files in the folder, and a .GetDetailsOf(file,property) to retrieve each of the values seen in the details tab and explorer columns.
Sorry i have no idea of perl, so i can not include any working code.
I am creating a dynamic list of placeholders, some of the values held in these place holders are decimal numbers that are supposed to represent money.
What I'm wondering is if there is a way I can format them to display as such?
Something like [[+MoneyField:formatmoney]]
I see http://rtfm.modx.com/revolution/2.x/making-sites-with-modx/customizing-content/input-and-output-filters-(output-modifiers) but I do not see a way to do this here.
You most definitely can, under the header "Creating a Custom Output Modifier" on the link you posted it's described how you can place a snippet name as a output modifier. This snippet will recieve the [[+MoneyField]] value in a variable called $input.
So you'd have to create this custom snippet which could be as simple as
return '$'.number_format($input);
Another version of doing this is calling the snippet directly instead of as an output modifier like so:
[[your_custom_money_format_snippet ? input=`[[+MoneyField]]`]]
I'm not sure if theres any difference between the two in this case. Obviously you can pass any value into the number format snippet when calling it as a snippet instead of an output modifier. And i'm sure theres a microsecond of performance difference in the two but i'm afraid i don't know which one would win. ;)
Update:
Actually found the exact example you want to implement on this link;
http://rtfm.modx.com/revolution/2.x/making-sites-with-modx/customizing-content/input-and-output-filters-%28output-modifiers%29/custom-output-filter-examples
Snippet:
<?php
$number = floatval($input);
$optionsXpld = #explode('&', $options);
$optionsArray = array();
foreach ($optionsXpld as $xpld) {
$params = #explode('=', $xpld);
array_walk($params, create_function('&$v', '$v = trim($v);'));
if (isset($params[1])) {
$optionsArray[$params[0]] = $params[1];
} else {
$optionsArray[$params[0]] = '';
}
}
$decimals = isset($optionsArray['decimals']) ? $optionsArray['decimals'] : null;
$dec_point = isset($optionsArray['dec_point']) ? $optionsArray['dec_point'] : null;
$thousands_sep = isset($optionsArray['thousands_sep']) ? $optionsArray['thousands_sep'] : null;
$output = number_format($number, $decimals, $dec_point, $thousands_sep);
return $output;
Used as output modifier:
[[+price:numberformat=`&decimals=2&dec_point=,&thousands_sep=.`]]
I have the following snippet code hooked up to a FormIt email form:
$tv = "taken" . (int)$hook->getValue('datetime');
$docID = $modx->resource->get('id'); //get the page id
$page = $modx->getObject('modResource', $docID);
$current = (int)$page->getTVValue($tv);
if (!$page->setTVValue($tv, $current + 1)) {
$modx->log(xPDO::LOG_LEVEL_ERROR, 'There was a problem saving your TV...');
}
$modx->setPlaceholder('successMessage','<h2 class="success">'.$current.'</h2>');
return true;`
It increments a template variable every time it is run and outputs a success message (although right now I'm using that functionality to output a debug message instead). The problem is, it only increments the TV once after saving the snippet, thereby refreshing the cache. Normally I would call the snippet without cache by appending ! to its name, but that doesn't appear to work for FormIt hooks. How can I get this code to work? Right now I'm running the entire page as uncacheable, but that is obviously suboptimal. Perhaps, there's a way to hook a snippet in an uncached manner? Call a snippet from within a snippet as uncached?
I'm doing something similar - but to count page loads, it looks to me like you are missing the last little bit: $current->save();
<?php
$docID = $modx->resource->get('id');
$tvIdm = 32;
$tvm = $modx->getObject('modTemplateVar',$tvIdm );
$tvm->setValue($docID, $tvm->getValue($docID) + 1 );
$tvm->save();
Try add this before you save $tv object
$tv->_processed = false;
It's derived from modElement's property it extends.
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?)