PageRenderTime 59ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/core/Mage/XmlConnect/Helper/Data.php

https://bitbucket.org/andrewjleavitt/magestudy
PHP | 852 lines | 481 code | 73 blank | 298 comment | 56 complexity | 4225a23f480ad640959d68a6686954e4 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. class Mage_XmlConnect_Helper_Data extends Mage_Core_Helper_Abstract
  27. {
  28. /**
  29. * Push title length
  30. *
  31. * @var int
  32. */
  33. const PUSH_TITLE_LENGTH = 140;
  34. /**
  35. * Message title length
  36. *
  37. * @var int
  38. */
  39. const MESSAGE_TITLE_LENGTH = 255;
  40. /**
  41. * List of the keys for xml config that have to be excluded form application config
  42. *
  43. * @var array
  44. */
  45. protected $_excludedXmlConfigKeys = array(
  46. 'notifications/applicationMasterSecret',
  47. );
  48. /**
  49. * Application names array
  50. *
  51. * @var array
  52. */
  53. protected $_appNames = array();
  54. /**
  55. * Template names array
  56. *
  57. * @var array
  58. */
  59. protected $_tplNames = array();
  60. /**
  61. * XML path to nodes to be excluded
  62. *
  63. * @var string
  64. */
  65. const XML_NODE_CONFIG_EXCLUDE_FROM_XML = 'xmlconnect/mobile_application/nodes_excluded_from_config_xml';
  66. /**
  67. * Default device type
  68. *
  69. * @var string
  70. */
  71. const DEVICE_TYPE_DEFAULT = 'unknown';
  72. /**
  73. * iPhone device identifier
  74. *
  75. * @var string
  76. */
  77. const DEVICE_TYPE_IPHONE = 'iphone';
  78. /**
  79. * iPad device identifier
  80. *
  81. * @var string
  82. */
  83. const DEVICE_TYPE_IPAD = 'ipad';
  84. /**
  85. * Android device identifier
  86. *
  87. * @var string
  88. */
  89. const DEVICE_TYPE_ANDROID = 'android';
  90. /**
  91. * Social network Twitter id
  92. *
  93. * @var string
  94. */
  95. const SOCIAL_NETWORK_TWITTER = 'twitter';
  96. /**
  97. * Social network Facebook id
  98. *
  99. * @var string
  100. */
  101. const SOCIAL_NETWORK_FACEBOOK = 'facebook';
  102. /**
  103. * Social network LinkedIn id
  104. *
  105. * @var string
  106. */
  107. const SOCIAL_NETWORK_LINKEDIN = 'linkedin';
  108. /**
  109. * Get device preview model
  110. *
  111. * @throws Mage_Core_Exception
  112. * @return Mage_XmlConnect_Model_Preview_Abstract
  113. */
  114. public function getPreviewModel()
  115. {
  116. $deviceType = $this->getDeviceType();
  117. switch ($deviceType) {
  118. case self::DEVICE_TYPE_IPHONE:
  119. case self::DEVICE_TYPE_IPAD:
  120. case self::DEVICE_TYPE_ANDROID:
  121. $previewModel = Mage::getSingleton('xmlconnect/preview_' . strtolower($deviceType));
  122. break;
  123. default:
  124. Mage::throwException(
  125. Mage::helper('xmlconnect')->__('Device doesn\'t recognized: "%s". Unable to load preview model.', $deviceType)
  126. );
  127. break;
  128. }
  129. return $previewModel;
  130. }
  131. /**
  132. * Get device helper
  133. *
  134. * @throws Mage_Core_Exception
  135. * @param Mage_XmlConnect_Model_Application $application
  136. * @return Mage_Core_Helper_Abstract
  137. */
  138. public function getDeviceHelper($application = null)
  139. {
  140. $deviceType = $this->getDeviceType($application);
  141. switch ($deviceType) {
  142. case self::DEVICE_TYPE_IPHONE:
  143. case self::DEVICE_TYPE_IPAD:
  144. case self::DEVICE_TYPE_ANDROID:
  145. $helper = Mage::helper('xmlconnect/' . $deviceType);
  146. break;
  147. default:
  148. Mage::throwException(
  149. Mage::helper('xmlconnect')->__('Device doesn\'t recognized: "%s". Unable to load a helper.', $deviceType)
  150. );
  151. break;
  152. }
  153. return $helper;
  154. }
  155. /**
  156. * Get device tipe from application
  157. *
  158. * @param Mage_XmlConnect_Model_Application $application
  159. * @return string
  160. */
  161. public function getDeviceType($application = null)
  162. {
  163. $deviceType = null;
  164. if (empty($application) && Mage::registry('current_app') !== null) {
  165. $deviceType = (string) $this->getApplication()->getType();
  166. } elseif ($application instanceof Mage_XmlConnect_Model_Application) {
  167. $deviceType = (string) $application->getType();
  168. }
  169. if (empty($deviceType)) {
  170. $deviceType = self::DEVICE_TYPE_DEFAULT;
  171. }
  172. return $deviceType;
  173. }
  174. /**
  175. * Getter for current loaded application model
  176. *
  177. * @throws Mage_Core_Exception
  178. * @return Mage_XmlConnect_Model_Application
  179. */
  180. public function getApplication()
  181. {
  182. $model = Mage::registry('current_app');
  183. if (!($model instanceof Mage_XmlConnect_Model_Application)) {
  184. Mage::throwException(Mage::helper('xmlconnect')->__('App model not loaded.'));
  185. }
  186. return $model;
  187. }
  188. /**
  189. * Create filter object by key
  190. *
  191. * @param string $key
  192. * @return Mage_Catalog_Model_Layer_Filter_Abstract
  193. */
  194. public function getFilterByKey($key)
  195. {
  196. switch ($key) {
  197. case 'price':
  198. $filterModelName = 'catalog/layer_filter_price';
  199. break;
  200. case 'decimal':
  201. $filterModelName = 'catalog/layer_filter_decimal';
  202. break;
  203. case 'category':
  204. $filterModelName = 'catalog/layer_filter_category';
  205. break;
  206. default:
  207. $filterModelName = 'catalog/layer_filter_attribute';
  208. break;
  209. }
  210. return Mage::getModel($filterModelName);
  211. }
  212. /**
  213. * Export $this->_getUrl() function to public
  214. *
  215. * @param string $route
  216. * @param array $params
  217. * @return array
  218. */
  219. public function getUrl($route, $params = array())
  220. {
  221. return $this->_getUrl($route, $params);
  222. }
  223. /**
  224. * Retrieve device specific country options array
  225. *
  226. * @throws Mage_Core_Exception
  227. * @param bool $isItunes
  228. * @return array
  229. */
  230. public function getCountryOptionsArray($isItunes = false)
  231. {
  232. Varien_Profiler::start('TEST: '.__METHOD__);
  233. $deviceType = $this->getDeviceType();
  234. switch ($deviceType) {
  235. case self::DEVICE_TYPE_IPHONE:
  236. case self::DEVICE_TYPE_IPAD:
  237. $cacheKey = 'XMLCONNECT_COUNTRY_ITUNES_SELECT_STORE_'.Mage::app()->getStore()->getCode();
  238. $deviceCountries = $this->getDeviceHelper()->getItunesCountriesArray();
  239. break;
  240. case self::DEVICE_TYPE_ANDROID:
  241. $cacheKey = 'XMLCONNECT_COUNTRY_ANDROID_SELECT_STORE_'.Mage::app()->getStore()->getCode();
  242. $deviceCountries = $this->getDeviceHelper()->getAndroidMarketCountriesArray();
  243. break;
  244. default:
  245. Mage::throwException(
  246. Mage::helper('xmlconnect')->__('Country options don\'t recognized for "%s".', $deviceType)
  247. );
  248. break;
  249. }
  250. if (Mage::app()->useCache('config') && $cache = Mage::app()->loadCache($cacheKey)) {
  251. $options = unserialize($cache);
  252. } else {
  253. if (isset($deviceCountries)) {
  254. $options = Mage::getModel('directory/country')
  255. ->getResourceCollection()
  256. ->addFieldToFilter('country_id', array('in' => $deviceCountries))
  257. ->loadByStore()
  258. ->toOptionArray(false);
  259. }
  260. if (Mage::app()->useCache('config')) {
  261. Mage::app()->saveCache(serialize($options), $cacheKey, array('config'));
  262. }
  263. }
  264. Varien_Profiler::stop('TEST: '.__METHOD__);
  265. if (count($options)) {
  266. $options[] = array(
  267. 'value' => 'NEW_COUNTRIES',
  268. 'label' => 'New Territories As Added'
  269. );
  270. }
  271. return $options;
  272. }
  273. /**
  274. * Get list of predefined and supported Devices
  275. *
  276. * @return array
  277. */
  278. static public function getSupportedDevices()
  279. {
  280. $devices = array (
  281. self::DEVICE_TYPE_IPAD => Mage::helper('xmlconnect')->__('iPad'),
  282. self::DEVICE_TYPE_IPHONE => Mage::helper('xmlconnect')->__('iPhone'),
  283. self::DEVICE_TYPE_ANDROID => Mage::helper('xmlconnect')->__('Android')
  284. );
  285. return $devices;
  286. }
  287. /**
  288. * Get list of predefined and supported Devices
  289. *
  290. * @return array
  291. */
  292. public function getStatusOptions()
  293. {
  294. $options = array (
  295. Mage_XmlConnect_Model_Application::APP_STATUS_SUCCESS => Mage::helper('xmlconnect')->__('Submitted'),
  296. Mage_XmlConnect_Model_Application::APP_STATUS_INACTIVE => Mage::helper('xmlconnect')->__('Not Submitted'),
  297. );
  298. return $options;
  299. }
  300. /**
  301. * Retrieve supported device types as "html select options"
  302. *
  303. * @return array
  304. */
  305. public function getDeviceTypeOptions()
  306. {
  307. $devices = self::getSupportedDevices();
  308. $options = array();
  309. $options[] = array('value' => '', 'label' => Mage::helper('xmlconnect')->__('Please Select Device Type'));
  310. foreach ($devices as $type => $label) {
  311. $options[] = array('value' => $type, 'label' => $label);
  312. }
  313. return $options;
  314. }
  315. /**
  316. * Get default application tabs
  317. *
  318. * @param string
  319. * @return array
  320. */
  321. public function getDefaultApplicationDesignTabs()
  322. {
  323. return $this->getDeviceHelper()->getDefaultDesignTabs();
  324. }
  325. /**
  326. * Get default Cache Lifetime
  327. *
  328. * @return int
  329. */
  330. public function getDefaultCacheLifetime()
  331. {
  332. return (int)Mage::getStoreConfig(
  333. Mage_XmlConnect_Model_Application::XML_PATH_DEFAULT_CACHE_LIFETIME
  334. );
  335. }
  336. /**
  337. * Return array for tabs like label -> action array
  338. *
  339. * @return array
  340. */
  341. protected function _getTabLabelActionArray()
  342. {
  343. if (!isset($this->_tabLabelActionArray)) {
  344. $this->_tabLabelActionArray = array();
  345. foreach ($this->getDefaultApplicationDesignTabs() as $tab) {
  346. $this->_tabLabelActionArray[$tab['action']] = $tab['label'];
  347. }
  348. }
  349. return $this->_tabLabelActionArray;
  350. }
  351. /**
  352. * Return Translated tab label for given $action
  353. *
  354. * @param string $action
  355. * @return string|bool
  356. */
  357. public function getTabLabel($action)
  358. {
  359. $action = (string) $action;
  360. $tabs = $this->_getTabLabelActionArray();
  361. return (isset($tabs[$action])) ? $tabs[$action] : false;
  362. }
  363. /**
  364. * Merges $changes array to $target array recursive, overwriting existing key, and adding new one
  365. *
  366. * @param mixed $target
  367. * @param mixed $changes
  368. * @return array
  369. */
  370. static public function arrayMergeRecursive($target, $changes)
  371. {
  372. if (!is_array($target)) {
  373. $target = empty($target) ? array() : array($target);
  374. }
  375. if (!is_array($changes)) {
  376. $changes = array($changes);
  377. }
  378. foreach ($changes as $key => $value) {
  379. if (!array_key_exists($key, $target) and !is_numeric($key)) {
  380. $target[$key] = $changes[$key];
  381. continue;
  382. }
  383. if (is_array($value) or is_array($target[$key])) {
  384. $target[$key] = self::arrayMergeRecursive($target[$key], $changes[$key]);
  385. } else if (is_numeric($key)) {
  386. if (!in_array($value, $target)) {
  387. $target[] = $value;
  388. }
  389. } else {
  390. $target[$key] = $value;
  391. }
  392. }
  393. return $target;
  394. }
  395. /**
  396. * Wrap $body with HTML4 headers
  397. *
  398. * @param string $body
  399. * @return string
  400. */
  401. public function htmlize($body)
  402. {
  403. $w3cUrl = 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd';
  404. return <<<EOT
  405. &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  406. &lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Strict//EN&quot; &quot;$w3cUrl&quot;&gt;
  407. &lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot; xml:lang=&quot;en&quot; lang=&quot;en&quot;&gt;
  408. &lt;head&gt;
  409. &lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;style.css&quot; media=&quot;screen&quot;/&gt;
  410. &lt;/head&gt;
  411. &lt;body&gt;$body&lt;/body&gt;&lt;/html&gt;
  412. EOT;
  413. }
  414. /**
  415. * Return select options for xml from array
  416. *
  417. * @param array $dataArray - source array
  418. * @param string $info - selected item
  419. * @return string
  420. */
  421. public function getArrayAsXmlItemValues($dataArray, $selected)
  422. {
  423. $items = array();
  424. foreach ($dataArray as $k => $v) {
  425. if (!$k) {
  426. continue;
  427. }
  428. $items[] = '
  429. <item' . ($k == $selected ? ' selected="1"' : '') . '>
  430. <label>' . $v . '</label>
  431. <value>' . ($k ? $k : '') . '</value>
  432. </item>';
  433. }
  434. $result = implode('', $items);
  435. return $result;
  436. }
  437. /**
  438. * Return Solo Xml optional fieldset
  439. *
  440. * @param string $ssCcMonths
  441. * @param string $ssCcYears
  442. * @return string
  443. */
  444. public function getSoloXml($ssCcMonths, $ssCcYears)
  445. {
  446. $validatorMessage = $this->__('Please enter issue number or start date for switch/solo card type.');
  447. // issue number ==== validate-cc-ukss cvv
  448. $solo = <<<EOT
  449. <fieldset_optional>
  450. <depend_on name="payment[cc_type]">
  451. <show_value>
  452. <item>SS</item>
  453. <item>SM</item>
  454. <item>SO</item>
  455. </show_value>
  456. </depend_on>
  457. <field name="payment[cc_ss_issue]" type="text" label="{$this->__('Issue Number')}">
  458. <validators>
  459. <validator relation="payment[cc_type]" type="credit_card_ukss" message="{$validatorMessage}"/>
  460. </validators>
  461. </field>;
  462. <field name="payment[cc_ss_start_month]" type="select" label="{$this->__('Start Date - Month')}">
  463. <values>
  464. $ssCcMonths
  465. </values>
  466. </field>
  467. <field name="payment[cc_ss_start_year]" type="select" label="{$this->__('Start Date - Year')}">
  468. <values>
  469. $ssCcYears
  470. </values>
  471. </field>
  472. </fieldset_optional>
  473. EOT;
  474. return $solo;
  475. }
  476. /**
  477. * Format price for correct view inside xml strings
  478. *
  479. * @param float $price
  480. * @return string
  481. */
  482. public function formatPriceForXml($price)
  483. {
  484. return sprintf('%01.2F', $price);
  485. }
  486. /**
  487. * Get list of predefined and supported message types
  488. *
  489. * @return array
  490. */
  491. static public function getSupportedMessageTypes()
  492. {
  493. $messages = array (
  494. Mage_XmlConnect_Model_Queue::MESSAGE_TYPE_PUSH => Mage::helper('xmlconnect')->__('Push message'),
  495. Mage_XmlConnect_Model_Queue::MESSAGE_TYPE_AIRMAIL => Mage::helper('xmlconnect')->__('AirMail message'),
  496. );
  497. return $messages;
  498. }
  499. /**
  500. * Retrieve supported message types as "html select options"
  501. *
  502. * @return array
  503. */
  504. public function getMessageTypeOptions()
  505. {
  506. $options = array();
  507. $messages = self::getSupportedMessageTypes();
  508. foreach ($messages as $type => $label) {
  509. $options[] = array('value' => $type, 'label' => $label);
  510. }
  511. return $options;
  512. }
  513. /**
  514. * Get push title length
  515. *
  516. * @return int
  517. */
  518. public function getPushTitleLength()
  519. {
  520. return self::PUSH_TITLE_LENGTH;
  521. }
  522. /**
  523. * Get message title length
  524. *
  525. * @return int
  526. */
  527. public function getMessageTitleLength()
  528. {
  529. return self::MESSAGE_TITLE_LENGTH;
  530. }
  531. /**
  532. * Retrieve applications as "html select options"
  533. *
  534. * @return array
  535. */
  536. public function getApplicationOptions()
  537. {
  538. $options = array();
  539. foreach (Mage::getModel('xmlconnect/application')->getCollection() as $app) {
  540. if (self::isTemplateAllowedForApplication($app)) {
  541. $options[] = array('value' => $app->getCode(), 'label' => $app->getName());
  542. }
  543. }
  544. if (count($options) > 1) {
  545. $options[] = array('value' => '', 'label' => Mage::helper('xmlconnect')->__('Please Select Application'));
  546. }
  547. return $options;
  548. }
  549. /**
  550. * Get applications array like `code` as `name`
  551. *
  552. * @staticvar array $apps
  553. * @return array
  554. */
  555. public function getApplications()
  556. {
  557. static $apps = array();
  558. if (empty($apps)) {
  559. foreach (Mage::getModel('xmlconnect/application')->getCollection() as $app) {
  560. $apps[$app->getCode()] = $app->getName();
  561. }
  562. }
  563. return $apps;
  564. }
  565. /**
  566. * Check if creating AirMail template for the application is allowed
  567. *
  568. * @param Mage_XmlConnect_Model_Application $application
  569. * @return boolean
  570. */
  571. public static function isTemplateAllowedForApplication($application = null)
  572. {
  573. return $application instanceof Mage_XmlConnect_Model_Application ?
  574. in_array($application->getType(), array(self::DEVICE_TYPE_IPHONE)) :
  575. false;
  576. }
  577. /**
  578. * Send broadcast message
  579. *
  580. * @param Mage_XmlConnect_Model_Queue $queue
  581. */
  582. public function sendBroadcastMessage(Mage_XmlConnect_Model_Queue $queue)
  583. {
  584. if ($queue->getStatus() != Mage_XmlConnect_Model_Queue::STATUS_IN_QUEUE) {
  585. return;
  586. }
  587. try {
  588. $appCode = $queue->getAppCode();
  589. $app = Mage::getModel('xmlconnect/application')->load($appCode, 'code');
  590. if (!$app->getId()) {
  591. Mage::throwException(
  592. Mage::helper('xmlconnect')->__('Can\'t load application with code "%s"', $appCode)
  593. );
  594. }
  595. $userpwd = $app->getUserpwd();
  596. $sendType = $queue->getData('type');
  597. switch ($sendType) {
  598. case Mage_XmlConnect_Model_Queue::MESSAGE_TYPE_AIRMAIL:
  599. $configPath = 'xmlconnect/' . Mage_XmlConnect_Model_Queue::MESSAGE_TYPE_AIRMAIL . '/broadcast_url';
  600. $broadcastUrl = Mage::getStoreConfig($configPath);
  601. $params = $queue->getAirmailBroadcastParams();
  602. break;
  603. case Mage_XmlConnect_Model_Queue::MESSAGE_TYPE_PUSH:
  604. default:
  605. $configPath = 'xmlconnect/' . Mage_XmlConnect_Model_Queue::MESSAGE_TYPE_PUSH . '/broadcast_url';
  606. $broadcastUrl = Mage::getStoreConfig($configPath);
  607. $params = $queue->getPushBroadcastParams();
  608. break;
  609. }
  610. $ch = curl_init($broadcastUrl);
  611. $httpHeaders = $this->getHttpHeaders();
  612. curl_setopt($ch, CURLOPT_POST, 1);
  613. curl_setopt($ch, CURLOPT_HTTPHEADER, $httpHeaders);
  614. curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
  615. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  616. curl_setopt($ch, CURLOPT_USERPWD, $userpwd);
  617. curl_setopt($ch, CURLOPT_TIMEOUT, 60);
  618. // Execute the request.
  619. $result = curl_exec($ch);
  620. $succeeded = curl_errno($ch) == 0 ? true : false;
  621. // close cURL resource, and free up system resources
  622. curl_close($ch);
  623. if ($succeeded && (is_null($result) || strtolower($result) == 'null')) {
  624. $queue->setStatus(Mage_XmlConnect_Model_Queue::STATUS_COMPLETED);
  625. }
  626. $queue->setIsSent(true);
  627. return;
  628. } catch (Exception $e) {
  629. Mage::logException($e);
  630. throw $e;
  631. }
  632. }
  633. /**
  634. * Get headers for broadcast message
  635. *
  636. * @return string
  637. */
  638. public function getHttpHeaders()
  639. {
  640. $httpHeaders = array('Content-Type: application/json');
  641. return $httpHeaders;
  642. }
  643. /**
  644. * Remove from array the unnecessary parameters by given keys
  645. *
  646. * @param array $data Source array
  647. * @param array $excludedKeys Keys to be excluded from array. Keys must be in xPath notation
  648. * @return array
  649. */
  650. public function excludeXmlConfigKeys($data, $excludedKeys = array())
  651. {
  652. $excludedKeys = $this->getExcludedXmlConfigKeys();
  653. if (!empty($excludedKeys)) {
  654. foreach ($excludedKeys as $keyToExclude) {
  655. if (strpos($keyToExclude, '/')) {
  656. $keys = array();
  657. foreach (explode('/', $keyToExclude) as $key) {
  658. $key = trim($key);
  659. if (!empty($key)) {
  660. $keys[] = $key;
  661. }
  662. }
  663. if (!empty($keys)) {
  664. $keys = '$data["' . implode('"]["', $keys) . '"]';
  665. eval('if (isset(' . $keys . ')) unset(' . $keys . ');');
  666. }
  667. } elseif (!empty($keyToExclude) && isset($data[$keyToExclude])) {
  668. unset($data[$keyToExclude]);
  669. }
  670. }
  671. }
  672. return $data;
  673. }
  674. /**
  675. * Get excluded keys as array
  676. *
  677. * @return array
  678. */
  679. public function getExcludedXmlConfigKeys()
  680. {
  681. $toExclude = Mage::getStoreConfig(self::XML_NODE_CONFIG_EXCLUDE_FROM_XML);
  682. $nodes = array();
  683. foreach ($toExclude as $value) {
  684. $nodes[] = trim($value, '/');
  685. }
  686. return $nodes;
  687. }
  688. /**
  689. * Returns Application name by it's code
  690. * @param string $appCode
  691. * @return string
  692. */
  693. public function getApplicationName($appCode = null)
  694. {
  695. if (empty($appCode)) {
  696. return '';
  697. }
  698. if (!isset($this->_appNames[$appCode])) {
  699. $app = Mage::getModel('xmlconnect/application')->loadByCode($appCode);
  700. if ($app->getId()) {
  701. $this->_appNames[$appCode] = $app->getName();
  702. } else {
  703. return '';
  704. }
  705. }
  706. return $this->_appNames[$appCode];
  707. }
  708. /**
  709. * Returns Application name by it's code
  710. * @param string $appCode
  711. * @return string
  712. */
  713. public function getTemplateName($templateId = null)
  714. {
  715. if (empty($templateId)) {
  716. return '';
  717. }
  718. if (!isset($this->_tplNames[$templateId])) {
  719. $template = Mage::getModel('xmlconnect/template')->load($templateId);
  720. if ($template->getId()) {
  721. $this->_tplNames[$templateId] = $template->getName();
  722. } else {
  723. return '';
  724. }
  725. }
  726. return $this->_tplNames[$templateId];
  727. }
  728. /**
  729. * Set value into multidimensional array 'conf/native/navigationBar/icon'
  730. *
  731. * @param &array $target // pointer to target array
  732. * @param string $fieldPath // 'conf/native/navigationBar/icon'
  733. * @param mixed $fieldValue // 'Some Value' || 12345 || array(1=>3, 'aa'=>43)
  734. * @param string $delimiter // path delimiter
  735. * @return null
  736. */
  737. public function _injectFieldToArray(&$target, $fieldPath, $fieldValue, $delimiter = '/')
  738. {
  739. $nameParts = explode($delimiter, $fieldPath);
  740. foreach ($nameParts as $next) {
  741. if (!isset($target[$next])) {
  742. $target[$next] = array();
  743. }
  744. $target =& $target[$next];
  745. }
  746. $target = $fieldValue;
  747. return null;
  748. }
  749. /**
  750. * Convert Url link to file path for images
  751. *
  752. * @param string $icon
  753. * @return string
  754. */
  755. public function urlToPath($icon)
  756. {
  757. $baseUrl = Mage::getBaseUrl('media');
  758. $path = str_replace($baseUrl, '', $icon);
  759. $filePath = Mage::getBaseDir('media') . DS . str_replace('/', DS, $path);
  760. return $filePath;
  761. }
  762. /**
  763. * Validate config body field is not empty
  764. *
  765. * @param string $field
  766. * @param array $native
  767. * @return bool
  768. */
  769. public function validateConfFieldNotEmpty($field, $native)
  770. {
  771. if ( ($native === false)
  772. || (!isset($native['body']) || !is_array($native['body'])
  773. || !isset($native['body'][$field])
  774. || !Zend_Validate::is($native['body'][$field], 'NotEmpty'))) {
  775. return false;
  776. }
  777. return true;
  778. }
  779. }