Canary_Alerts - Version 1.0.2

Version Notes

first release

Download this release

Release Info

Developer Luke Davis
Extension Canary_Alerts
Version 1.0.2
Comparing to
See all releases


Version 1.0.2

app/code/community/Canary/Alerts/Block/Adminhtml/Form/Emailpassword.php ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ class Canary_Alerts_Block_Adminhtml_Form_Emailpassword extends Mage_Adminhtml_Block_Widget_Form_Container
3
+ {
4
+ public function __construct()
5
+ {
6
+ parent::__construct();
7
+ $this->_blockGroup = 'canaryalerts';
8
+ $this->_controller = 'adminhtml_form';
9
+ $this->_mode = 'emailpassword';
10
+ $this->_updateButton('save', 'label', Mage::helper('canaryalerts')->__('Update'));
11
+ }
12
+
13
+ public function getHeaderText()
14
+ {
15
+ return Mage::helper('canaryalerts')->__('Canary Alerts Setup');
16
+ }
17
+ }
app/code/community/Canary/Alerts/Block/Adminhtml/Form/Emailpassword/Form.php ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ class Canary_Alerts_Block_Adminhtml_Form_Emailpassword_Form extends Mage_Adminhtml_Block_Widget_Form
3
+ {
4
+ protected function _prepareForm()
5
+ {
6
+ $form = new Varien_Data_Form(array(
7
+ 'id' => 'edit_form',
8
+ 'action' => $this->getUrl('*/*/getkey'),
9
+ 'method' => 'post',
10
+ ));
11
+ $form->setUseContainer(true);
12
+ $this->setForm($form);
13
+ $fieldset = $form->addFieldset('form_general', array('legend' => Mage::helper('canaryalerts')->__('Please type your email and password.')));
14
+ $fieldset->addField('canary_email', 'text', array(
15
+ 'label' => Mage::helper('canaryalerts')->__('Canary Email'),
16
+ 'class' => 'required-entry',
17
+ 'required' => true,
18
+ 'name' => 'canary_email'
19
+ ));
20
+ $fieldset->addField('canary_password', 'password', array(
21
+ 'label' => Mage::helper('canaryalerts')->__('Canary Password'),
22
+ 'class' => 'required-entry',
23
+ 'required' => true,
24
+ 'name' => 'canary_password'
25
+ ));
26
+ if($status = $this->getRequest()->getParam("status")){
27
+ if($status == "failed"){
28
+ echo "<span style='color:red;'>" . $this->__("We don't have a username / password combination like that on file, please <a href=https://canaryalerts.com/>visit the site</a></span>");
29
+ }
30
+ }
31
+ return parent::_prepareForm();
32
+ }
33
+
34
+ }
app/code/community/Canary/Alerts/Block/Adminhtml/Form/Store.php ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ class Canary_Alerts_Block_Adminhtml_Form_Store extends Mage_Adminhtml_Block_Widget_Form_Container
3
+ {
4
+ public function __construct()
5
+ {
6
+ parent::__construct();
7
+ $this->_blockGroup = 'canaryalerts';
8
+ $this->_controller = 'adminhtml_form';
9
+ $this->_mode = 'store';
10
+
11
+ $this->_updateButton('save', 'label', Mage::helper('canaryalerts')->__('Update'));
12
+
13
+ }
14
+ public function getHeaderText()
15
+ {
16
+ return Mage::helper('canaryalerts')->__('Canary Alerts Setup');
17
+ }
18
+ }
app/code/community/Canary/Alerts/Block/Adminhtml/Form/Store/Form.php ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ class Canary_Alerts_Block_Adminhtml_Form_Store_Form extends Mage_Adminhtml_Block_Widget_Form
3
+ {
4
+ protected function _prepareForm()
5
+ {
6
+ $form = new Varien_Data_Form(array(
7
+ 'id' => 'edit_form',
8
+ 'action' => $this->getUrl('*/*/savestore'),
9
+ 'method' => 'post',
10
+ ));
11
+ $form->setUseContainer(true);
12
+ $this->setForm($form);
13
+ $fieldset = $form->addFieldset('form_general', array('legend' => Mage::helper('canaryalerts')->__('Please select store.')));
14
+ $storeDropDownValues = Mage::registry("canary_alerts_stores_dropdown");
15
+ $fieldset->addField('alerts_store', 'select', array(
16
+ 'label' => Mage::helper('canaryalerts')->__('Select Store'),
17
+ 'class' => 'required-entry',
18
+ 'required' => true,
19
+ 'name' => 'alerts_store',
20
+ 'values' => $storeDropDownValues
21
+ ));
22
+ return parent::_prepareForm();
23
+ }
24
+
25
+ }
app/code/community/Canary/Alerts/Helper/Data.php ADDED
@@ -0,0 +1,103 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ class Canary_Alerts_Helper_Data extends Mage_Core_Helper_Abstract{
3
+ public function _construct(){
4
+ parent::_construct();
5
+ }
6
+ public function sendRequest($url, $data = array(), $method = "GET"){
7
+ Mage::log("Sending request to url:" . $url, null, "canaryalerts.log");
8
+ // Get cURL resource
9
+ $curl = curl_init();
10
+ curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
11
+ curl_setopt($curl, CURLOPT_URL, $url);
12
+ curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT x.y; Win64; x64; rv:10.0) Gecko/20100101 Firefox/10.0');
13
+ curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
14
+ curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
15
+
16
+ if($method == "POST"){
17
+ curl_setopt($curl, CURLOPT_POST, 1);
18
+ curl_setopt($curl, CURLOPT_POSTFIELDS , $data);
19
+ }
20
+ Mage::log($data, null, "canaryalerts.log");
21
+ // Send the request & save response to $resp
22
+
23
+ $resp = curl_exec($curl);
24
+
25
+
26
+ if(!$resp || $resp == "false"){
27
+ return false;
28
+ }
29
+ $header = curl_getinfo ( $curl );
30
+ $httpCode = curl_getinfo ( $curl, CURLINFO_HTTP_CODE );
31
+ if($httpCode != 200 && $httpCode != 302){
32
+
33
+ }
34
+ // Close request to clear up some resources
35
+ curl_close($curl);
36
+ return $resp;
37
+ }
38
+ public function getStoreId(){
39
+ return Mage::getStoreConfig("canaryalerts/store_id");
40
+ }
41
+ public function isInstalled(){
42
+ $api = Mage::getStoreConfig("canaryalerts/api");
43
+ return ($api != "false" && $api != "") ? true : false;
44
+ }
45
+ public function getSaleFeed($orderId = false, $store_id = null){
46
+ $data = array();
47
+ $order = Mage::getModel("sales/order")->load($orderId);
48
+ $shippingAddress = $order->getShippingAddress();
49
+ $shippingData = $shippingAddress->getData();
50
+ $billingAddress = $order->getBillingAddress();
51
+ $billingData = $billingAddress->getData();
52
+ $data['billing_address1'] = $billingAddress->getStreet(1);
53
+ $data['billing_address2'] = $billingAddress->getStreet(2);
54
+ $data['billing_city'] = $billingData['city'];
55
+ $data['billing_country'] = Mage::getModel('directory/country')->load($billingData['country_id'])->getName();
56
+ $data['billing_firstname'] = $billingData['firstname'];
57
+ $data['billing_lastname'] = $billingData['lastname'];
58
+ $data['billing_postalcode'] = $billingData['postcode'];
59
+ $data['billing_region'] = $billingData['region'];
60
+ $data['item_count'] = count($order->getItemsCollection());
61
+ $data['shipping_address1'] = $shippingAddress->getStreet(1);
62
+ $data['shipping_address2'] = $shippingAddress->getStreet(2);
63
+ $data['shipping_city'] = $shippingData['city'];
64
+ $data['shipping_country'] = Mage::getModel('directory/country')->load($shippingData['country_id'])->getName();
65
+ $data['shipping_firstname'] = $shippingData['firstname'];
66
+ $data['shipping_lastname'] = $shippingData['lastname'];
67
+ $data['shipping_postalcode'] = $shippingData['postcode'];
68
+ $data['shipping_region'] = $shippingData['region'];
69
+ $data['store_id'] = $store_id ? $store_id : $this->getStoreId();
70
+ $data['subtotal'] = $order->getSubtotal();
71
+ $totals = $order->getTotals();
72
+ if(isset($totals['tax']) && $totals['tax']->getValue()) {
73
+ $tax = $totals['tax']->getValue(); //Tax value if present
74
+ } else {
75
+ $tax = 0;
76
+ }
77
+ $data['taxes'] = $tax;
78
+ $data['total'] = $order->getGrandTotal();
79
+ $data['date_created'] = date(DATE_ATOM, strtotime($order->getCreatedAt()));
80
+ $data['payment_method'] = $order->getPayment()->getMethodInstance()->getTitle();
81
+ $data['shipping_method'] = $order->getShippingDescription();
82
+ $data['shipping'] = $order->getShippingAmount();
83
+ $data['reference_id'] = $order->getIncrementId();
84
+ $orderLinesInfo = array();
85
+ foreach ($order->getItemsCollection() as $item){
86
+ $product = Mage::getModel("catalog/product")->load($item['product_id']);
87
+ $itemArray = array();
88
+
89
+ $regularPrice = number_format($product->getPrice(), 2);
90
+ $discountedPrice = number_format($product->getFinalPrice(), 2);
91
+ $itemArray['discount_price'] = $discountedPrice;
92
+ $itemArray['original_price'] = $regularPrice;
93
+ $itemArray['quantity'] = $item->getData("qty_ordered");
94
+ $itemArray['product_id'] = $product->getId();
95
+ $itemArray['product_name'] = $product->getName();
96
+ $itemArray['product_sku'] = $product->getSku();
97
+ $orderLinesInfo[] = $itemArray;
98
+ }
99
+ $data['details'] = $orderLinesInfo;
100
+ return json_encode($data);
101
+ }
102
+ }
103
+ ?>
app/code/community/Canary/Alerts/Model/Observer.php ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ class Canary_Alerts_Model_Observer
3
+ {
4
+ private $_ordersApiUrl;
5
+ private $_apiKey;
6
+ public function __construct(){
7
+ $this->_apiKey = Mage::getStoreConfig("canaryalerts/api");
8
+ $this->_ordersApiUrl = "https://canaryalerts.com/api/orders?key=" . $this->_apiKey;
9
+ }
10
+ public function orderPlaced(Varien_Event_Observer $observer){
11
+ if($this->isInstalled()){
12
+ $orderId = $observer['order_ids'][0];
13
+ $feedData = Mage::helper("canaryalerts")->getSaleFeed($orderId);
14
+ $result = Mage::helper("canaryalerts")->sendRequest($this->_ordersApiUrl, $feedData, "POST");
15
+ if(!$result){
16
+ return false;
17
+ }
18
+ }
19
+ }
20
+ public function isInstalled(){
21
+ $api = Mage::getStoreConfig("canaryalerts/api");
22
+ return ($api != "false" && $api != "") ? true : false;
23
+ }
24
+
25
+ }
app/code/community/Canary/Alerts/controllers/ConfigController.php ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ class Canary_Alerts_ConfigController extends Mage_Adminhtml_Controller_Action{
3
+ private $_ordersApiUrl;
4
+ private $_apiKey;
5
+ private $_apiUrl = "https://canaryalerts.com/api/";
6
+ private $_configModel;
7
+ public function _construct(){
8
+ parent::_construct();
9
+ $this->_apiKey = Mage::getStoreConfig("canaryalerts/api");
10
+ $this->_ordersApiUrl = "https://canaryalerts.com/api/orders?key=" . $this->_apiKey;
11
+ $this->_configModel = new Mage_Core_Model_Config();
12
+ }
13
+ public function indexAction(){
14
+ if(Mage::helper("canaryalerts")->isInstalled()){
15
+ $this->_redirect('*/*/installed');
16
+ return;
17
+ }
18
+ $this->loadLayout();
19
+ $this->_addContent($this->getLayout()->createBlock('canaryalerts/adminhtml_form_emailpassword'));
20
+ $this->renderLayout();
21
+ }
22
+ public function getkeyAction(){
23
+ if(Mage::helper("canaryalerts")->isInstalled()){
24
+ $this->_redirect('*/*/installed');
25
+ return;
26
+ }
27
+ if ($data = $this->getRequest()->getPost()) {
28
+ $email = $this->getRequest()->getPost("canary_email");
29
+ $password = $this->getRequest()->getPost("canary_password");
30
+ $url = $this->_apiUrl . "company?email=" . $email . "&password=" . $password;
31
+ $getApiRequest = Mage::helper("canaryalerts")->sendRequest($url, array());
32
+ if($getApiRequest == false){
33
+ $this->_redirect('*/*/index', array( "status" => "failed" ));
34
+ return;
35
+ }
36
+ $key = json_decode($getApiRequest, true)['api_key'];
37
+ $this->_configModel->saveConfig('canaryalerts/api', $key);
38
+ $dropDownUrl = $this->_apiUrl . "store?key=" . $key;
39
+ $dropDownArray = json_decode(Mage::helper("canaryalerts")->sendRequest($dropDownUrl, array()));
40
+ $dropDownValues = $this->getDropDownValues($dropDownArray);
41
+ Mage::register("canary_alerts_stores_dropdown", $dropDownValues);
42
+ $this->loadLayout();
43
+ $this->_addContent($this->getLayout()->createBlock('canaryalerts/adminhtml_form_store'));
44
+ $this->renderLayout();
45
+ }
46
+ }
47
+ public function installedAction(){
48
+ $this->loadLayout();
49
+
50
+ $api = Mage::getStoreConfig("canaryalerts/api");
51
+
52
+ //$message = $this->__("You are all setup to go!") . " <a href='" . Mage::helper("adminhtml")->getUrl("adminhtml/index/index") . "'>" . $this->__("Click here to return to the admin" ) . "</a>";
53
+ $message = '<iframe style="width: 100%; height: 800px; border: none;" src="https://canaryalerts.com/login?inline=1&key=' . $api . '"></iframe>';
54
+ $this->_addContent($this->getLayout()->createBlock('core/text')->setText($message));
55
+ $this->renderLayout();
56
+ }
57
+ public function savestoreAction(){
58
+ if ($data = $this->getRequest()->getPost()) {
59
+ $store_id = $this->getRequest()->getPost("alerts_store");
60
+ }
61
+ $this->_configModel->saveConfig('canaryalerts/store_id', $store_id);
62
+ $this->sendAllOrders($store_id);
63
+ $this->_redirect('*/*/installed');
64
+ }
65
+ public function sendAllOrders($store_id){
66
+ $orders = Mage::getModel("sales/order")->getCollection()->addFieldToFilter('created_at', array(
67
+ 'from' => strtotime('-6 month', time()),
68
+ 'to' => time(),
69
+ 'datetime' => true
70
+ ));
71
+ foreach($orders as $order){
72
+ $feedData = Mage::helper("canaryalerts")->getSaleFeed($order->getId(), $store_id);
73
+ $result = Mage::helper("canaryalerts")->sendRequest($this->_ordersApiUrl, $feedData, "POST");
74
+ }
75
+ }
76
+ public function getDropDownValues($dropDownArray){
77
+ $array = array();
78
+ foreach($dropDownArray as $el){
79
+ $array[] = array("value" => $el->id, "label" => $el->name);
80
+ }
81
+ return $array;
82
+ }
83
+ }
84
+ ?>
app/code/community/Canary/Alerts/etc/adminhtml.xml ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0"?>
2
+ <config>
3
+ <acl>
4
+ <resources>
5
+ <all>
6
+ <title>Allow Everything</title>
7
+ </all>
8
+ <admin>
9
+ <children>
10
+ <system>
11
+ <children>
12
+ <config>
13
+ <children>
14
+ <canaryalerts translate="title">
15
+ <title>Canaryalerts</title>
16
+ <sort_order>100</sort_order>
17
+ </canaryalerts>
18
+ </children>
19
+ </config>
20
+ </children>
21
+ </system>
22
+ </children>
23
+ </admin>
24
+ </resources>
25
+ </acl>
26
+ </config>
app/code/community/Canary/Alerts/etc/config.xml ADDED
@@ -0,0 +1,85 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <config>
2
+ <modules>
3
+ <Canary_Alerts>
4
+ <version>1.0.2</version>
5
+ </Canary_Alerts>
6
+ </modules>
7
+ <global>
8
+ <helpers>
9
+ <canaryalerts>
10
+ <class>Canary_Alerts_Helper</class>
11
+ </canaryalerts>
12
+ </helpers>
13
+ <blocks>
14
+ <canaryalerts>
15
+ <class>Canary_Alerts_Block</class>
16
+ </canaryalerts>
17
+ </blocks>
18
+ <models>
19
+ <canaryalerts>
20
+ <class>Canary_Alerts_Model</class>
21
+ </canaryalerts>
22
+ </models>
23
+ <events>
24
+ <checkout_onepage_controller_success_action>
25
+ <observers>
26
+ <canaryalerts_order_new>
27
+ <class>canaryalerts/observer</class>
28
+ <method>orderPlaced</method>
29
+ </canaryalerts_order_new>
30
+ </observers>
31
+ </checkout_onepage_controller_success_action>
32
+ </events>
33
+ </global>
34
+ <adminhtml>
35
+ <translate>
36
+ <modules>
37
+ <canaryalerts>
38
+ <files>
39
+ <default>Canary_Alerts.csv</default>
40
+ </files>
41
+ </canaryalerts>
42
+ </modules>
43
+ </translate>
44
+ <menu>
45
+ <canaryalerts module="canaryalerts">
46
+ <title>Canary</title>
47
+ <sort_order>60</sort_order>
48
+ <children>
49
+ <config translate="title" module="canaryalerts" >
50
+ <title>Alerts</title>
51
+ <action>canaryalerts/config/index</action>
52
+ </config>
53
+ </children>
54
+ </canaryalerts>
55
+ </menu>
56
+ </adminhtml>
57
+ <admin>
58
+ <routers>
59
+ <canaryalerts>
60
+ <use>admin</use>
61
+ <args>
62
+ <module>Canary_Alerts</module>
63
+ <frontName>canaryalerts</frontName>
64
+ </args>
65
+ </canaryalerts>
66
+ </routers>
67
+ </admin>
68
+ <frontend>
69
+ <routers>
70
+ <canaryalerts>
71
+ <use>standard</use>
72
+ <args>
73
+ <module>Canary_Alerts</module>
74
+ <frontName>canaryalerts</frontName>
75
+ </args>
76
+ </canaryalerts>
77
+ </routers>
78
+ </frontend>
79
+ <default>
80
+ <canaryalerts>
81
+ <api></api>
82
+ <store_id></store_id>
83
+ </canaryalerts>
84
+ </default>
85
+ </config>
app/etc/modules/Canary_Alerts.xml ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
1
+ <config>
2
+ <modules>
3
+ <Canary_Alerts>
4
+ <active>true</active>
5
+ <codePool>community</codePool>
6
+ </Canary_Alerts>
7
+ </modules>
8
+ </config>
package.xml ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0"?>
2
+ <package>
3
+ <name>Canary_Alerts</name>
4
+ <version>1.0.2</version>
5
+ <stability>stable</stability>
6
+ <license>GNU General Public License</license>
7
+ <channel>community</channel>
8
+ <extends/>
9
+ <summary>Canary monitors your stores to make sure you are still getting orders.</summary>
10
+ <description>Canary uses statistical modeling to figure out how many orders you should be receiving at any time of the day. If you too few or too many orders you'll be notified. Canary also checks your server every minute to make sure there are no technical errors and that your customers can reach your site as well.</description>
11
+ <notes>first release</notes>
12
+ <authors><author><name>Luke Davis</name><user>dangdevelopers</user><email>luke@dangdevelopers.com</email></author></authors>
13
+ <date>2014-11-06</date>
14
+ <time>16:21:03</time>
15
+ <contents><target name="magecommunity"><dir name="Canary"><dir name="Alerts"><dir name="Block"><dir name="Adminhtml"><dir name="Form"><dir name="Emailpassword"><file name="Form.php" hash="09d397bb6da54c300875a229544e768f"/></dir><file name="Emailpassword.php" hash="be66f0e56a451c567b6031cb88168b2a"/><dir name="Store"><file name="Form.php" hash="518f9b447d5bcc3713a57ba2c94e6266"/></dir><file name="Store.php" hash="965912cd619801dcfd1d5a55f60f1663"/></dir></dir></dir><dir name="Helper"><file name="Data.php" hash="62b91fb255eab1206ce1a6f32da114f5"/></dir><dir name="Model"><file name="Observer.php" hash="82af9ff839bdd4a2bcb17bc9e03f1c8b"/></dir><dir name="controllers"><file name="ConfigController.php" hash="4d4e6daba8e9ba49733b9932bfb4dae9"/></dir><dir name="etc"><file name="adminhtml.xml" hash="9088e3423aa512718da74d8e08f245a8"/><file name="config.xml" hash="c0bf32323ec2092cac33ad96b4ba1ee3"/></dir></dir></dir></target><target name="mageetc"><dir name="modules"><file name="Canary_Alerts.xml" hash="c0678779865b23ac8282177b0b53f848"/></dir></target><target name="magelocale"><dir/></target></contents>
16
+ <compatible/>
17
+ <dependencies><required><php><min>5.2.13</min><max>6.0.0</max></php></required></dependencies>
18
+ </package>