Zend appendFIle tweaking - function

How can I include a same .js file multiple times but it will add it to the header only once. The idea is similar to include and include_once function in PHP. I am using the following zend framework function. And idea would be appreciated.
appendFile($src, $type = 'text/javascript', $attrs = array())

There are two possibilities:
Extend Zend_View_Helper_HeadScript and loop through the container ($view->headScript()->getContainer()) to check whether the file already exists
Before rendering the headScript(), retrieve the container and filter out duplicates.

Related

Error when rendering .tpl file from SQL query

I'm trying to get a result from a MySQL database and display selected values into a .tpl template file.
This is what I tried so far:
{php}
$clienthosting = $this->get_template_vars(service);//Here is where the exception is thrown
$dbid = $clienthosting['id'];
$query = mysql_query("SELECT dedicatedip FROM tblhosting WHERE id = $dbid");
$result = mysql_fetch_array($query);
$dedicatedip = $result["dedicatedip"];
$this->assign("dedicatedip", $dedicatedip);
{/php}
But it generated the following error:
Something went wrong and we couldn't process your request.
What am I doing wrong?
Thanks.
WHMCS recommends not use {php} inside tpl files, you can use hooks instead to add variables and use them in the TPL file.
But you can enable it in the settings: Setup > General Settings > Security > Allow Smarty PHP Tags.
Also, if you're running PHP 7 the mysql extension is removed, you can't use functions like mysql_query and mysql_fetch_array.
You should use Capsule and Eloquent as recommended at Interacting with the Database page

Create a folder if it doesn't exist

Create a folder if it doesn't exist in Magento, I don't want to use mkdir(). Is there any way to do this ? May be core_file_uploader can work out, but its throwing error.
Magento has Varien_Io_File class for quite many filesystem works.
So instead of using PHP's vanilla mkdir() function, you can use the Magento's way:
$io = new Varien_Io_File();
$io->mkdir(your_path_to_dir);
Or you can also check if the directory does not exist right before creating it
$io = new Varien_Io_File();
if (!$io->fileExists(your_path_to_dir, false)) {
$io->mkdir(your_path_to_dir);
}

get sql query from collection model

I know that we can get sql query from magento model collection using this,
getSelect();
But I can get query from only the model collections, not worked in others or May be I dont know how to use it.
Here I want to know what query is running behind this,
$productModel = Mage::getModel('catalog/product')->getCollection();
$attr = $productModel->getResource()->getAttribute("color");
if ($attr->usesSource()) {
echo $color_label = $attr->getSource()->getOptionText("28");
}
If I use this,
echo $productModel->getSelect(); exit;
I'm just get the one part the query only, like,
SELECT `e`.* FROM `catalog_product_entity` AS `e`
Update:
This is my full code,
<?php
require_once('app/Mage.php'); //Path to Magento
umask(0);
Mage::app();
$productModel = Mage::getModel('catalog/product')->getCollection();
$attr = $productModel->getResource()->getAttribute("color");
if ($attr->usesSource()) {
$color_label = $attr->getSource()->getOptionText("28");
}
$productModel->printlogquery(true);exit;
Please help me guys,
Your order condition is not visible in the query. The reason your order isn't showing is because the orders are added to the query during the load() method.
See Varien_Data_Collection_Db::load()
Try calling load(true) to see the complete SQL containing the order by clause.
$productModel->load(true);
$productModel->printLogQuery(true);
Hope it helps.
If you want to see what is the exact query, then you can get this by using:
$productModel->printlogquery(true);exit;
Use this code after you have loaded your model's object and applied all conditions.
I hope this will help you.
Magento collecting data with lot of internal queries - models and lot of checks, and may be more than 1 table. So it is not possible to get the query like what I'm looking for.

How to execute large sql query in magento?

