smartbear_alertsite - Version 1.0.0

Version Notes

This is the initial release of AlertSite for Magento.

Download this release

Release Info

Developer SmartBear Software
Extension smartbear_alertsite
Version 1.0.0
Comparing to
See all releases


Version 1.0.0

Files changed (33) hide show
  1. app/code/community/Smartbear/Alertsite/Block/Adminhtml/Create.php +76 -0
  2. app/code/community/Smartbear/Alertsite/Block/Adminhtml/Dashboard.php +71 -0
  3. app/code/community/Smartbear/Alertsite/Block/Adminhtml/Notifications.php +35 -0
  4. app/code/community/Smartbear/Alertsite/Block/Adminhtml/Smartbear.php +12 -0
  5. app/code/community/Smartbear/Alertsite/Block/Adminhtml/System/Form/Renderer/ConfigMessage.php +29 -0
  6. app/code/community/Smartbear/Alertsite/Block/Adminhtml/System/Form/Renderer/DeviceDescription.php +56 -0
  7. app/code/community/Smartbear/Alertsite/Block/Adminhtml/System/Form/Renderer/Label.php +41 -0
  8. app/code/community/Smartbear/Alertsite/Block/Adminhtml/System/Form/Renderer/LinkButton.php +38 -0
  9. app/code/community/Smartbear/Alertsite/Helper/Data.php +18 -0
  10. app/code/community/Smartbear/Alertsite/Model/Alertsiteapi.php +904 -0
  11. app/code/community/Smartbear/Alertsite/Model/Observer.php +60 -0
  12. app/code/community/Smartbear/Alertsite/Model/System/Config/Backend/Editaccount.php +63 -0
  13. app/code/community/Smartbear/Alertsite/Model/System/Config/Backend/Editdevice.php +45 -0
  14. app/code/community/Smartbear/Alertsite/Model/System/Config/Backend/Enable.php +44 -0
  15. app/code/community/Smartbear/Alertsite/controllers/Adminhtml/AlertsiteController.php +128 -0
  16. app/code/community/Smartbear/Alertsite/etc/adminhtml.xml +58 -0
  17. app/code/community/Smartbear/Alertsite/etc/config.xml +84 -0
  18. app/code/community/Smartbear/Alertsite/etc/system.xml +160 -0
  19. app/design/adminhtml/default/default/layout/alertsite.xml +38 -0
  20. app/design/adminhtml/default/default/template/alertsite/create.phtml +90 -0
  21. app/design/adminhtml/default/default/template/alertsite/dashboard.phtml +36 -0
  22. app/design/adminhtml/default/default/template/alertsite/linkbutton.phtml +10 -0
  23. app/design/adminhtml/default/default/template/alertsite/notifications.phtml +11 -0
  24. app/etc/modules/Smartbear_Alertsite.xml +9 -0
  25. package.xml +19 -0
  26. skin/adminhtml/default/default/smartbear/alertsite/css/alertsite.css +25 -0
  27. skin/adminhtml/default/default/smartbear/alertsite/images/smartbear_15_icon.png +0 -0
  28. skin/adminhtml/default/default/smartbear/alertsite/images/smartbear_20_icon.gif +0 -0
  29. skin/adminhtml/default/default/smartbear/alertsite/images/smartbear_error.png +0 -0
  30. skin/adminhtml/default/default/smartbear/alertsite/images/smartbear_great.png +0 -0
  31. skin/adminhtml/default/default/smartbear/alertsite/images/smartbear_info.png +0 -0
  32. skin/adminhtml/default/default/smartbear/alertsite/images/smartbear_notice.png +0 -0
  33. skin/adminhtml/default/default/smartbear/alertsite/images/smartbear_warning.png +0 -0
