Skip to content

Commit 40fbe24

Browse files
committed
update cms page sync api
1 parent df98a65 commit 40fbe24

File tree

20 files changed

+529
-429
lines changed

20 files changed

+529
-429
lines changed

CmsSync/Block/Adminhtml/Cms/Block/Edit.php

Lines changed: 7 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -5,52 +5,16 @@ class CalinDiacon_CmsSync_Block_Adminhtml_Cms_Block_Edit extends Mage_Adminhtml_
55

66
public function __construct()
77
{
8-
$this->_objectId = 'block_id';
9-
$this->_controller = 'cms_block';
10-
118
parent::__construct();
129

13-
$this->_updateButton('save', 'label', Mage::helper('cms')->__('Save Block'));
14-
$this->_updateButton('delete', 'label', Mage::helper('cms')->__('Delete Block'));
15-
16-
$isMaster = Mage::getStoreConfig('cmssync/general/source');
17-
if ($isMaster){
18-
$this->_addButton('sync', array(
19-
'label' => Mage::helper('adminhtml')->__('Sync Block to all nodes'),
20-
'onclick' => 'setLocation(\''. $this->getUrl('*/*/sync') . 'block_id/' . $this->getRequest()->getParam('block_id') . '\')',
21-
'class' => 'sync'
10+
// show button only if source is enabled
11+
$cmsSyncEnabled = Mage::getStoreConfig(CalinDiacon_CmsSync_Model_Constants::CMSSYNC_ENABLE);
12+
if ($cmsSyncEnabled){
13+
$this->_addButton('syncall', array(
14+
'label' => Mage::helper('cmssync')->__('Sync Block'),
15+
'onclick' => 'setLocation(\''. $this->getUrl('cmssync/cms_block/sync') . 'block_id/' . $this->getRequest()->getParam('block_id') . '\')',
2216
),-1,5);
23-
}
24-
25-
26-
$this->_formScripts[] = "
27-
function toggleEditor() {
28-
if (tinyMCE.getInstanceById('block_content') == null) {
29-
tinyMCE.execCommand('mceAddControl', false, 'block_content');
30-
} else {
31-
tinyMCE.execCommand('mceRemoveControl', false, 'block_content');
32-
}
33-
}
17+
}// end
3418

35-
function saveAndContinueEdit(){
36-
editForm.submit($('edit_form').action+'back/edit/');
37-
}
38-
";
3919
}
40-
41-
/**
42-
* Get edit form container header text
43-
*
44-
* @return string
45-
*/
46-
public function getHeaderText()
47-
{
48-
if (Mage::registry('cms_block')->getId()) {
49-
return Mage::helper('cms')->__("Edit Block '%s'", self::escapeHtml(Mage::registry('cms_block')->getTitle()));
50-
}
51-
else {
52-
return Mage::helper('cms')->__('New Block');
53-
}
54-
}
55-
5620
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
/**
3+
* Created by Calin Diacon.
4+
* User: cdiacon
5+
* Date: 06/05/2013
6+
* Time: 15:52
7+
*
8+
*/
9+
class CalinDiacon_CmsSync_Block_Adminhtml_Cms_Page_Edit extends Mage_Adminhtml_Block_Cms_Page_Edit
10+
{
11+
public function __construct()
12+
{
13+
parent::__construct();
14+
15+
// show button only if source is enabled
16+
$cmsSyncEnabled = Mage::getStoreConfig(CalinDiacon_CmsSync_Model_Constants::CMSSYNC_ENABLE);
17+
if ($cmsSyncEnabled){
18+
$this->_addButton('syncall', array(
19+
'label' => Mage::helper('cmssync')->__('Sync Page'),
20+
'onclick' => 'setLocation(\''. $this->getUrl('cmssync/cms_page/sync') . 'page_id/' . $this->getRequest()->getParam('page_id') . '\')',
21+
),-1,5);
22+
}// end
23+
}
24+
25+
}

CmsSync/Block/Field.php

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
/**
3+
* Created by Calin Diacon.
4+
* System configuration array input
5+
* User: cdiacon
6+
* Date: 06/05/2013
7+
* Time: 09:59
8+
*
9+
*/
10+
class CalinDiacon_CmsSync_Block_Field extends Mage_Adminhtml_Block_System_Config_Form_Field_Array_Abstract
11+
{
12+
/**
13+
* Get select block for type
14+
* @return mixed
15+
*/
16+
protected function _getTypeRenderer()
17+
{
18+
if (!$this->_typeRenderer) {
19+
$this->_typeRenderer = $this->getLayout()
20+
->createBlock('cmssync/select')
21+
->setIsRenderToJsTemplate(true);
22+
}
23+
return $this->_typeRenderer;
24+
}
25+
26+
/**
27+
* Get select block for search field
28+
* @return mixed
29+
*/
30+
protected function _getSearchFieldRenderer()
31+
{
32+
if (!$this->_searchFieldRenderer) {
33+
$this->_searchFieldRenderer = $this->getLayout()
34+
->createBlock('cmssync/select')
35+
->setIsRenderToJsTemplate(true);
36+
}
37+
return $this->_searchFieldRenderer;
38+
}
39+
40+
41+
protected function _prepareToRender()
42+
{
43+
44+
$this->_typeRenderer = null;
45+
$this->_searchFieldRenderer = null;
46+
47+
$this->addColumn('id', array(
48+
'label' => Mage::helper('cmssync')->__('ID'),
49+
'class' => 'disabled',
50+
'style' => 'width:20px;'
51+
));
52+
$this->addColumn('apiurl', array(
53+
'label' => Mage::helper('cmssync')->__('API Url'),
54+
'style' => 'width:400px '
55+
56+
));
57+
$this->addColumn('username', array(
58+
'label' => Mage::helper('cmssync')->__('Username'),
59+
'style' => 'width: 150px'
60+
));
61+
$this->addColumn('password', array(
62+
'label' => Mage::helper('cmssync')->__('Api key'),
63+
'style' => 'width: 150px'
64+
));
65+
66+
// Disables "Add after" button
67+
$this->_addAfter = false;
68+
$this->_addButtonLabel = Mage::helper('cmssync')->__('Add Field');
69+
}
70+
71+
}

CmsSync/Block/Select.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
/**
3+
* Created by Calin Diacon.
4+
* Html Select Element
5+
* User: cdiacon
6+
* Date: 06/05/2013
7+
* Time: 10:18
8+
*
9+
*/
10+
class CalinDiacon_CmsSync_Block_Select extends Mage_Adminhtml_Block_Html_Select
11+
{
12+
/**
13+
* return all in one line
14+
* @return string
15+
*/
16+
public function _toHtml()
17+
{
18+
return trim(preg_replace('/s+/', ' ', parent::_toHtml()));
19+
20+
}
21+
}

CmsSync/Model/Abstract.php

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
/**
3+
* Created by Calin Diacon.
4+
* User: cdiacon
5+
* Date: 05/05/2013
6+
* Time: 19:23
7+
*
8+
*/
9+
class CalinDiacon_CmsSync_Model_Abstract extends Mage_Core_Model_Abstract
10+
{
11+
protected $isEnabled = false;
12+
protected $isMaster = false;
13+
// block id, page id
14+
protected $id;
15+
protected $pageId;
16+
protected $storeIds = array();
17+
public $title ;
18+
public $content;
19+
protected $proxy;
20+
protected $session;
21+
public $node;
22+
public $identifier;
23+
24+
protected $_creation_time;
25+
protected $_update_time;
26+
public $is_active;
27+
protected $_storeId;
28+
protected $_stores = array();
29+
public $storeCodes = array();
30+
31+
32+
33+
/**
34+
* check if remote has most updated info
35+
* @param $remoteUpdateTime
36+
* @return bool
37+
*/
38+
public function isLater($remoteUpdateTime)
39+
{
40+
$remoteTime = new Zend_Date($remoteUpdateTime);
41+
$localTime = new Zend_Date($this->updateTime);
42+
if($localTime->isLater($remoteTime)){
43+
return true;
44+
};
45+
return false;
46+
}
47+
48+
/**
49+
* Get nodes data
50+
* @return array|bool
51+
*/
52+
public function getValidNodes()
53+
{
54+
55+
$nodes = unserialize(Mage::getStoreConfig('cmssync/general/nodes'));
56+
57+
if(! empty($nodes)){
58+
59+
$data = array();
60+
foreach ($nodes as $node){
61+
62+
$nodeModel = new CalinDiacon_CmsSync_Model_Node();
63+
64+
$nodeModel->setUrl($node['apiurl']);
65+
$nodeModel->setUsername($node['username']);
66+
$nodeModel->setPassword($node['password']);
67+
68+
// save valid
69+
if($nodeModel->isValid())
70+
$data[] = $nodeModel;
71+
}
72+
return $data;
73+
}
74+
75+
return false;
76+
}
77+
78+
79+
}

CmsSync/Model/Block.php

Lines changed: 67 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,73 @@
11
<?php
22

3-
class CalinDiacon_CmsSync_Model_Block
3+
class CalinDiacon_CmsSync_Model_Block extends CalinDiacon_CmsSync_Model_Abstract
44
{
5-
public $blockId;
6-
public $title;
7-
public $identifier;
8-
public $content;
9-
protected $_creation_time;
10-
protected $_update_time;
11-
public $is_active;
12-
protected $_storeId;
13-
protected $_stores = array();
14-
public $storeCodes = array();
5+
6+
public function sync($blockId)
7+
{
8+
$result = array('errors' => false, 'message' => "<br /><ul style='list-style-type: square;'>");
9+
// node models
10+
$nodes = $this->getValidNodes();
11+
12+
if($nodes){
13+
14+
$blockModel = Mage::getModel('cms/block')->load($blockId);
15+
16+
// loop all nodes api
17+
foreach ($nodes as $node) {
18+
19+
try{
20+
21+
$proxy = new SoapClient($node->getUrl());
22+
$sessionId = $proxy->login($node->getUsername(), $node->getPassword());
23+
$remoteBlock = $proxy->call($sessionId, 'cms_api.block_info', $blockModel->getIdentifier());// array of info for the remote block
24+
25+
if($remoteBlock){
26+
27+
// update block
28+
$proxy->call($sessionId, 'cms_api.block_update', array($blockModel->getData()));
29+
}else{
30+
31+
// create new block
32+
$proxy->call($sessionId, 'cms_api.block_create', array($blockModel->getData()));
33+
}
34+
/**
35+
* save all media from the content
36+
*/
37+
preg_match_all('/<img.*src=.*url="(.*)"\}\}.*\/\>/', $blockModel->getContent(), $matches);
38+
if (isset($matches[1])){
39+
$allMedia = $matches[1];
40+
41+
foreach ($allMedia as $filename){
42+
$fileData = base64_encode(file_get_contents(Mage::getBaseDir('media') . DS . $filename));
43+
$data = array(
44+
'fileName' => $filename,
45+
'file' => $fileData
46+
);
47+
48+
$proxy->call($sessionId, 'cms_api.block_media', array($data));
49+
50+
}
51+
52+
53+
}
54+
55+
}catch(Exception $e){
56+
Mage::throwException($e->getMessage());
57+
$result['errors'] = true;
58+
$result['message'] = '<li>error sync remote node, ' . $e->getMessage() . '</li>';
59+
}
60+
}
61+
62+
}else{
63+
Mage::getSingleton('adminhtml/session')->addNotice('No valid nodes found!');
64+
$result['errors'] = true;
65+
$result['message'] = '<li>No valid nodes found.</li>';
66+
}
67+
$result['message'] .='</ul>';
68+
return $result;
69+
70+
}
1571

1672

1773
}

0 commit comments

Comments
 (0)