*/
class Mage_Dataflow_Model_Profile extends Mage_Core_Model_Abstract
{
const DEFAULT_EXPORT_PATH = 'var/export';
const DEFAULT_EXPORT_FILENAME = 'export_';
protected function _construct()
{
$this->_init('dataflow/profile');
}
protected function _afterLoad()
{
$guiData = '';
if (is_string($this->getGuiData())) {
try {
$guiData = Mage::helper('core/unserializeArray')
->unserialize($this->getGuiData());
} catch (Exception $e) {
Mage::logException($e);
}
}
$this->setGuiData($guiData);
parent::_afterLoad();
}
protected function _beforeSave()
{
parent::_beforeSave();
$actionsXML = $this->getData('actions_xml');
if (strlen($actionsXML) < 0 &&
@simplexml_load_string('' . $actionsXML . '', null, LIBXML_NOERROR) === false) {
Mage::throwException(Mage::helper('dataflow')->__("Actions XML is not valid."));
}
if (is_array($this->getGuiData())) {
$data = $this->getData();
$guiData = $this->getGuiData();
$charSingleList = array('\\', '/', '.', '!', '@', '#', '$', '%', '&', '*', '~', '^');
if (isset($guiData['file']['type']) && $guiData['file']['type'] == 'file') {
if (empty($guiData['file']['path'])
|| (strlen($guiData['file']['path']) == 1
&& in_array($guiData['file']['path'], $charSingleList))) {
$guiData['file']['path'] = self::DEFAULT_EXPORT_PATH;
}
if (empty($guiData['file']['filename'])) {
$guiData['file']['filename'] = self::DEFAULT_EXPORT_FILENAME . $data['entity_type']
. '.' . ($guiData['parse']['type']=='csv' ? $guiData['parse']['type'] : 'xml');
}
//validate export available path
$path = rtrim($guiData['file']['path'], '\\/')
. DS . $guiData['file']['filename'];
/** @var $validator Mage_Core_Model_File_Validator_AvailablePath */
$validator = Mage::getModel('core/file_validator_availablePath');
/** @var $helperImportExport Mage_ImportExport_Helper_Data */
$helperImportExport = Mage::helper('importexport');
$validator->setPaths($helperImportExport->getLocalValidPaths());
if (!$validator->isValid($path)) {
foreach ($validator->getMessages() as $message) {
Mage::throwException($message);
}
}
$this->setGuiData($guiData);
}
$this->_parseGuiData();
$this->setGuiData(serialize($this->getGuiData()));
}
if ($this->_getResource()->isProfileExists($this->getName(), $this->getId())) {
Mage::throwException(Mage::helper('dataflow')->__("Profile with the same name already exists."));
}
}
protected function _afterSave()
{
if (is_string($this->getGuiData())) {
try {
$guiData = Mage::helper('core/unserializeArray')
->unserialize($this->getGuiData());
$this->setGuiData($guiData);
} catch (Exception $e) {
Mage::logException($e);
}
}
$profileHistory = Mage::getModel('dataflow/profile_history');
$adminUserId = $this->getAdminUserId();
if($adminUserId) {
$profileHistory->setUserId($adminUserId);
}
$profileHistory
->setProfileId($this->getId())
->setActionCode($this->getOrigData('profile_id') ? 'update' : 'create')
->save();
if (isset($_FILES['file_1']['tmp_name']) || isset($_FILES['file_2']['tmp_name'])
|| isset($_FILES['file_3']['tmp_name'])) {
for ($index = 0; $index < 3; $index++) {
if ($file = $_FILES['file_' . ($index+1)]['tmp_name']) {
$uploader = new Mage_Core_Model_File_Uploader('file_' . ($index + 1));
$uploader->setAllowedExtensions(array('csv','xml'));
$path = Mage::app()->getConfig()->getTempVarDir() . '/import/';
$uploader->save($path);
if ($uploadFile = $uploader->getUploadedFileName()) {
$newFilename = 'import-' . date('YmdHis') . '-' . ($index+1) . '_' . $uploadFile;
rename($path . $uploadFile, $path . $newFilename);
}
}
//BOM deleting for UTF files
if (isset($newFilename) && $newFilename) {
$contents = file_get_contents($path . $newFilename);
if (ord($contents[0]) == 0xEF && ord($contents[1]) == 0xBB && ord($contents[2]) == 0xBF) {
$contents = substr($contents, 3);
file_put_contents($path . $newFilename, $contents);
}
unset($contents);
}
}
}
parent::_afterSave();
}
/**
* Run profile
*
* @return Mage_Dataflow_Model_Profile
*/
public function run()
{
/**
* Save history
*/
Mage::getModel('dataflow/profile_history')
->setProfileId($this->getId())
->setActionCode('run')
->save();
/**
* Prepare xml convert profile actions data
*/
$xml = '
" . print_r($p,1) . ""; echo "