I want to store huge content in db and my sample text is 16129 characters in length when i tried to execute this query it is showing "error:The requested URL could not be retrieved" in firefox and "no-data received" in chrome.
Moreover I use LONGTEXT as datatype for text in DB.
I also tried to execute the query directly in phpmyadmin it is working correctly.
The code is shown below.
public function _getConnection($type = 'core_write') {
return Mage::getSingleton('core/resource')->getConnection($type);
}
public function testdbAction(){
$db = $this->_getConnection();
$current_time=now();
$text="The European languages are members of the same family...... ...Europe uses the same vocabulary. The ";//text is 16129 characters in length
$sql = "INSERT into test(`usercontent_id`,`app_id`,`module_id`,`customer_id`,`content`,`created_time`,`updated_time`,`item_id`,`index_id`,`position_id`) VALUES (NULL, 15, 9,2,'" .$text. "','" . $current_time . "','" . $current_time . "',1003,5,4)";
$db->query($sql);
}
How do i handle this? any suggestions or help..
Try using $db->exec($sql) instead of $db->query($sql)
For manipulating with database structure and data Magento has a dedicated place. It is called install / upgrade scripts. It was made to keep track of modules version for easy updates. So you should use a script to add new data.
Here's an example.
config.xml of your module:
<modules>
<My_Module>
<version>0.0.1</version>
</My_Module>
</modules>
<global>
<resources>
<my_module_setup>
<setup>
<module>My_Module</module>
<class>Mage_Core_Model_Resource_Setup</class>
</setup>
</my_module_setup>
</resources>
</global>
Now, you need to create following file:
My_Module/sql/my_module_setup/install-0.0.1.php
Now, depending on your Magento version, file would need to have a different name. If you're using Magento CE 1.4 or lower (EE 1.8) then you should name it mysql4-install-0.0.1.php.
This script will be launched at next website request. Class Mage_Core_Model_Resource_Setup will execute code inside install-0.0.1.php. From within the install script you will have access to the Setup class by using $this object;
So now, you can write in script following code:
$this->startSetup();
$text = <<<TEXT
YOUR LONG TEXT GOES HERE
TEXT;
$this->getConnection()
->insert(
$this->getTable('test'),
array(
'usercontent_id' => null,
'app_id' => 15,
'content' => $text,
//other fields in the same fashion
)
);
$this->endSetup();
And that's it. It's a clean and appropriate way of adding custom data to the database in Magento.
If you want to save a user input on a regular basis using forms, then I recommend creating w model, resource model and collection, define entities in config.xml. For more information please refer to Alan Storm's articles, like this one.
I hope that I understood your question correctly.

Database input sanitation

Sorry if this question has somewhat been answered, but everywhere I look answers seems to change..
I'm really not 100% sure of how to deal with data into and from a db.
For instance: A UTF8 mysql database and a form that takes user input.
When dealing with the post - should I htmlspecialchars('data',ENT_QUOTES) to the data before it's saved to the database or do I just save it raw.. the data will most likely contain umlouts and the like of special chars. so if no sanitization is done I assume < and > and all types of quotes would be stored exactly as they are.
Also do I need to addslashes or mysql_real_escape_string before saving.
OR should all this stuff be done on the output and just use filtering for input, I use zend so I have filters added to most elements.
Not sure its relevant magic_quotes but are off.
EDIT 2:
I believe my queries use prepared statements ->where('fqha.form_question_has_answer_form_id = ?', $result[$input->formpage]['form_id'])
So does this mean I can ignore any further manipulation to data and just save what the post vars contain. Only worrying about sanitizing for output to the page?
EDIT:
I'm using doctrine 1.2 and from what I can tell this is pdo.. but regarding this and the above question any advice would be greatly appreciated
protected function _initDoctrine()
{
require_once 'Doctrine/Doctrine.php';
require_once 'Doctrine/Overloadable.php';
require_once 'Doctrine/Connection/Profiler.php';
$this->getApplication()
->getAutoloader()
->pushAutoloader(array('Doctrine', 'autoload'), 'Doctrine');
$manager = Doctrine_Manager::getInstance();
$manager->setAttribute(
Doctrine::ATTR_MODEL_LOADING,
Doctrine::MODEL_LOADING_CONSERVATIVE
);
$cacheDriver = new Doctrine_Cache_Apc();
$config = $this->getOption('doctrine');
$conn = Doctrine_Manager::connection('mysql://user:pass#localhost/db_name', 'doctrine');
$conn->setAttribute(Doctrine_Core::ATTR_QUERY_CACHE, $cacheDriver);
$conn->setCharset('utf8');
$conn->exec('SET NAMES utf8');
$profiler = new Imind_Profiler_Doctrine_Firebug();
$conn->setListener($profiler);
return $conn;
}