Currently I’m using only 1 model+controller where I perform all kinds of Magento tasks
Here it is (I hope the names and comments are clear enough, I’m happy to provide explanation)
controller:
<?php
class CronController extends Controller
{
//set 'last_added' attribute in magento in the order of last modified
public function actionLastModified()
{
$mage = new MagentoActions;
$mage->setLastModified();
die("ok");
}
//put all products that have a reduced price in the category sale
public function actionSetSale()
{
$mage = new MagentoActions;
$mage->setSale();
die("ok");
}
//loop blog posts, check amount of comments and update table
public function actionUpdateBlogCommentCount()
{
$mage = new MagentoActions;
$mage->updateBlogCommentCount();
die("ok");
}
//clean up redundant Magento files
public function actionCleanUp()
{
$mage = new MagentoActions;
$mage->cleanUpFiles();
$mage->cleanUpDB();
die("ok");
}
//to a reindex
public function actionReindexData()
{
$mage = new MagentoActions;
$mage->reindexData();
die("ok");
}
//subscribe users to mailing
public function actionSyncNewsletterSubscribers()
{
$mage = new MagentoActions;
$mage->syncNewsletterSubscribers();
die("ok");
}
}
Model:
<?php
class MagentoActions
{
function magento_init()
{
$autoloaders = spl_autoload_functions();
$yii = $autoloaders[0];
spl_autoload_unregister($yii);
// Start magentoo stuff
require_once('../../app/Mage.php');
Mage::App('default');
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
}
public function setSale()
{
$this->magento_init();
$sale_cat_id = 7;
foreach (array(1) as $storeId) {
$products = Mage::getResourceModel('reports/product_collection')
->setStoreId($storeId)
->addStoreFilter($storeId)
->addAttributeToFilter('visibility', 4)
->addAttributeToSelect('*');
foreach ($products as $product) {
$orig_product_cats = $product_cats = $product->getCategoryIds();
if ($product->getFinalPrice() < $product->getPrice()) {
$product_cats[] = $sale_cat_id;
$product_cats = array_unique($product_cats);
}
else {
$product_cats = array_diff($product_cats, array($sale_cat_id));
}
if ($orig_product_cats != $product_cats) {
$product->setCategoryIds($product_cats);
$product->save();
}
}
}
}
public function setLastModified()
{
$this->magento_init();
foreach (array(1) as $storeId) {
$products = Mage::getResourceModel('reports/product_collection')
->setStoreId($storeId)
->addStoreFilter($storeId)
->addAttributeToFilter('visibility', 4)
->addAttributeToSort('entity_id', 'desc')
->setPageSize(10000000)
->setCurPage(1)
->addAttributeToSelect('*');
$i = 1;
foreach ($products as $product) {
$nr = str_pad($i, 10, "0", STR_PAD_LEFT);
$product->setLastAdded($nr);
$product->save();
$i++;
}
}
}
//checks the amount of comment
public function updateBlogCommentCount()
{
$sql = "select post_id, count(comment_id) as comments from aw_blog_comment where status=2 group by post_id";
$rows = Yii::app()->db->createCommand($sql)->query()->readAll();
foreach ($rows as $row)
{
$sql = "update aw_blog set comments = " . $row['comments'] . " where post_id=" . $row['post_id'];
Yii::app()->db->createCommand($sql)->query();
}
}
//cleanup temp/cache files
public function cleanUpFiles()
{
$dirs = array(
'../../downloader/pearlib/cache/*',
'../../downloader/pearlib/download/*',
'../../var/cache/',
'../../var/log/',
'../../var/report/',
'../../var/session/',
'../../var/tmp/'
);
foreach ($dirs as $v => $k) {
exec('rm -rf ' . $k);
}
}
//cleanup log/tmp db tables
public function cleanUpDB()
{
$tables = array(
'dataflow_batch_export',
'dataflow_batch_import',
'log_customer',
'log_quote',
'log_summary',
'log_summary_type',
'log_url',
'log_url_info',
'log_visitor',
'log_visitor_info',
'log_visitor_online',
'report_event'
);
foreach ($tables as $v => $k) {
$sql = "TRUNCATE $k";
Yii::app()->db->createCommand($sql)->query();
}
}
public function reindexData($index_name = "all")
{
/* index names:
catalog_product_attribute
catalog_product_price
catalog_url
catalog_product_flat
catalog_category_flat
catalog_category_product
catalogsearch_fulltext
tag_summary
cataloginventory_stock
*/
$this->magento_init();
echo system('php ../../shell/indexer.php --reindex all');
}
public function syncNewsletterSubscribers()
{
$this->magento_init();
$collection = Mage::getResourceModel('newsletter/subscriber_collection')->showStoreInfo()->showCustomerInfo();
foreach ($collection as $customer) {
$first_name = $customer->customer_firstname;
$last_name = $customer->customer_lastname;
$email = $customer->subscriber_email;
//submit
$fields = array(
'customers_email_address' => $email,
'customers_email_format' => $email,
'customers_firstname' => $first_name,
'customers_lastname' => $last_name,
'entry_street_address' => "unknown",
'entry_suburb' => "-",
'entry_city' => "onbekend",
'entry_state' => "-",
'entry_postcode' => "onbekend",
'entry_country_id' => "NLD",
'customers_telephone' => "onbekend",
'customers_fax' => "onbekend",
'customers_newsletter' => TRUE,
'kcm_title' => "",
);
$subscriber = new InterspireApi;
$subscriber->iemAddSubscriberToList($fields);
}
}
}