public function run()
{
// Set tracking
$track = $this->getArgs('t', 'track');
if ($track !== false) {
if (!strcmp('0', $track)) {
$doTracking = $this->_helper->disable();
} else {
$doTracking = $this->_helper->enable();
}
} else {
$doTracking = $this->_helper->isActive();
}
// Set username
$user = $this->getArgs('u', 'user');
if ($user !== false) {
$this->_helper->setUser($user);
}
if ($this->getArgs('s', 'status')) {
$this->_printHeader();
$pendingList = $this->_io->getPendingList();
if (count($pendingList)) {
printf("Pending Actions list:\r\n");
foreach ($pendingList as $i => $row) {
$actionDescr = sprintf("ID: %d\t - %s (%s on %s)", $i + 1, $row[PugMoRe_Mageploy_Model_Action_Abstract::INDEX_ACTION_DESCR], $row[PugMoRe_Mageploy_Model_Action_Abstract::INDEX_ACTION_USER], strftime("%c", $row[PugMoRe_Mageploy_Model_Action_Abstract::INDEX_ACTION_TIMESTAMP]));
$spacer = str_repeat(" ", max(0, 40 - strlen($actionDescr)));
printf("%s\r\n", $actionDescr);
}
printf("\r\nTotal pending actions: %d\r\n", count($pendingList));
} else {
printf("There aren't any pending actions to execute.\r\n");
}
} else {
if ($limit = $this->getArgs('hi', 'history')) {
$this->_printHeader();
$historyList = $this->_io->getHistoryList($limit);
if (count($historyList)) {
printf("Global Actions list:\r\n");
foreach ($historyList as $i => $row) {
$actionDescr = sprintf("ID: %d\t - %s (%s on %s)", $i + 1, $row[PugMoRe_Mageploy_Model_Action_Abstract::INDEX_ACTION_DESCR], $row[PugMoRe_Mageploy_Model_Action_Abstract::INDEX_ACTION_USER], strftime("%c", $row[PugMoRe_Mageploy_Model_Action_Abstract::INDEX_ACTION_TIMESTAMP]));
$spacer = str_repeat(" ", max(0, 40 - strlen($actionDescr)));
printf("%s\r\n", $actionDescr);
}
printf("\r\nTotal global actions listed: %d\r\n", count($historyList));
} else {
printf("There aren't any actions tracked.\r\n");
}
} else {
if ($id = $this->getArgs('r', 'run')) {
$this->_printHeader();
$pendingList = $this->_io->getPendingList();
if (count($pendingList)) {
$executed = 0;
$session = Mage::getSingleton('adminhtml/session');
foreach ($pendingList as $i => $row) {
if ($id > 0 && $i + 1 != $id) {
continue;
}
$actionExecutorClass = $row[PugMoRe_Mageploy_Model_Action_Abstract::INDEX_EXECUTOR_CLASS];
$actionExecutorVersion = $row[PugMoRe_Mageploy_Model_Action_Abstract::INDEX_VERSION];
$controllerModule = $row[PugMoRe_Mageploy_Model_Action_Abstract::INDEX_CONTROLLER_MODULE];
$controllerName = $row[PugMoRe_Mageploy_Model_Action_Abstract::INDEX_CONTROLLER_NAME];
$controllerClassName = $this->_getControllerClassName($controllerModule, $controllerName);
if (class_exists($actionExecutorClass)) {
$controllerFileName = $this->_getControllerClassPath($controllerModule, $controllerName);
if (file_exists($controllerFileName)) {
include_once $controllerFileName;
} else {
printf("Error: file '%s' not found!\r\n", $controllerFileName);
}
if (class_exists($controllerClassName)) {
$actionExecutor = new $actionExecutorClass();
$parameters = $row[PugMoRe_Mageploy_Model_Action_Abstract::INDEX_ACTION_PARAMS];
try {
$request = $actionExecutor->decode($parameters, $actionExecutorVersion);
$controller = new $controllerClassName($request, new PugMoRe_Mageploy_Controller_Response_Http());
$action = $row[PugMoRe_Mageploy_Model_Action_Abstract::INDEX_ACTION_NAME] . 'Action';
$controller->preDispatch();
$controller->{$action}();
$controller->postDispatch();
} catch (Zend_Controller_Response_Exception $e) {
# do nothing: avoid "Headers already sent" error message
} catch (Exception $e) {
$session->addError($e->getMessage());
}
// Add messages in body response in case of Ajax requests
$messages = $session->getMessages(true);
if ($request && $request->getParam('isAjax', false)) {
$body = $controller->getResponse()->getBody();
$msg = Mage::getSingleton('core/message')->notice($body);
$messages->add($msg);
}
foreach ($messages->getItems() as $message) {
$messageType = $message->getType();
switch ($messageType) {
case Mage_Core_Model_Message::ERROR:
$color = self::TERM_COLOR_RED;
break;
case Mage_Core_Model_Message::SUCCESS:
$color = self::TERM_COLOR_GREEN;
break;
default:
#break intentionally omitted
#break intentionally omitted
case Mage_Core_Model_Message::WARNING:
#break intentionally omitted
#break intentionally omitted
case Mage_Core_Model_Message::NOTICE:
$color = self::TERM_COLOR_YELLOW;
break;
}
printf("Action ID #%d - %s %s\r\n", $i + 1, $this->__getColoredString($message->getType(), $color), $message->getText());
}
$executed++;
// register executed action
$this->_io->done($row);
} else {
printf("Error: class '%s' not found!\r\n", $controllerClassName);
}
} else {
printf("Error: class '%s' not found!\r\n", $actionExecutorClass);
}
// Yes, PHP is Object Oriented but don't forget the
// Superglobals! After all PHP is not Java :-)
$_GET = array();
$_POST = array();
$_REQUEST = array();
// And don't forget to reinitialize Magento to avoid
// problems with already populated objects (i.e. registry)
Mage::reset();
Mage::init($this->_appCode, $this->_appType);
}
// end foreach
printf("\r\nExecuted actions: %d/%d\r\n", $executed, count($pendingList));
} else {
printf("There aren't any pending actions to execute.\r\n");
}
} else {
echo $this->usageHelp($doTracking);
}
}
}
printf("\r\n");
}