PageRenderTime 50ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/core/Mage/XmlConnect/Model/Application.php

https://bitbucket.org/claudiu_marginean/magento-hg-mirror
PHP | 948 lines | 485 code | 83 blank | 380 comment | 91 complexity | c7d8a2fda02474a04cdffdde80b16428 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, LGPL-2.1, GPL-2.0, WTFPL
  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  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_XmlConnect
  23. * @copyright Copyright (c) 2010 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * @method Mage_XmlConnect_Model_Mysql4_Application _getResource()
  28. * @method Mage_XmlConnect_Model_Mysql4_Application getResource()
  29. * @method string getName()
  30. * @method Mage_XmlConnect_Model_Application setName(string $value)
  31. * @method string getCode()
  32. * @method Mage_XmlConnect_Model_Application setCode(string $value)
  33. * @method string getType()
  34. * @method Mage_XmlConnect_Model_Application setType(string $value)
  35. * @method Mage_XmlConnect_Model_Application setStoreId(int $value)
  36. * @method string getActiveFrom()
  37. * @method Mage_XmlConnect_Model_Application setActiveFrom(string $value)
  38. * @method string getActiveTo()
  39. * @method Mage_XmlConnect_Model_Application setActiveTo(string $value)
  40. * @method string getUpdatedAt()
  41. * @method Mage_XmlConnect_Model_Application setUpdatedAt(string $value)
  42. * @method string getConfiguration()
  43. * @method Mage_XmlConnect_Model_Application setConfiguration(string $value)
  44. * @method int getStatus()
  45. * @method Mage_XmlConnect_Model_Application setStatus(int $value)
  46. * @method int getBrowsingMode()
  47. * @method Mage_XmlConnect_Model_Application setBrowsingMode(int $value)
  48. *
  49. * @category Mage
  50. * @package Mage_XmlConnect
  51. * @author Magento Core Team <core@magentocommerce.com>
  52. */
  53. class Mage_XmlConnect_Model_Application extends Mage_Core_Model_Abstract
  54. {
  55. /**
  56. * Application code cookie name
  57. *
  58. * @var string
  59. */
  60. const APP_CODE_COOKIE_NAME = 'app_code';
  61. /**
  62. * Device screen size name
  63. *
  64. * @var string
  65. */
  66. const APP_SCREEN_SIZE_NAME = 'screen_size';
  67. /**
  68. * Device screen size name
  69. *
  70. * @var string
  71. */
  72. const APP_SCREEN_SIZE_DEFAULT = '320x480';
  73. /**
  74. * Device screen size source name
  75. *
  76. * @var string
  77. */
  78. const APP_SCREEN_SOURCE_DEFAULT = 'default';
  79. /**
  80. * Application status "submitted" value
  81. *
  82. * @var int
  83. */
  84. const APP_STATUS_SUCCESS = 1;
  85. /**
  86. * Application status "not submitted" value
  87. *
  88. * @var int
  89. */
  90. const APP_STATUS_INACTIVE = 0;
  91. /**
  92. * Application prefix length of cutted part of deviceType and storeCode
  93. *
  94. * @var int
  95. */
  96. const APP_PREFIX_CUT_LENGTH = 3;
  97. /**
  98. * Last submitted data from history table
  99. *
  100. * @var null|array
  101. */
  102. protected $_lastParams;
  103. /**
  104. * Application submit info
  105. *
  106. * @var array
  107. */
  108. protected $submit_params = array();
  109. /**
  110. * Application submit action type
  111. *
  112. * @var bool
  113. */
  114. protected $is_resubmit_action = false;
  115. /**
  116. * Full application code
  117. *
  118. * @var null|string
  119. */
  120. protected $code;
  121. /**
  122. * Main configuration of current application
  123. *
  124. * @var null|array
  125. */
  126. protected $conf;
  127. /**
  128. * Social networking validation array
  129. *
  130. * Social networking validation array specified as
  131. * array (
  132. * network id => API key string length
  133. * )
  134. *
  135. * @var array
  136. */
  137. protected $_socialNetValidationArray = array(
  138. Mage_XmlConnect_Helper_Data::SOCIAL_NETWORK_TWITTER,
  139. Mage_XmlConnect_Helper_Data::SOCIAL_NETWORK_FACEBOOK,
  140. Mage_XmlConnect_Helper_Data::SOCIAL_NETWORK_LINKEDIN,
  141. );
  142. /**
  143. * Submission/Resubmission key max length
  144. *
  145. * @var int
  146. */
  147. const APP_MAX_KEY_LENGTH = 40;
  148. /**
  149. * XML path to config with an email address
  150. * for contact to receive credentials
  151. * of Urban Airship notifications
  152. *
  153. * @var string
  154. */
  155. const XML_PATH_CONTACT_CREDENTIALS_EMAIL = 'xmlconnect/mobile_application/urbanairship_credentials_email';
  156. /**
  157. * XML path to config with Urban Airship Terms of Service URL
  158. *
  159. * @var string
  160. */
  161. const XML_PATH_URBAN_AIRSHIP_TOS_URL = 'xmlconnect/mobile_application/urbanairship_terms_of_service_url';
  162. /**
  163. * XML path to config copyright data
  164. *
  165. * @var string
  166. */
  167. const XML_PATH_DESIGN_FOOTER_COPYRIGHT = 'design/footer/copyright';
  168. /**
  169. * XML path to config restriction status
  170. * (EE module)
  171. *
  172. * @var string
  173. */
  174. const XML_PATH_GENERAL_RESTRICTION_IS_ACTIVE = 'general/restriction/is_active';
  175. /**
  176. * XML path to config restriction mode
  177. * (EE module)
  178. *
  179. * @var string
  180. */
  181. const XML_PATH_GENERAL_RESTRICTION_MODE = 'general/restriction/mode';
  182. /**
  183. * XML path to config secure base link URL
  184. *
  185. * @var string
  186. */
  187. const XML_PATH_SECURE_BASE_LINK_URL = 'web/secure/base_link_url';
  188. /**
  189. * XML path to config for paypal business account
  190. *
  191. * @var string
  192. */
  193. const XML_PATH_PAYPAL_BUSINESS_ACCOUNT = 'paypal/general/business_account';
  194. /**
  195. * XML path to config for default cache time
  196. *
  197. * @var string
  198. */
  199. const XML_PATH_DEFAULT_CACHE_LIFETIME = 'xmlconnect/mobile_application/cache_lifetime';
  200. /**
  201. * Initialize application
  202. *
  203. * @return void
  204. */
  205. protected function _construct()
  206. {
  207. $this->_init('xmlconnect/application');
  208. }
  209. /**
  210. * Checks is it app is submitted
  211. * (edit is premitted only before submission)
  212. *
  213. * @return bool
  214. */
  215. public function getIsSubmitted()
  216. {
  217. return $this->getStatus() == Mage_XmlConnect_Model_Application::APP_STATUS_SUCCESS;
  218. }
  219. /**
  220. * Load data (flat array) for Varien_Data_Form
  221. *
  222. * @return array
  223. */
  224. public function getFormData()
  225. {
  226. $data = $this->getData();
  227. $data = Mage::helper('xmlconnect')->getDeviceHelper()->checkImages($data);
  228. return $this->_flatArray($data);
  229. }
  230. /**
  231. * Load data (flat array) for Varien_Data_Form
  232. *
  233. * @param array $subtree
  234. * @param string $prefix
  235. * @return array
  236. */
  237. protected function _flatArray($subtree, $prefix=null)
  238. {
  239. $result = array();
  240. foreach ($subtree as $key => $value) {
  241. if (is_null($prefix)) {
  242. $name = $key;
  243. } else {
  244. $name = $prefix . '[' . $key . ']';
  245. }
  246. if (is_array($value)) {
  247. $result = array_merge($result, $this->_flatArray($value, $name));
  248. } else {
  249. $result[$name] = $value;
  250. }
  251. }
  252. return $result;
  253. }
  254. /**
  255. * Like array_merge_recursive(), but string values is replaced
  256. *
  257. * @param array $a
  258. * @param array $b
  259. * @return array
  260. */
  261. protected function _configMerge(array $a, array $b)
  262. {
  263. $result = array();
  264. $keys = array_unique(array_merge(array_keys($a), array_keys($b)));
  265. foreach ($keys as $key) {
  266. if (!isset($a[$key])) {
  267. $result[$key] = $b[$key];
  268. } elseif (!isset($b[$key])) {
  269. $result[$key] = $a[$key];
  270. } elseif (is_scalar($a[$key]) || is_scalar($b[$key])) {
  271. $result[$key] = $b[$key];
  272. } else {
  273. $result[$key] = $this->_configMerge($a[$key], $b[$key]);
  274. }
  275. }
  276. return $result;
  277. }
  278. /**
  279. * Set default configuration data
  280. *
  281. * @return void
  282. */
  283. public function loadDefaultConfiguration()
  284. {
  285. $this->setCode($this->getCodePrefix());
  286. $this->setConf(Mage::helper('xmlconnect')->getDeviceHelper()->getDefaultConfiguration());
  287. }
  288. /**
  289. * Return first part for application code field
  290. *
  291. * @return string
  292. */
  293. public function getCodePrefix()
  294. {
  295. return substr(Mage::app()->getStore($this->getStoreId())->getCode(), 0, self::APP_PREFIX_CUT_LENGTH)
  296. . substr($this->getType(), 0, self::APP_PREFIX_CUT_LENGTH);
  297. }
  298. /**
  299. * Checks if application code field has autoincrement
  300. *
  301. * @return bool
  302. */
  303. public function isCodePrefixed()
  304. {
  305. $suffix = substr($this->getCode(), self::APP_PREFIX_CUT_LENGTH * 2);
  306. return !empty($suffix);
  307. }
  308. /**
  309. * Load application configuration
  310. *
  311. * @return array
  312. */
  313. public function prepareConfiguration()
  314. {
  315. return $this->getData('conf');
  316. }
  317. /**
  318. * Get config formatted for rendering
  319. *
  320. * @return array
  321. */
  322. public function getRenderConf()
  323. {
  324. $result = Mage::helper('xmlconnect')->getDeviceHelper()->getDefaultConfiguration();
  325. $result = $result['native'];
  326. $extra = array();
  327. if (isset($this->_data['conf'])) {
  328. if (isset($this->_data['conf']['native'])) {
  329. $result = $this->_configMerge($result, $this->_data['conf']['native']);
  330. }
  331. if (isset($this->_data['conf']['extra'])) {
  332. $extra = $this->_data['conf']['extra'];
  333. if (isset($extra['tabs'])) {
  334. $tabs = Mage::getModel('xmlconnect/tabs', $extra['tabs']);
  335. $result['tabBar']['tabs'] = $tabs;
  336. }
  337. if (isset($extra['fontColors'])) {
  338. if (!empty($extra['fontColors']['header'])) {
  339. $result['fonts']['Title1']['color'] = $extra['fontColors']['header'];
  340. }
  341. if (!empty($extra['fontColors']['primary'])) {
  342. $result['fonts']['Title2']['color'] = $extra['fontColors']['primary'];
  343. $result['fonts']['Title3']['color'] = $extra['fontColors']['primary'];
  344. $result['fonts']['Text1']['color'] = $extra['fontColors']['primary'];
  345. $result['fonts']['Text2']['color'] = $extra['fontColors']['primary'];
  346. $result['fonts']['Title7']['color'] = $extra['fontColors']['primary'];
  347. }
  348. if (!empty($extra['fontColors']['secondary'])) {
  349. $result['fonts']['Title4']['color'] = $extra['fontColors']['secondary'];
  350. $result['fonts']['Title6']['color'] = $extra['fontColors']['secondary'];
  351. $result['fonts']['Title8']['color'] = $extra['fontColors']['secondary'];
  352. $result['fonts']['Title9']['color'] = $extra['fontColors']['secondary'];
  353. }
  354. if (!empty($extra['fontColors']['price'])) {
  355. $result['fonts']['Title5']['color'] = $extra['fontColors']['price'];
  356. }
  357. }
  358. }
  359. }
  360. /** @var $helperImage Mage_XmlConnect_Helper_Image */
  361. $helperImage = Mage::helper('xmlconnect/image');
  362. $paths = $helperImage->getInterfaceImagesPathsConf();
  363. foreach ($paths as $confPath => $dataPath) {
  364. $imageNodeValue =& $helperImage->findPath($result, $dataPath);
  365. if ($imageNodeValue) {
  366. if (!file_exists($imageNodeValue)) {
  367. /**
  368. * We set empty string to get default image if original was missing in some reason
  369. */
  370. $imageNodeValue = '';
  371. } else {
  372. /**
  373. * Creating file ending (some_inner/some_dir/filename.png) For url
  374. */
  375. $imageNodeValue = $helperImage->getFileCustomDirSuffixAsUrl($confPath, $imageNodeValue);
  376. }
  377. }
  378. }
  379. $result = $this->_absPath($result);
  380. /**
  381. * General configuration
  382. $resulI = $this->_absPath($result);
  383. */
  384. $result['general']['updateTimeUTC'] = strtotime($this->getUpdatedAt());
  385. $result['general']['browsingMode'] = $this->getBrowsingMode();
  386. $result['general']['currencyCode'] = Mage::app()->getStore($this->getStoreId())->getDefaultCurrencyCode();
  387. $result['general']['secureBaseUrl'] = Mage::getStoreConfig(
  388. self::XML_PATH_SECURE_BASE_LINK_URL,
  389. $this->getStoreId()
  390. );
  391. $maxRecipients = 0;
  392. $allowGuest = 0;
  393. if (Mage::getStoreConfig(Mage_Sendfriend_Helper_Data::XML_PATH_ENABLED)) {
  394. $maxRecipients = Mage::getStoreConfig(Mage_Sendfriend_Helper_Data::XML_PATH_MAX_RECIPIENTS);
  395. $allowGuest = Mage::getStoreConfig(Mage_Sendfriend_Helper_Data::XML_PATH_ALLOW_FOR_GUEST);
  396. }
  397. $result['general']['emailToFriendMaxRecepients'] = $maxRecipients;
  398. $result['general']['emailAllowGuest'] = $allowGuest;
  399. $result['general']['primaryStoreLang'] = Mage::app()
  400. ->getStore($this->getStoreId())->getConfig(Mage_Core_Model_Locale::XML_PATH_DEFAULT_LOCALE);
  401. $result['general']['magentoVersion'] = Mage::getVersion();
  402. $result['general']['copyright'] = Mage::getStoreConfig(
  403. self::XML_PATH_DESIGN_FOOTER_COPYRIGHT,
  404. $this->getStoreId()
  405. );
  406. $result['general']['isAllowedGuestCheckout'] = Mage::getSingleton('checkout/session')
  407. ->getQuote()->isAllowedGuestCheckout();
  408. /**
  409. * Check is guest can post product reviews
  410. */
  411. if (Mage::helper('review')->getIsGuestAllowToWrite()) {
  412. $result['general']['isAllowedGuestReview'] = '1';
  413. } else {
  414. $result['general']['isAllowedGuestReview'] = '0';
  415. }
  416. /**
  417. * Check is wishlist enabled in a config
  418. */
  419. if (Mage::getStoreConfigFlag('wishlist/general/active')) {
  420. $result['general']['wishlistEnable'] = '1';
  421. } else {
  422. $result['general']['wishlistEnable'] = '0';
  423. }
  424. /**
  425. * "Use Secure URLs in Frontend" flag
  426. */
  427. $result['general']['useSecureURLInFrontend'] = (int)Mage::getStoreConfigFlag(
  428. Mage_Core_Model_Store::XML_PATH_SECURE_IN_FRONTEND
  429. );
  430. /**
  431. * PayPal configuration
  432. */
  433. $result['paypal']['businessAccount'] = Mage::getModel('paypal/config')->businessAccount;
  434. $result['paypal']['merchantLabel'] = $this->getData('conf/special/merchantLabel');
  435. $isActive = 0;
  436. if (isset($result['paypal']) && isset($result['paypal']['isActive'])) {
  437. $paypalMep = Mage::getModel('xmlconnect/payment_method_paypal_mep');
  438. $isActive = (int)($result['paypal']['isActive'] && $paypalMep->isAvailable(null));
  439. }
  440. $result['paypal']['isActive'] = $isActive;
  441. if ((int)Mage::getStoreConfig(self::XML_PATH_GENERAL_RESTRICTION_IS_ACTIVE)) {
  442. $result['website_restrictions']['mode'] = (int)Mage::getStoreConfig(
  443. self::XML_PATH_GENERAL_RESTRICTION_MODE
  444. );
  445. }
  446. return $result;
  447. }
  448. /**
  449. * Return current screen_size parameter
  450. *
  451. * @return string
  452. */
  453. public function getScreenSize()
  454. {
  455. if (!isset($this->_data['screen_size'])) {
  456. $this->_data['screen_size'] = self::APP_SCREEN_SIZE_DEFAULT;
  457. }
  458. return $this->_data['screen_size'];
  459. }
  460. /**
  461. * Setter
  462. * for current screen_size parameter
  463. *
  464. * @param string $screenSize
  465. * @return this
  466. */
  467. public function setScreenSize($screenSize)
  468. {
  469. $this->_data['screen_size'] = Mage::helper('xmlconnect/image')->filterScreenSize((string) $screenSize);
  470. return $this;
  471. }
  472. /**
  473. * Return Enabled Tabs array from actual config
  474. *
  475. * @return array:
  476. */
  477. public function getEnabledTabsArray()
  478. {
  479. if ($this->getData('conf/extra/tabs')) {
  480. return Mage::getModel('xmlconnect/tabs', $this->getData('conf/extra/tabs'))->getRenderTabs();
  481. }
  482. return array();
  483. }
  484. /**
  485. * Change URLs to absolute
  486. *
  487. * @param array $subtree
  488. * @return array
  489. */
  490. protected function _absPath($subtree)
  491. {
  492. foreach ($subtree as $key => $value) {
  493. if (!empty($value)) {
  494. if (is_array($value)) {
  495. $subtree[$key] = $this->_absPath($value);
  496. } elseif ((substr($key, -4) == 'icon') ||
  497. (substr($key, -4) == 'Icon') ||
  498. (substr($key, -5) == 'Image')) {
  499. $subtree[$key] = Mage::getBaseUrl('media') . 'xmlconnect/' . $value;
  500. }
  501. }
  502. }
  503. return $subtree;
  504. }
  505. /**
  506. * Return content pages
  507. *
  508. * @return array
  509. */
  510. public function getPages()
  511. {
  512. if (isset($this->_data['conf']['native']['pages'])) {
  513. return $this->_data['conf']['native']['pages'];
  514. }
  515. return array();
  516. }
  517. /**
  518. * Processing object before save data
  519. *
  520. * @return Mage_XmlConnect_Model_Application
  521. */
  522. protected function _beforeSave()
  523. {
  524. $conf = serialize($this->prepareConfiguration());
  525. $this->setConfiguration($conf);
  526. $this->setUpdatedAt(date('Y-m-d H:i:s', time()));
  527. return $this;
  528. }
  529. /**
  530. * Load configuration data (from serialized blob)
  531. *
  532. * @return Mage_XmlConnect_Model_Application
  533. */
  534. public function loadConfiguration()
  535. {
  536. static $isConfigurationLoaded = null;
  537. if (is_null($isConfigurationLoaded)) {
  538. $configuration = $this->getConfiguration();
  539. if (!empty($configuration)) {
  540. $configuration = unserialize($configuration);
  541. $this->setData('conf', $configuration);
  542. $isConfigurationLoaded = true;
  543. }
  544. }
  545. return $this;
  546. }
  547. /**
  548. * Load application by code
  549. *
  550. * @param string $code
  551. * @return Mage_XmlConnect_Model_Application
  552. */
  553. public function loadByCode($code)
  554. {
  555. $this->_getResource()->load($this, $code, 'code');
  556. return $this;
  557. }
  558. /**
  559. * Loads submit tab data from xmlconnect/history table
  560. *
  561. * @return bool
  562. */
  563. public function loadSubmit()
  564. {
  565. $isResubmitAction = false;
  566. if ($this->getId()) {
  567. $params = $this->getLastParams();
  568. if (!empty($params)) {
  569. // Using Pointer !
  570. $conf = &$this->_data['conf'];
  571. if (!isset($conf['submit_text']) || !is_array($conf['submit_text'])) {
  572. $conf['submit_text'] = array();
  573. }
  574. if (!isset($conf['submit_restore']) || !is_array($conf['submit_restore'])) {
  575. $conf['submit_restore'] = array();
  576. }
  577. foreach ($params as $id => $value) {
  578. $deviceImages = Mage::helper('xmlconnect')
  579. ->getDeviceHelper()
  580. ->getSubmitImages();
  581. if (!in_array($id, $deviceImages)) {
  582. $conf['submit_text'][$id] = $value;
  583. } else {
  584. $conf['submit_restore'][$id] = $value;
  585. }
  586. $isResubmitAction = true;
  587. }
  588. }
  589. }
  590. $this->setIsResubmitAction($isResubmitAction);
  591. return $isResubmitAction;
  592. }
  593. /**
  594. * Returns ( image[ ID ] => "SRC" ) array
  595. *
  596. * @return array
  597. */
  598. public function getImages()
  599. {
  600. $images = array();
  601. $params = $this->getLastParams();
  602. $deviceImages = Mage::helper('xmlconnect')
  603. ->getDeviceHelper()
  604. ->getSubmitImages();
  605. foreach ($deviceImages as $id) {
  606. $path = $this->getData('conf/submit/'.$id);
  607. $basename = null;
  608. if (!empty($path)) {
  609. /**
  610. * Fetching data from session restored array
  611. */
  612. $basename = basename($path);
  613. } else if (isset($params[$id])) {
  614. /**
  615. * Fetching data from submission history table record
  616. *
  617. * converting : "@\var\somedir\media\xmlconnect\form_icon_6.png"
  618. * to "\var\somedir\media\xmlconnect\forn_icon_6.png"
  619. */
  620. $basename = basename($params[$id]);
  621. }
  622. if (!empty($basename)) {
  623. $images['conf/submit/'.$id] = Mage::getBaseUrl('media').'xmlconnect/'
  624. . Mage::helper('xmlconnect/image')->getFileDefaultSizeSuffixAsUrl($basename);
  625. }
  626. }
  627. return $images;
  628. }
  629. /**
  630. * Return last submitted data from history table
  631. *
  632. * @return array
  633. */
  634. public function getLastParams()
  635. {
  636. if (!isset($this->_lastParams)) {
  637. $this->_lastParams = Mage::getModel('xmlconnect/history')->getLastParams($this->getId());
  638. }
  639. return $this->_lastParams;
  640. }
  641. /**
  642. * Validate application data
  643. *
  644. * @return array|bool
  645. */
  646. public function validate()
  647. {
  648. $errors = array();
  649. $validateConf = $this->_validateConf();
  650. if ($validateConf !== true) {
  651. $errors = $validateConf;
  652. }
  653. if (!Zend_Validate::is($this->getName(), 'NotEmpty')) {
  654. $errors[] = Mage::helper('xmlconnect')->__('Please enter "App Title".');
  655. }
  656. if (empty($errors)) {
  657. return true;
  658. }
  659. return $errors;
  660. }
  661. /**
  662. * Validate submit application data
  663. *
  664. * @param array $params
  665. * @return array|bool
  666. */
  667. public function validateSubmit($params)
  668. {
  669. $errors = array();
  670. $validateConf = $this->_validateConf();
  671. if ($validateConf !== true) {
  672. $errors = $validateConf;
  673. }
  674. $submitErrors = Mage::helper('xmlconnect')->getDeviceHelper($this)->validateSubmit($params);
  675. if (count($submitErrors)) {
  676. $errors = array_merge($errors, $submitErrors);
  677. }
  678. if (empty($errors)) {
  679. return true;
  680. }
  681. return $errors;
  682. }
  683. /**
  684. * Check config for valid values
  685. *
  686. * @return bool|array
  687. */
  688. protected function _validateConf()
  689. {
  690. $conf = $this->getConf();
  691. $native = isset($conf['native']) && is_array($conf['native']) ? $conf['native'] : false;
  692. $errors = Mage::helper('xmlconnect')->getDeviceHelper($this)->validateConfig($native);
  693. foreach ($this->_socialNetValidationArray as $networkKey) {
  694. if (isset($native['socialNetworking'][$networkKey]['isActive'])
  695. && $native['socialNetworking'][$networkKey]['isActive']
  696. ) {
  697. if ($networkKey !== Mage_XmlConnect_Helper_Data::SOCIAL_NETWORK_FACEBOOK) {
  698. $networkName = ucfirst($networkKey);
  699. if (!isset($native['socialNetworking'][$networkKey]['apiKey'])
  700. || !Zend_Validate::is($native['socialNetworking'][$networkKey]['apiKey'], 'NotEmpty')
  701. ) {
  702. $errors[] = Mage::helper('xmlconnect')->__('%s API Key required.', $networkName);
  703. }
  704. if (!isset($native['socialNetworking'][$networkKey]['secretKey'])
  705. || !Zend_Validate::is($native['socialNetworking'][$networkKey]['secretKey'], 'NotEmpty')
  706. ) {
  707. $errors[] = Mage::helper('xmlconnect')->__('%s Secret Key required.', $networkName);
  708. }
  709. } else {
  710. $networkName = ucfirst($networkKey);
  711. if (!isset($native['socialNetworking'][$networkKey]['appID'])
  712. || !Zend_Validate::is($native['socialNetworking'][$networkKey]['appID'], 'NotEmpty')
  713. ) {
  714. $errors[] = Mage::helper('xmlconnect')->__('%s Application ID required.', $networkName);
  715. }
  716. }
  717. }
  718. }
  719. if (empty($errors)) {
  720. return true;
  721. }
  722. return $errors;
  723. }
  724. /**
  725. * Imports post/get data into the model
  726. *
  727. * @param array $data - $_REQUEST[]
  728. * @return array
  729. */
  730. public function prepareSubmitParams($data)
  731. {
  732. $params = array();
  733. if (isset($data['conf']) && is_array($data['conf'])) {
  734. if (isset($data['conf']['submit_text']) && is_array($data['conf']['submit_text'])) {
  735. $params = $data['conf']['submit_text'];
  736. }
  737. $params['name'] = $this->getName();
  738. $params['code'] = $this->getCode();
  739. $params['type'] = $this->getType();
  740. $params['url'] = Mage::getUrl('xmlconnect/configuration/index', array(
  741. '_store' => $this->getStoreId(),
  742. '_nosid' => true,
  743. 'app_code' => $this->getCode()
  744. ));
  745. $params['magentoversion'] = Mage::getVersion();
  746. if (isset($params['country']) && is_array($params['country'])) {
  747. $params['country'] = implode(',', $params['country']);
  748. }
  749. if ($this->getIsResubmitAction()) {
  750. if (isset($params['resubmission_activation_key'])) {
  751. $params['resubmission_activation_key'] = trim($params['resubmission_activation_key']);
  752. $params['key'] = $params['resubmission_activation_key'];
  753. } else {
  754. $params['key'] = '';
  755. }
  756. } else {
  757. $params['key'] = isset($params['key']) ? trim($params['key']) : '';
  758. }
  759. // processing files :
  760. $submit = array();
  761. if (isset($this->_data['conf']['submit']) && is_array($this->_data['conf']['submit'])) {
  762. $submit = $this->_data['conf']['submit'];
  763. }
  764. $submitRestore = array();
  765. if (isset($this->_data['conf']['submit_restore']) && is_array($this->_data['conf']['submit_restore'])) {
  766. $submitRestore = $this->_data['conf']['submit_restore'];
  767. }
  768. $deviceImages = Mage::helper('xmlconnect')
  769. ->getDeviceHelper()
  770. ->getSubmitImages();
  771. foreach ($deviceImages as $id) {
  772. if (isset($submit[$id])) {
  773. $params[$id] = '@' . $submit[$id];
  774. } else if (isset($submitRestore[$id])) {
  775. $params[$id] = $submitRestore[$id];
  776. }
  777. }
  778. }
  779. $this->setSubmitParams($params);
  780. return $params;
  781. }
  782. /**
  783. * Retrieve Store Id
  784. *
  785. * @return int
  786. */
  787. public function getStoreId()
  788. {
  789. if ($this->hasData('store_id')) {
  790. return $this->getData('store_id');
  791. }
  792. return Mage::app()->getStore()->getId();
  793. }
  794. /**
  795. * Getter, returns activation key for current application
  796. *
  797. * @return string|null
  798. */
  799. public function getActivationKey()
  800. {
  801. $key = null;
  802. if (isset($this->_data['conf']) && is_array($this->_data['conf']) &&
  803. isset($this->_data['conf']['submit_text']) && is_array($this->_data['conf']['submit_text']) &&
  804. isset($this->_data['conf']['submit_text']['key'])) {
  805. $key = $this->_data['conf']['submit_text']['key'];
  806. }
  807. return $key;
  808. }
  809. /**
  810. * Perform update for all applications "updated at" parameter with current date
  811. *
  812. * @return Mage_XmlConnect_Model_Application
  813. */
  814. public function updateAllAppsUpdatedAtParameter()
  815. {
  816. $this->_getResource()->updateAllAppsUpdatedAtParameter();
  817. return $this;
  818. }
  819. /**
  820. * Getter return concatenated user and password
  821. *
  822. * @return string
  823. */
  824. public function getUserpwd()
  825. {
  826. return $this->loadConfiguration()->getAppKey() . ':' . $this->getAppMasterSecret();
  827. }
  828. /**
  829. * Getter for Application Key
  830. *
  831. * @return string
  832. */
  833. public function getAppKey()
  834. {
  835. return $this->getData('conf/native/notifications/applicationKey');
  836. }
  837. /**
  838. * Getter for Application Secret
  839. *
  840. * @return string
  841. */
  842. public function getAppSecret()
  843. {
  844. return $this->getData('conf/native/notifications/applicationSecret');
  845. }
  846. /**
  847. * Getter for Application Master Secret
  848. *
  849. * @return string
  850. */
  851. public function getAppMasterSecret()
  852. {
  853. return $this->getData('conf/native/notifications/applicationMasterSecret');
  854. }
  855. /**
  856. * Getter for Application Cache Lifetime
  857. *
  858. * @return int|string
  859. */
  860. public function getCacheLifetime()
  861. {
  862. $lifetime = (int)$this->loadConfiguration()->getData('conf/native/cacheLifetime');
  863. return $lifetime <= 0 ? '' : $lifetime;
  864. }
  865. }