app/code/community/Smartbear/Alertsite/Block/Adminhtml/Create.php ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Smartbear_Alertsite_Block_Adminhtml_Notifications
4
+ * Notifications block. Displays a note at the top of adminhtml pages.
5
+ *
6
+ * @category
7
+ * @package Smartbear_Alertsite
8
+ */
9
+ class Smartbear_Alertsite_Block_Adminhtml_Create extends Mage_Adminhtml_Block_Template
10
+ {
11
+ /**
12
+ * Sets the form action urls for the page.
13
+ */
14
+ public function _construct()
15
+ {
16
+ parent::_construct();
17
+
18
+ $this->setFormAction(Mage::helper('adminhtml')->getUrl('*/alertsite/provision'));
19
+ }
20
+
21
+ /**
22
+ * Retrieve Session Form Key
23
+ *
24
+ * @return string
25
+ */
26
+ public function getFormKey()
27
+ {
28
+ return Mage::getSingleton('core/session')->getFormKey();
29
+ }
30
+
31
+ public function getFirstname()
32
+ {
33
+ $user = Mage::getSingleton('admin/session')->getUser();
34
+ if($user && $user->getFirstname())
35
+ return ucwords($user->getFirstname());
36
+ else
37
+ return '';
38
+ }
39
+
40
+ public function getLastname()
41
+ {
42
+ $user = Mage::getSingleton('admin/session')->getUser();
43
+ if($user && $user->getLastname())
44
+ return ucwords($user->getLastname());
45
+ else
46
+ return '';
47
+ }
48
+
49
+ public function getCompanyName()
50
+ {
51
+ $storeName = Mage::getStoreConfig('general/store_information/name', Mage::app()->getStore()->getCode());
52
+ if($storeName)
53
+ return $storeName;
54
+ else
55
+ return '';
56
+ }
57
+
58
+ public function getStoreUrl()
59
+ {
60
+ $storeUrl = Mage::getStoreConfig('web/secure/base_url', Mage::app()->getStore()->getCode());
61
+ if($storeUrl)
62
+ return $storeUrl;
63
+ else
64
+ return '';
65
+ }
66
+
67
+ public function getEmail()
68
+ {
69
+ $user = Mage::getSingleton('admin/session')->getUser();
70
+ if($user && $user->getEmail())
71
+ return ucwords($user->getEmail());
72
+ else
73
+ return '';
74
+ }
75
+
76
+ }
app/code/community/Smartbear/Alertsite/Block/Adminhtml/Dashboard.php ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ class Smartbear_Alertsite_Block_Adminhtml_Dashboard extends Mage_Core_Block_Template
4
+ {
5
+
6
+ var $_template = "alertsite/dashboard.phtml";
7
+
8
+ /** @var $_alertsiteApi Smartbear_Alertsite_Model_AlertsiteApi */
9
+ private $_alertsiteApi = null;
10
+
11
+ public function __construct()
12
+ {
13
+ parent::__construct();
14
+
15
+ if(! Mage::helper('alertsite')->getConfig('alertsite_config', 'enabled') )
16
+ {
17
+ return;
18
+ }
19
+
20
+ $this->_alertsiteApi = Mage::getModel('alertsite/alertsiteapi');
21
+ $this->_alertsiteApi->getDeviceStatus();
22
+ }
23
+
24
+ public function isEnabled()
25
+ {
26
+ return Mage::helper('alertsite')->getConfig('alertsite_config', 'enabled');
27
+ }
28
+
29
+ public function getFriendlyStatus()
30
+ {
31
+ return $this->_alertsiteApi->getFriendlyStatus();
32
+ }
33
+
34
+ public function getStatusCode()
35
+ {
36
+ return $this->_alertsiteApi->getStatusCode();
37
+ }
38
+
39
+ public function getStatusTime()
40
+ {
41
+ $timeAtLastChange = $this->_alertsiteApi->getStatusLastChanged();
42
+ $currentTime = time();
43
+ $timeDiff = $currentTime - $timeAtLastChange;
44
+
45
+ /** @var $dateModel Mage_Core_Model_Date */
46
+ $dateModel = Mage::getModel('core/date');
47
+
48
+ if ($timeAtLastChange)
49
+ return $dateModel->date('M j, g:i:s A', $timeAtLastChange);
50
+ else
51
+ return 'N/A';
52
+ }
53
+
54
+ public function getStatusDescription()
55
+ {
56
+ return $this->_alertsiteApi->getDeviceDescription();
57
+ }
58
+
59
+ public function _toHtml()
60
+ {
61
+ if(! Mage::helper('alertsite')->getConfig('alertsite_config', 'enabled') )
62
+ {
63
+ return "</fieldset></div>";
64
+ }
65
+
66
+ $html = $this->renderView();
67
+ return $html;
68
+
69
+ }
70
+
71
+ }
app/code/community/Smartbear/Alertsite/Block/Adminhtml/Notifications.php ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * SL_Signaturelink_Block_Adminhtml_Notifications
4
+ * Notifications block. Displays a note at the top of adminhtml pages.
5
+ *
6
+ * @category Signaturelink
7
+ * @package SL_Signaturelink
8
+ */
9
+ class Smartbear_Alertsite_Block_Adminhtml_Notifications extends Mage_Adminhtml_Block_Template
10
+ {
11
+ /**
12
+ * Get x management url
13
+ *
14
+ * @return string
15
+ */
16
+ public function getManageUrl()
17
+ {
18
+ return $this->getUrl('adminhtml/system_config/edit', array('section' => 'alertsite'));
19
+ }
20
+
21
+ /**
22
+ * Check to see if config options are set
23
+ * @return bool
24
+ */
25
+ public function isRequiredSettingsNotification()
26
+ {
27
+ if(!Mage::helper('alertsite')->getConfig('alertsite_config', 'enabled'))
28
+ {
29
+ return false;
30
+ }
31
+
32
+ $test = (strlen(trim(Mage::helper('alertsite')->getConfig('alertsite_config', 'alertsite_user'))) == 0 || strlen(trim(Mage::helper('alertsite')->getConfig('alertsite_config', 'alertsite_pass'))) == 0);
33
+ return $test;
34
+ }
35
+ }
app/code/community/Smartbear/Alertsite/Block/Adminhtml/Smartbear.php ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Smartbear_Alertsite_Block_Adminhtml_Notifications
4
+ * Notifications block. Displays a note at the top of adminhtml pages.
5
+ *
6
+ * @category
7
+ * @package Smartbear_Alertsite
8
+ */
9
+ class Smartbear_Alertsite_Block_Adminhtml_Notifications extends Mage_Adminhtml_Block_Template
10
+ {
11
+
12
+ }
app/code/community/Smartbear/Alertsite/Block/Adminhtml/System/Form/Renderer/ConfigMessage.php ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * @category Signaturelink
4
+ * @package SL_Signaturelink
5
+ */
6
+
7
+ /**
8
+ * Renderer for sub-heading in fieldset
9
+ *
10
+ * @author Magento Core Team <core@magentocommerce.com>
11
+ */
12
+ class Smartbear_Alertsite_Block_Adminhtml_System_Form_Renderer_ConfigMessage
13
+ extends Mage_Adminhtml_Block_Abstract implements Varien_Data_Form_Element_Renderer_Interface
14
+ {
15
+ /**
16
+ * Render element html
17
+ *
18
+ * @param Varien_Data_Form_Element_Abstract $element
19
+ * @return string
20
+ */
21
+ public function render(Varien_Data_Form_Element_Abstract $element)
22
+ {
23
+ return sprintf('<tr class="system-fieldset-sub-head" id="row_%s"><td colspan="5">'
24
+ . '<h4 id="%s">%s</h4><div id="system-fieldset-sub-head-comment">%s</div></td></tr>',
25
+ $element->getHtmlId(), $element->getHtmlId(),
26
+ $element->getLabel(), $element->getComment()
27
+ );
28
+ }
29
+ }
app/code/community/Smartbear/Alertsite/Block/Adminhtml/System/Form/Renderer/DeviceDescription.php ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ class Smartbear_Alertsite_Block_Adminhtml_System_Form_Renderer_DeviceDescription
4
+ extends Mage_Adminhtml_Block_System_Config_Form_Field
5
+ implements Varien_Data_Form_Element_Renderer_Interface
6
+ {
7
+ /**
8
+ * Get equipped with API!
9
+ *
10
+ * @return Smartbear_Alertsite_Model_AlertsiteApi
11
+ */
12
+ public function getApi()
13
+ {
14
+ return Mage::getModel('alertsite/alertsiteapi');
15
+ }
16
+
17
+ /**
18
+ * Unset scope label and pass further to parent render()
19
+ *
20
+ * @param Varien_Data_Form_Element_Abstract $element
21
+ * @return string
22
+ */
23
+ public function render(Varien_Data_Form_Element_Abstract $element)
24
+ {
25
+ // Unset the scope label near the button
26
+ $element->unsScope()->unsCanUseWebsiteValue()->unsCanUseDefaultValue();
27
+ return parent::render($element);
28
+ }
29
+
30
+ protected function _getElementHtml(Varien_Data_Form_Element_Abstract $element)
31
+ {
32
+ $this->addData(array(
33
+ 'html_id' => $element->getHtmlId()
34
+ ));
35
+
36
+ return $this->_toHtml();
37
+ }
38
+
39
+ protected function _toHtml()
40
+ {
41
+
42
+ $api = $this->getApi();
43
+ $response = $api->getDeviceStatus();
44
+ $deviceDescription = $api->getDeviceDescription();
45
+
46
+ if (empty($deviceDescription))
47
+ $deviceDescription = 'None configured';
48
+
49
+ $html = '<h5 id="'. $this->getHtmlId() . '"><span>';
50
+ $html .= $deviceDescription;
51
+ $html .= '</span></h5>';
52
+
53
+ return $html;
54
+
55
+ }
56
+ }
app/code/community/Smartbear/Alertsite/Block/Adminhtml/System/Form/Renderer/Label.php ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Magento Enterprise Edition
4
+ *
5
+ * NOTICE OF LICENSE
6
+ *
7
+ * This source file is subject to the Magento Enterprise Edition License
8
+ * that is bundled with this package in the file LICENSE_EE.txt.
9
+ * It is also available through the world-wide-web at this URL:
10
+ * http://www.magentocommerce.com/license/enterprise-edition
11
+ * If you did not receive a copy of the license and are unable to
12
+ * obtain it through the world-wide-web, please send an email
13
+ * to license@magentocommerce.com so we can send you a copy immediately.
14
+ *
15
+ * DISCLAIMER
16
+ *
17
+ * Do not edit or add to this file if you wish to upgrade Magento to newer
18
+ * versions in the future. If you wish to customize Magento for your
19
+ * needs please refer to http://www.magentocommerce.com for more information.
20
+ *
21
+ * @category Mage
22
+ * @package Mage_Adminhtml
23
+ * @copyright Copyright (c) 2011 Magento Inc. (http://www.magentocommerce.com)
24
+ * @license http://www.magentocommerce.com/license/enterprise-edition
25
+ */
26
+
27
+
28
+ /**
29
+ * Adminhtml system config datetime field renderer
30
+ *
31
+ * @category Mage
32
+ * @package Mage_Adminhtml
33
+ * @author Magento Core Team <core@magentocommerce.com>
34
+ */
35
+ class Smartbear_Alertsite_Block_Adminhtml_System_Form_Renderer_Label extends Mage_Adminhtml_Block_System_Config_Form_Field
36
+ {
37
+ protected function _getElementHtml(Varien_Data_Form_Element_Abstract $element)
38
+ {
39
+ return '<h5 id="'.$element->getHtmlId().'"><span>'.$this->escapeHtml($element->getValue()).'</span></h5>';
40
+ }
41
+ }
app/code/community/Smartbear/Alertsite/Block/Adminhtml/System/Form/Renderer/LinkButton.php ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ class Smartbear_Alertsite_Block_Adminhtml_System_Form_Renderer_LinkButton
4
+ extends Mage_Adminhtml_Block_System_Config_Form_Field
5
+ implements Varien_Data_Form_Element_Renderer_Interface
6
+ {
7
+ protected $_template = 'alertsite/linkbutton.phtml';
8
+
9
+ /**
10
+ * Unset scope label and pass further to parent render()
11
+ *
12
+ * @param Varien_Data_Form_Element_Abstract $element
13
+ * @return string
14
+ */
15
+ public function render(Varien_Data_Form_Element_Abstract $element)
16
+ {
17
+ // Unset the scope label near the button
18
+ $element->unsScope()->unsCanUseWebsiteValue()->unsCanUseDefaultValue();
19
+ return parent::render($element);
20
+ }
21
+
22
+ /**
23
+ * Get the button and scripts contents
24
+ *
25
+ * @param Varien_Data_Form_Element_Abstract $element
26
+ * @return string
27
+ */
28
+ protected function _getElementHtml(Varien_Data_Form_Element_Abstract $element)
29
+ {
30
+ $originalData = $element->getOriginalData();
31
+ $this->addData(array(
32
+ 'button_label' => $originalData['button_label'],
33
+ 'html_id' => $element->getHtmlId(),
34
+ 'button_url' => Mage::getModel('adminhtml/url')->getUrl($originalData['button_url']),
35
+ ));
36
+ return $this->_toHtml();
37
+ }
38
+ }
app/code/community/Smartbear/Alertsite/Helper/Data.php ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ class Smartbear_Alertsite_Helper_Data extends Mage_Core_Helper_Abstract
3
+ {
4
+
5
+ const XML_CONFIG_PATH = 'alertsite/';
6
+
7
+ public function getConfig($section, $key, $flag = false) {
8
+ $path = self::XML_CONFIG_PATH . $section . '/' . $key;
9
+
10
+ if ($flag) {
11
+ return Mage::getStoreConfigFlag($path);
12
+ } else {
13
+ return Mage::getStoreConfig($path);
14
+ }
15
+ }
16
+
17
+
18
+ }
app/code/community/Smartbear/Alertsite/Model/Alertsiteapi.php ADDED
@@ -0,0 +1,904 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Alertsite API Model
4
+ *
5
+ *
6
+ * @method string getStatusCode()
7
+ * @method string getStatusLastChanged()
8
+ * @method string getDeviceDescription()
9
+ * @method string getLastResponseTime()
10
+ * @method setStatusCode(string $code)
11
+ * @method setStatusLastChanged(string $date)
12
+ * @method setDeviceDescription(string $description)
13
+ * @method setLastResponseTime(string $time)
14
+ * @method setLastApiStatus(int $status)
15
+ * @method int getLastApiStatus()
16
+ * @method setLastApiMessage(string $message)
17
+ * @method string getLastApiMessage()
18
+ *
19
+ */
20
+ class Smartbear_Alertsite_Model_Alertsiteapi extends Mage_Core_Model_Abstract
21
+ {
22
+
23
+ //Endpoint URLs
24
+ const ALERTSITE_REST_API_URL = 'https://www.alertsite.com/cgi-bin/alertsite-restapi/index.cgi/'; // http://help.alertsite.com/AlertSite/RestAPI
25
+ const ALERTSITE_REPORT_API_URL = 'https://www.alertsite.com/report-api/'; // http://help.alertsite.com/ReportAPI
26
+ const ALERTSITE_PROVISION_API_URL = 'https://www.alertsite.com/cgi-bin/orderform';
27
+
28
+ //Config paths that are needed for configuration of AlertSite API
29
+ const CONFIG_ACTIVATION_URL = 'alertsite/alertsite_config/alertsite_activation_email';
30
+ const CONFIG_DEVICE_ID = 'alertsite/alertsite_config/device_id';
31
+ const CONFIG_DEJACLICK_DEVICE_ID = 'alertsite/alertsite_config/dejaclick_device_id';
32
+ const CONFIG_CLIENT_ID = 'alertsite/alertsite_config/client_id';
33
+ const CONFIG_ALERTSITE_USER = 'alertsite/alertsite_config/alertsite_user';
34
+ const CONFIG_ALERTSITE_PASS = 'alertsite/alertsite_config/alertsite_pass';
35
+ const CONFIG_ALERTSITE_DEVICE_URL = 'alertsite/alertsite_config/device_url';
36
+ const CONFIG_ALERTSITE_DEVICE_DESCRIPTION = 'alertsite/alertsite_config/device_description';
37
+ const CONFIG_ALERTSITE_ENABLED = 'alertsite/alertsite_config/enabled';
38
+ const CONFIG_ALERTSITE_PROVISIONED = 'alertsite/alertsite_config/provisioned';
39
+ const CONFIG_ALERTSITE_PHONE = 'alertsite/alertsite_config/alertsite_phone';
40
+
41
+
42
+ protected $_statusApiResult = null;
43
+ protected $_detailApiResult = null;
44
+ protected $_statuses =
45
+ array(
46
+ "0" => "Site responded normally to all tests",
47
+ "1" => "TCP connection failed",
48
+ "2" => "Test timed out",
49
+ "3" => "Invalid response from server",
50
+ "5" => "Validation failed",
51
+ "6" => "No response from server",
52
+ "7" => "HTTP error from web server",
53
+ "8" => "Web site is redirected (warning)",
54
+ "9" => "Ping failed (site is not responding)",
55
+ "51" => "Unable to resolve IP address",
56
+ "60" => "soapUI-specific error",
57
+ "61" => "soapUI startup error",
58
+ "99" => "Unable to ping from location",
59
+ "80" => "Browser event timeout encountered",
60
+ "82" => "Page took too long to load",
61
+ "83" => "Firefox event notification did not occur",
62
+ "84" => "Page location did not change when expected",
63
+ "85" => "Expected page updates did not occur",
64
+ "86" => "Network activity did not stop",
65
+ "89" => "Internal Browser timeout",
66
+ "81" => "Maximum transaction time was exceeded",
67
+ "90" => "Unexpected processing exception occurred",
68
+ "91" => "Minimum match score not met",
69
+ "92" => "Maximum number of skipped events exceeded",
70
+ "93" => "Missing instruction for dialog prompt",
71
+ "94" => "Maximum transaction steps exceeded",
72
+ "95" => "Unable to monitor from chosen station",
73
+ "96" => "Unable to parse transaction XML",
74
+ "97" => "Target page element was not found",
75
+ "70" => "Remote verification did not detect an error. No alert is generated",
76
+ "71" => "Remote verification has confirmed an error. Alert is generated",
77
+ "72" => "Remote verification response was not received. Alert is generated",
78
+ "73" => "Remote verification response was invalid. Alert is generated",
79
+ "79" => "Remote verification was unavailable. Alert is generated",
80
+ "98" => "Notification (alert) was generated",
81
+ "9095" => "Unable to run test from chosen monitoring location",
82
+ "50XX" => "Mobile Device error where XX corresponds to the codes above",
83
+ "5300" => "Service Execution Error",
84
+ "60XX" => "soapUI error where XX corresponds to the codes above",
85
+ "70XX" => "Page object error where XX corresponds to the codes above",
86
+ "7121" => "Object length changed",
87
+ "7122" => "Missing object",
88
+ "7123" => "New object found",
89
+ "7130" => "No fullpage objects found",
90
+ "7202" => "Fullpage Timeout",
91
+ "4030" => "Warning threshold exceeded for monitored item",
92
+ "4040" => "Error threshold exceeded for monitored item",
93
+ "4050" => "Reported results were not received when expected",
94
+ "4059" => "Ping failed (server not reporting)"
95
+ );
96
+
97
+
98
+ /**
99
+ * Get the configured username returned from provisioning
100
+ *
101
+ * @return string
102
+ */
103
+ public function getUsername()
104
+ {
105
+ return Mage::helper('alertsite')->getConfig('alertsite_config', 'alertsite_user');
106
+ }
107
+
108
+ /**
109
+ * Get the configured password returned from provisioning
110
+ *
111
+ * todo make sure that we change the password config type to obscured
112
+ * @return string
113
+ */
114
+ public function getPassword()
115
+ {
116
+ return Mage::helper('alertsite')->getConfig('alertsite_config', 'alertsite_pass');
117
+ }
118
+
119
+ /**
120
+ * Get the configured phone number!
121
+ *
122
+ * @return string
123
+ */
124
+ public function getPhone()
125
+ {
126
+ return Mage::helper('alertsite')->getConfig('alertsite_config', 'alertsite_phone');
127
+ }
128
+
129
+ /**
130
+ * Get the configured device id returned from provisioning
131
+ *
132
+ * @return string
133
+ */
134
+ public function getDeviceId()
135
+ {
136
+ return Mage::helper('alertsite')->getConfig('alertsite_config','device_id');
137
+ }
138
+
139
+ /**
140
+ * Get the configured dejaclick device id returned from provisioning
141
+ *
142
+ * @return string
143
+ */
144
+ public function getDejaclickDeviceId()
145
+ {
146
+ return Mage::helper('alertsite')->getConfig('alertsite_config','dejaclick_device_id');
147
+ }
148
+
149
+ /**
150
+ * Get the configured customer id returned from provisioning
151
+ *
152
+ * @return string
153
+ */
154
+ public function getCustomerId()
155
+ {
156
+ return Mage::helper('alertsite')->getConfig('alertsite_config','client_id');
157
+ }
158
+
159
+ /**
160
+ * Get the configured device monitor url
161
+ * @return string
162
+ */
163
+ public function getDeviceMonitorUrl()
164
+ {
165
+ return Mage::helper('alertsite')->getConfig('alertsite_config','device_url');
166
+ }
167
+
168
+ /**
169
+ * Get a url for use in a curl call
170
+ *
171
+ * @param string $action
172
+ * @param array $params
173
+ * @param string $api
174
+ * @return string
175
+ */
176
+ public function getUrl($action = '', $params = array(), $api = 'rest')
177
+ {
178
+
179
+ $baseUrl = '';
180
+
181
+ switch(strtolower($api))
182
+ {
183
+ case 'report':
184
+ $baseUrl = self::ALERTSITE_REPORT_API_URL;
185
+ break;
186
+ case 'rest':
187
+ $baseUrl = self::ALERTSITE_REST_API_URL;
188
+ break;
189
+ case 'provision':
190
+ $baseUrl = self::ALERTSITE_PROVISION_API_URL;
191
+ break;
192
+ default:
193
+ $baseUrl = self::ALERTSITE_REST_API_URL;
194
+ }
195
+
196
+ $url = $baseUrl.$action;
197
+
198
+ if (!empty($params))
199
+ {
200
+ $customerId = ''; // customer id appears at the beginning of the urls in report calls - might be unused now
201
+ if (array_key_exists('customer_id', $params))
202
+ {
203
+ $customerId = $params['customer_id'];
204
+
205
+ unset($params['customer_id']);
206
+ }
207
+
208
+ $url .= '/'.$customerId.'?';
209
+
210
+ $i = 0;
211
+ $size = count($params);
212
+
213
+ foreach ($params as $k => $v)
214
+ {
215
+ $url .= urlencode($k).'='.urlencode($v);
216
+ if ($i!=$size-1)
217
+ $url .= '&';
218
+
219
+ $i++;
220
+ }
221
+
222
+ }
223
+
224
+ return $url;
225
+ }
226
+
227
+ /**
228
+ * Build an XML request for our (Rest) API
229
+ *
230
+ *
231
+ * Here's a typical request:
232
+ * <Upload>
233
+ * <APIVersion>1.1</APIVersion>
234
+ * <Authenticate>
235
+ * <Login>$LOGIN</Login>
236
+ * <SessionID>$SESSION_ID</SessionID>
237
+ * </Authenticate>
238
+ * <Request>
239
+ * <ObjDevice>$OBJ_DEVICE</ObjDevice>
240
+ * <URL>$URL</URL>
241
+ * </Request>
242
+ * </Upload>
243
+ *
244
+ * Here's the special case Login request:
245
+ *
246
+ * <Login>
247
+ * <ControlID></ControlID>
248
+ * <KeepAlive>1</KeepAlive>
249
+ * <Login>foo@bar.com</Login>
250
+ * <Password>$PASS</Password>
251
+ * </Login>
252
+ *
253
+ *
254
+ * @param string $root
255
+ * @param bool $autoLogin - automatically log in if we don't have a session id
256
+ * @return SimpleXMLElement
257
+ */
258
+ public function getRequest($root = '', $autoLogin = true)
259
+ {
260
+
261
+ $requestXml = new SimpleXMLElement('<'.$root.'/>');
262
+
263
+
264
+ if ($root == 'Login') // special case login xml
265
+ {
266
+ $requestXml->KeepAlive = '1';
267
+ $requestXml->Login = $this->getUsername();
268
+ $requestXml->Password = $this->getPassword();
269
+
270
+ } else
271
+ {
272
+ $requestXml->APIVersion = '1.1';
273
+
274
+ // set up login and session
275
+ $requestXml->Authenticate->Login = $this->getUsername();
276
+
277
+ if ($autoLogin && !$this->getSessionId())
278
+ $this->login(); // if we don't have a session id, we have to log in... this will trigger the special case code above.
279
+
280
+ $requestXml->Authenticate->SessionID = $this->getSessionId();
281
+ }
282
+
283
+ return $requestXml;
284
+ }
285
+
286
+ /**
287
+ * Curl function to make a post or get request and get the resulting response.
288
+ *
289
+ * Called with the URL to use, the request object if necessary, what kind of request you want and whether or not
290
+ * you are making an XML request!
291
+ *
292
+ * @param string $url
293
+ * @param SimpleXMLElement|null $request todo maybe make this a string someday
294
+ * @param string $action
295
+ * @param bool $xml
296
+ * @return SimpleXMLElement|bool
297
+ */
298
+ public function getCurlResponse($url, $request = null, $action = 'POST', $xml = true)
299
+ {
300
+ if ($action == 'POST')
301
+ {
302
+ // curl set up
303
+ $ch = curl_init();
304
+ curl_setopt($ch, CURLOPT_URL, $url);
305
+
306
+ if($xml)
307
+ {
308
+ curl_setopt($ch, CURLOPT_POST, 1);
309
+ curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
310
+ curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
311
+ curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
312
+ curl_setopt($ch, CURLOPT_POSTFIELDS, $request->asXML());
313
+ // Mage::Log('Request - '.$request->asXML(), null, 'alertsite.log', true);
314
+
315
+ }
316
+ else
317
+ {
318
+ $fieldString = '';
319
+
320
+ foreach($request as $key=>$value) { $fieldString .= $key.'='.$value.'&'; }
321
+
322
+ $fieldString = rtrim($fieldString, '&');
323
+
324
+ curl_setopt($ch,CURLOPT_POST, count($request));
325
+ curl_setopt($ch, CURLOPT_POSTFIELDS, $fieldString);
326
+
327
+ }
328
+
329
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
330
+ $output = curl_exec($ch);
331
+ // Mage::Log('Response - '.$output->asXML(), null, 'alertsite.log', true);
332
+ curl_close($ch);
333
+
334
+ // todo everything went swell check
335
+ $output = $this->getObjectFromXml($output);
336
+
337
+ return $output;
338
+ }
339
+
340
+ if ($action == 'GET')
341
+ {
342
+ // todo graceful failure on username not defined
343
+ $username = $this->getUsername();
344
+ $password = $this->getPassword();
345
+
346
+ //Get the result from the AlertSite/SmartBear API
347
+ $ch = curl_init();
348
+ curl_setopt($ch, CURLOPT_URL, $url);
349
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
350
+ curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
351
+ curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1);
352
+ curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
353
+ $output = curl_exec($ch);
354
+ curl_close($ch);
355
+
356
+ $output = $this->getObjectFromXml($output);
357
+
358
+ return $output;
359
+ }
360
+
361
+ return false;
362
+ }
363
+
364
+
365
+ /**
366
+ * Process an XML response string to create an XML object
367
+ *
368
+ * @param $xmlString
369
+ * @return bool|SimpleXMLElement
370
+ */
371
+ public function getObjectFromXml($xmlString)
372
+ {
373
+ try {
374
+ $output = simplexml_load_string($xmlString);
375
+ }
376
+ catch(Exception $ex) {
377
+ //todo: check if we should log this message
378
+ return false;
379
+ }
380
+
381
+ return $output;
382
+ }
383
+
384
+ /**
385
+ * Login to the SMARTBEAR ALERTSITE Api
386
+ *
387
+ * @return bool
388
+ */
389
+ public function login()
390
+ {
391
+ $request = $this->getRequest('Login');
392
+
393
+ // curl set up
394
+ $url = $this->getUrl('user/login');
395
+
396
+ $response = $this->getCurlResponse($url, $request);
397
+
398
+ if ($response && property_exists($response, 'Status') && $response->Status == '0' && property_exists($response, 'SessionID'))
399
+ {
400
+ $this->setSessionId((string) $response->SessionID);
401
+ return true;
402
+ }
403
+ else // there was an error,
404
+ {
405
+ //Check to see if the response code was 47, i.e. account was not activated yet
406
+ if($response && property_exists($response, 'Status') && $response->Status == '47')
407
+ {
408
+ $activationUrl = Mage::helper('alertsite')->getConfig('alertsite_config', 'alertsite_activation_email');
409
+ Mage::getSingleton('adminhtml/session')->addError('Your SmartBear AlertSite account has not yet been activated. Click <a target="_blank" onclick="setTimeout(\'window.location=window.location;\', 2000);" href="'.$activationUrl.'">here</a> to enable your account.');
410
+ return false;
411
+ }
412
+ }
413
+
414
+ }
415
+
416
+
417
+
418
+ /**
419
+ * Path: devices/download
420
+ *
421
+ * Request Body XML:
422
+ * <Download>
423
+ * <Authenticate>
424
+ * <Login>bob</Login>
425
+ * <SessionID>ef770427109e343e</SessionID>
426
+ * </Authenticate>
427
+ * <Request>
428
+ * <ControlID>control_555</ControlID>
429
+ * <ObjDevice>83621</ObjDevice>
430
+ * </Request>
431
+ * </Download>
432
+ *
433
+ * @return SimpleXMLElement
434
+ */
435
+ public function getDevice()
436
+ {
437
+ $request = $this->getRequest('Download');
438
+ $request->Request->ObjDevice = $this->getDeviceId();
439
+
440
+ $url = $this->getUrl('devices/download');
441
+
442
+ $response = $this->getCurlResponse($url, $request);
443
+
444
+ return $response;
445
+ }
446
+
447
+ /**
448
+ * Get the device status for this account!
449
+ *
450
+ * Path: devices/status
451
+ *
452
+ * Request Body XML:
453
+ * <Status>
454
+ * <APIVersion>1.1</APIVersion>
455
+ * <Authenticate>
456
+ * <Login>foo@bar.com</Login>
457
+ * <SessionID>17d63297eb5956be</SessionID>
458
+ * </Authenticate>
459
+ * <Request>
460
+ * <ControlID>5555-99</ControlID>
461
+ * <ObjDevice>159560</ObjDevice>
462
+ * </Request>
463
+ * </Status>
464
+ *
465
+ * @return SimpleXMLElement|bool
466
+ */
467
+ public function getDeviceStatus()
468
+ {
469
+
470
+ // set up our request!
471
+ $request = $this->getRequest('Status');
472
+ $request->Request->ObjDevice = $this->getDeviceId(); // todo no device id
473
+ $request->Request->ControlID = 1;
474
+ $url = $this->getUrl('devices/status');
475
+
476
+ // prepare response!
477
+ /** @var $response SimpleXMLElement */
478
+ $response = $this->getCurlResponse($url, $request);
479
+
480
+ if ($response && property_exists($response, 'Request'))
481
+ {
482
+ // if we got a valid response back, we should check for errors
483
+ /** @var $requestResponse SimpleXMLElement */
484
+ $requestResponse = $response->Request;
485
+
486
+ // if there aren't any errors...
487
+ if ($requestResponse
488
+ && property_exists($requestResponse, 'Status')
489
+ && $requestResponse->Status == '0')
490
+ {
491
+
492
+ // grab that device
493
+ $device = $requestResponse->Device; // todo NO DEVICE?
494
+
495
+ // do what we used to do
496
+ $this->setStatusCode((string)$device->StatusCode);
497
+ $this->setStatusLastChanged((string)$device->DtLastStatus);
498
+ $this->setDeviceDescription((string)$device->Descrip);
499
+ }
500
+
501
+ return $response;
502
+ }
503
+
504
+ return false;
505
+ }
506
+
507
+ /**
508
+ * Update our account information.
509
+ *
510
+ * Path: account/update
511
+ *
512
+ * Request body XML:
513
+ * <Update>
514
+ * <Authenticate>
515
+ * <Login>bob@foo.com</Login>
516
+ * <SessionID>10d0f31bbcc57750</SessionID>
517
+ * </Authenticate>
518
+ * <Request>
519
+ * <ControlID>control 12345</ControlID>
520
+ * <User>
521
+ * <SelectedLogin>bob@foo.com</SelectedLogin>
522
+ * <Login>joe@foo.com</Login>
523
+ * <ContactPhone1>954-312-1111</ContactPhone1>
524
+ * <ContactPhone2>954-312-2222</ContactPhone2>
525
+ * </User>
526
+ * <Company>My New Company Name</Company>
527
+ * <TimeZone>-5</TimeZone>
528
+ * <DST>US</DST>
529
+ * </Request>
530
+ * </Update>
531
+ *
532
+ *
533
+ * @param Varien_Object $info
534
+ * @return bool
535
+ */
536
+ public function updateAccount($info)
537
+ {
538
+ // set up request object
539
+ $request = $this->getRequest('Update');
540
+ $request->Request->ControlID = 1;
541
+
542
+ if ($info->getLogin())
543
+ {
544
+ $request->Request->User->SelectedLogin = $this->getUsername();
545
+ $request->Request->User->Login = $info->getLogin();
546
+ }
547
+
548
+ if ($info->getContactPhone())
549
+ {
550
+ $request->Request->User->SelectedLogin = $this->getUsername();
551
+ $request->Request->User->ContactPhone1 = $info->getContactPhone();
552
+ // $request->Request->User->ContactPhone2 = '';
553
+ }
554
+
555
+ // $request->Request->Company = '';
556
+ // $request->Request->TimeZone = '';
557
+ // $request->Request->DST = '';
558
+
559
+ $url = $this->getUrl('account/update');
560
+
561
+ // prepare response!
562
+ /** @var $response SimpleXMLElement */
563
+ $response = $this->getCurlResponse($url, $request);
564
+
565
+ if ($response && property_exists($response, 'Request'))
566
+ {
567
+ // if we got a valid response back, we should check for errors
568
+ /** @var $requestResponse SimpleXMLElement */
569
+ $requestResponse = $response->Request;
570
+
571
+ // if there aren't any errors...
572
+ if ($requestResponse
573
+ && property_exists($requestResponse, 'Status'))
574
+ {
575
+
576
+ if ($requestResponse->Status == '0')
577
+ {
578
+ return true; // success!
579
+ }
580
+ else // we ran into some trouble...
581
+ {
582
+ $this->setLastApiStatus($requestResponse->Status);
583
+
584
+ if (property_exists($requestResponse, 'Message'))
585
+ $this->setLastApiMessage($requestResponse->Message.' ('.$this->getLastApiStatus().')');
586
+ }
587
+ }
588
+ }
589
+
590
+ // something terrible happened
591
+ return false;
592
+ }
593
+
594
+ /**
595
+ * Update the Device URL for our account!
596
+ *
597
+ * Path: devices/upload
598
+ *
599
+ * Request body XML:
600
+ * <Upload>
601
+ * <APIVersion>1.1</APIVersion>
602
+ * <Authenticate>
603
+ * <Login>foo@bar.com</Login>
604
+ * <SessionID>17d63297eb5956be</SessionID>
605
+ * </Authenticate>
606
+ * <Request>
607
+ * <ControlID>5555-99</ControlID>
608
+ * <ObjDevice>159560</ObjDevice>
609
+ * <HttpFollowRedirect>n</HttpFollowRedirect>
610
+ * <URL>www.cnn.com</URL>
611
+ * </Request>
612
+ * <Request> todo make sure to update the deja click device too...
613
+ * <ControlID>5555-100</ControlID>
614
+ * <ObjDevice>159561</ObjDevice>
615
+ * <HttpFollowRedirect>n</HttpFollowRedirect>
616
+ * <URL>www.cnn.com</URL>
617
+ * <Monitor>n</Monitor>
618
+ * </Request>
619
+ * </Upload>
620
+ *
621
+ * $LOGIN, $SESSION_ID, $OBJ_DEVICE and $URL need to be replaced with appropriate values.
622
+ * Please note that the "APIVersion" node (with value 1.1) is required to enable the URL node to take effect.
623
+ * Other applicable nodes can be defined in the "Request" node when the "URL" node is defined.
624
+ *
625
+ * @param string $deviceId
626
+ * @param string $url
627
+ * @return bool
628
+ */
629
+ public function updateDeviceUrl($url = '', $deviceId = '', $dejaclickDeviceId = '')
630
+ {
631
+ if (empty($deviceId))
632
+ $deviceId = $this->getDeviceId();
633
+
634
+ if (empty($dejaclickDeviceId))
635
+ $dejaclickDeviceId = $this->getDejaclickDeviceId();
636
+
637
+
638
+
639
+ // set up request object
640
+ $request = $this->getRequest('Upload');
641
+
642
+ $siteDeviceRequest = $request->addChild('Request');
643
+ $siteDeviceRequest->ObjDevice = $deviceId;
644
+ $siteDeviceRequest->URL = $url;
645
+ $siteDeviceRequest->Descrip = $url;
646
+ $siteDeviceRequest->HttpFollowRedirect = 'n'; // should fix error code 64 - and remove request for function we don't qualify for
647
+ $siteDeviceRequest->ControlID = 1;
648
+
649
+ $dejaclickDeviceRequest = $request->addChild('Request');
650
+ $dejaclickDeviceRequest->ObjDevice = $dejaclickDeviceId;
651
+ $dejaclickDeviceRequest->URL = $url;
652
+ $dejaclickDeviceRequest->Descrip = $url;
653
+ $dejaclickDeviceRequest->HttpFollowRedirect = 'n'; // should fix error code 64 - and remove request for function we don't qualify for
654
+ $dejaclickDeviceRequest->ControlID = 1;
655
+
656
+
657
+ $url = $this->getUrl('devices/upload');
658
+
659
+ // prepare response!
660
+ /** @var $response SimpleXMLElement */
661
+ $response = $this->getCurlResponse($url, $request);
662
+
663
+ if ($response && property_exists($response, 'Request'))
664
+ {
665
+ // if we got a valid response back, we should check for errors
666
+ /** @var $requestResponse SimpleXMLElement */
667
+ $requestResponse = $response->Request;
668
+
669
+ // if there aren't any errors...
670
+ if ($requestResponse
671
+ && property_exists($requestResponse, 'Status'))
672
+ {
673
+
674
+ if ($requestResponse->Status == '0')
675
+ {
676
+ return true; // success!
677
+ }
678
+ else // prepare error response
679
+ {
680
+ $this->setLastApiStatus($requestResponse->Status);
681
+
682
+ if (property_exists($requestResponse, 'Message'))
683
+ $this->setLastApiMessage($requestResponse->Message.' ('.$this->getLastStatus().')');
684
+ }
685
+ }
686
+ }
687
+
688
+ // something terrible happened
689
+ return false;
690
+ }
691
+
692
+ /**
693
+ * Simple toggle function for turning monitor on and off on default device.
694
+ *
695
+ * for request structure
696
+ * @see Smartbear_Alertsite_Model_Alertsiteapi::updateDeviceUrl()
697
+ *
698
+ * @param bool $enable
699
+ * @return bool|SimpleXMLElement
700
+ */
701
+ public function enableMonitor($enable = true)
702
+ {
703
+ if ($enable)
704
+ $enable = 'y';
705
+ else
706
+ $enable = 'n';
707
+
708
+ // set up request object
709
+ $request = $this->getRequest('Upload');
710
+
711
+ $siteDevice = $request->addChild('Request');
712
+ $siteDevice->ObjDevice = $this->getDeviceId();
713
+ $siteDevice->HttpFollowRedirect = 'n';
714
+ $siteDevice->Monitor = $enable;
715
+
716
+ $dejaclickDevice = $request->addChild('Request');
717
+ $dejaclickDevice->ObjDevice = $this->getDejaclickDeviceId();
718
+ $dejaclickDevice->HttpFollowRedirect = 'n';
719
+ $dejaclickDevice->Monitor = $enable;
720
+
721
+ $url = $this->getUrl('devices/upload');
722
+
723
+ /** @var $response SimpleXMLElement */
724
+ $response = $this->getCurlResponse($url, $request);
725
+
726
+ if ($response && property_exists($response, 'Request'))
727
+ {
728
+ // if we got a valid response back, we should check for errors
729
+ /** @var $requestResponse SimpleXMLElement */
730
+ $requestResponse = $response->Request;
731
+
732
+ // if there aren't any errors...
733
+ if ($requestResponse
734
+ && property_exists($requestResponse, 'Status'))
735
+ {
736
+
737
+ if ($requestResponse->Status == '0')
738
+ {
739
+ return true; // success!
740
+ }
741
+ else // prepare error response
742
+ {
743
+ $this->setLastApiStatus($requestResponse->Status);
744
+
745
+ if (property_exists($requestResponse, 'Message'))
746
+ $this->setLastApiMessage($requestResponse->Message.' ('.$this->getLastStatus().')');
747
+ }
748
+ }
749
+ }
750
+
751
+ return false;
752
+ }
753
+
754
+ /**
755
+ * This does a lookup in the status array defined at the top of the class to produce a friendly status message.
756
+ *
757
+ * @param bool $code
758
+ * @return bool|string
759
+ */
760
+ public function getFriendlyStatus($code = false)
761
+ {
762
+ if($code === false && $this->hasData('status_code'))
763
+ $code = $this->getStatusCode();
764
+ else
765
+ return false;
766
+
767
+ $statuses = $this->_statuses;
768
+ $status = $statuses[$code];
769
+ return $status;
770
+ }
771
+
772
+ /**
773
+ * Provision a new account with SmartBear's AlertSite API and return any response
774
+ *
775
+ * @param $parameters
776
+ * @throws Exception - error message encountered when provisioning
777
+ * @return bool - was the account successfully provisioned?
778
+ */
779
+ public function provisionAccount(Varien_Object $parameters)
780
+ {
781
+ //get the provisioning URL
782
+ $url = self::ALERTSITE_PROVISION_API_URL;
783
+
784
+ $apiFriendlyParams = array();
785
+
786
+ $apiFriendlyParams['fname'] = urlencode($parameters->getFirstName());
787
+ $apiFriendlyParams['lname'] = urlencode($parameters->getLastName());
788
+ $apiFriendlyParams['phone'] = urlencode($parameters->getPhone());
789
+ $apiFriendlyParams['company'] = urlencode($parameters->getCompanyName());
790
+ $apiFriendlyParams['api_version'] = urlencode('1.0');
791
+ $apiFriendlyParams['order_type'] = urlencode('magento');
792
+ $apiFriendlyParams['response_type'] = urlencode('xml');
793
+ $apiFriendlyParams['login_email'] = urlencode($parameters->getLoginEmail());
794
+ $apiFriendlyParams['monitor_url'] = urlencode($parameters->getMonitorUrl());
795
+
796
+ $this->setPhone($parameters->getPhone());
797
+
798
+ $response = $this->getCurlResponse($url, $apiFriendlyParams, 'POST', false);
799
+
800
+ if(!$response)
801
+ {
802
+ throw new Exception("There was a problem provisioning your new account. Please try again or contact SmartBear support.");
803
+ return false;
804
+ }
805
+
806
+ $status = (string)$response["status"];
807
+
808
+ if($status == 'failed')
809
+ {
810
+ $error = $response->Error;
811
+
812
+ $errorMessage = (string)$error['description'];
813
+
814
+ if((string)$error['code'] == "0001")
815
+ {
816
+
817
+ if($error->missing_fields)
818
+ {
819
+ $count = count($error->missing_fields);
820
+ $missingFields = "";
821
+
822
+ if($count > 1)
823
+ {
824
+ foreach($error->missing_fields as $missingField)
825
+ {
826
+ if((string)$missingField['name'] == 'monitor_url')
827
+ $missingFields .= " - Monitor URL Required";
828
+ else if((string)$missingField['name'] == 'login_email')
829
+ $missingFields .= " - Login Email Required";
830
+ }
831
+ }
832
+
833
+ else
834
+ {
835
+ if((string)$error->missing_fields['name'] == 'monitor_url')
836
+ $missingFields .= " - Monitor URL Required";
837
+ else if((string)$error->missing_fields['name'] == 'login_email')
838
+ $missingFields .= " - Login Email Required";
839
+ }
840
+
841
+ $errorMessage .= $missingFields;
842
+
843
+ }
844
+ }
845
+
846
+ throw new Exception($errorMessage);
847
+ }
848
+ else if($status == 'success')
849
+ {
850
+ $this->_newAlertSiteConfig($response->AccountCreated);
851
+ return true;
852
+
853
+ }
854
+
855
+ return false;
856
+
857
+ }
858
+
859
+ /**
860
+ * Helper function to create a new account given a response from the provisioning API
861
+ *
862
+ * todo could use some cleanup.
863
+ *
864
+ * @param SimpleXMLElement $newAccount
865
+ */
866
+ protected function _newAlertSiteConfig($newAccount)
867
+ {
868
+ $account = $newAccount->Account;
869
+ $devices = $newAccount->Devices;
870
+ //Setup user info
871
+ Mage::getConfig()->saveConfig(self::CONFIG_ALERTSITE_ENABLED, true);
872
+ Mage::getConfig()->saveConfig(self::CONFIG_ACTIVATION_URL, (string)$account['activation_url']);
873
+ Mage::getConfig()->saveConfig(self::CONFIG_ALERTSITE_USER, (string)$account['login']);
874
+ Mage::getConfig()->saveConfig(self::CONFIG_ALERTSITE_PASS, (string)$account['password']);
875
+
876
+ Mage::getConfig()->saveConfig(self::CONFIG_CLIENT_ID, (string)$account['custid']);
877
+ Mage::getConfig()->saveConfig(self::CONFIG_ALERTSITE_PROVISIONED, 1);
878
+
879
+ if($this->getPhone())
880
+ Mage::getConfig()->saveConfig(self::CONFIG_ALERTSITE_PHONE, $this->getPhone());
881
+
882
+ Mage::getSingleton('adminhtml/session')->addSuccess('Thank you for creating your AlertSite account. Click <a target="_blank" onclick="setTimeout(\'window.location=window.location;\', 2000);" href="'.(string)$account['activation_url'].'">here</a> to enable your account.');
883
+
884
+ //Setup Devices
885
+ foreach($devices->Device as $device)
886
+ {
887
+ $deviceType = (string)$device['type'];
888
+ if($deviceType == 'ipd')
889
+ {
890
+ Mage::getConfig()->saveConfig(self::CONFIG_ALERTSITE_DEVICE_URL, (string)$device['URL']);
891
+ Mage::getConfig()->saveConfig(self::CONFIG_ALERTSITE_DEVICE_DESCRIPTION, (string)$device['description']);
892
+ Mage::getConfig()->saveConfig(self::CONFIG_DEVICE_ID, (string)$device['obj_device']);
893
+ break;
894
+ }
895
+ else if($deviceType == 'dejaclick')
896
+ {
897
+ Mage::getConfig()->saveConfig(self::CONFIG_DEJACLICK_DEVICE_ID, (string)$device['obj_device']);
898
+ }
899
+ }
900
+
901
+ Mage::getConfig()->reinit();
902
+ Mage::app()->reinitStores();
903
+ }
904
+ }
app/code/community/Smartbear/Alertsite/Model/Observer.php ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ class Smartbear_Alertsite_Model_Observer
3
+ {
4
+ /**
5
+ * Place the hook in order to land our alertsite block on the Dash.
6
+ *
7
+ * @param Varien_Event_Observer $observer
8
+ * @return Smartbear_Alertsite_Model_Observer
9
+ */
10
+ public function coreBlockAbstractPrepareLayoutAfter(Varien_Event_Observer $observer)
11
+ {
12
+ if(!Mage::helper('alertsite')->getConfig('alertsite_config', 'enabled') || strlen(trim(Mage::helper('alertsite')->getConfig('alertsite_config', 'alertsite_user'))) < 1)
13
+ {
14
+ return $this;
15
+ }
16
+
17
+ if (Mage::app()->getFrontController()->getAction()->getFullActionName() === 'adminhtml_dashboard_index')
18
+ {
19
+ $block = $observer->getBlock();
20
+ if ($block->getNameInLayout() === 'dashboard')
21
+ {
22
+ $block->getChild('topSearches')->setAlertsiteDashboardHook(true);
23
+ }
24
+ }
25
+ return $this;
26
+ }
27
+
28
+ /**
29
+ * Looks for the hook we set and adds our own custom block.
30
+ *
31
+ * @param Varien_Event_Observer $observer
32
+ * @return Smartbear_Alertsite_Model_Observer
33
+ */
34
+ public function coreBlockAbstractToHtmlAfter(Varien_Event_Observer $observer)
35
+ {
36
+
37
+ if(!Mage::helper('alertsite')->getConfig('alertsite_config', 'enabled') || strlen(trim(Mage::helper('alertsite')->getConfig('alertsite_config', 'alertsite_user'))) < 1)
38
+ {
39
+ return $this;
40
+ }
41
+
42
+ if (Mage::app()->getFrontController()->getAction()->getFullActionName() === 'adminhtml_dashboard_index')
43
+ {
44
+ if ($observer->getBlock()->getAlertsiteDashboardHook())
45
+ {
46
+ $html = $observer->getTransport()->getHtml(); // grab the html that's already rendered
47
+
48
+
49
+ /** @var $dashboardBox Smartbear_Alertsite_Block_Adminhtml_Dashboard */
50
+ $dashboardBox = $observer->getBlock()->getLayout()->createBlock('alertsite/adminhtml_dashboard');
51
+
52
+ $html .= $dashboardBox->toHtml();
53
+
54
+ $observer->getTransport()->setHtml($html);
55
+ }
56
+ }
57
+
58
+ return $this;
59
+ }
60
+ }
app/code/community/Smartbear/Alertsite/Model/System/Config/Backend/Editaccount.php ADDED
@@ -0,0 +1,63 @@