From b023f191e3e36fece00fd7209aea0c7bf0c17fe6 Mon Sep 17 00:00:00 2001 From: drigato Date: Thu, 14 May 2015 13:13:33 -0400 Subject: [PATCH] SAAS-772: Send metadata to Tunein Added form validation by making a test request to the TuneIn API with the user-entered values --- .../application/forms/TuneInPreferences.php | 54 +++++++++++++++---- .../scripts/form/preferences_tunein.phtml | 8 ++- airtime_mvc/public/css/styles.css | 4 ++ .../js/airtime/preferences/preferences.js | 4 ++ 4 files changed, 60 insertions(+), 10 deletions(-) diff --git a/airtime_mvc/application/forms/TuneInPreferences.php b/airtime_mvc/application/forms/TuneInPreferences.php index d97a557e9..5eb729aca 100644 --- a/airtime_mvc/application/forms/TuneInPreferences.php +++ b/airtime_mvc/application/forms/TuneInPreferences.php @@ -10,35 +10,71 @@ class Application_Form_TuneInPreferences extends Zend_Form_SubForm )); $enableTunein = new Zend_Form_Element_Checkbox("enable_tunein"); + $enableTunein->setDecorators(array( + 'ViewHelper', + 'Errors', + 'Label' + )); + $enableTunein->addDecorator('Label', array('class' => 'enable-tunein')); $enableTunein->setLabel(_("Push metadata to your station on TuneIn?")); $enableTunein->setValue(Application_Model_Preference::getTuneinEnabled()); $enableTunein->setAttrib("class", "block-display"); $this->addElement($enableTunein); - // TODO: figure out how to make this validator work - $validator = new ConditionalNotEmpty(array( - 'enable_tunein' => 1 - )); - $tuneinStationId = new Zend_Form_Element_Text("tunein_station_id"); $tuneinStationId->setLabel(_("Station ID:")); $tuneinStationId->setValue(Application_Model_Preference::getTuneinStationId()); $tuneinStationId->setAttrib("class", "input_text"); - $tuneinStationId->addValidator($validator); $this->addElement($tuneinStationId); $tuneinPartnerKey = new Zend_Form_Element_Text("tunein_partner_key"); $tuneinPartnerKey->setLabel(_("Partner Key:")); $tuneinPartnerKey->setValue(Application_Model_Preference::getTuneinPartnerKey()); $tuneinPartnerKey->setAttrib("class", "input_text"); - $tuneinPartnerKey->addValidator($validator); $this->addElement($tuneinPartnerKey); $tuneinPartnerId = new Zend_Form_Element_Text("tunein_partner_id"); $tuneinPartnerId->setLabel(_("Partner Id:")); $tuneinPartnerId->setValue(Application_Model_Preference::getTuneinPartnerId()); $tuneinPartnerId->setAttrib("class", "input_text"); - $tuneinPartnerId->addValidator($validator); $this->addElement($tuneinPartnerId); } -} \ No newline at end of file + + public function isValid($data) + { + // Make request to TuneIn API to test the settings are valid + if ($data["enable_tunein"]) { + $qry_str = "?partnerId=".$data["tunein_partner_id"]."&partnerKey=".$data["tunein_partner_key"]."&id=".$data["tunein_station_id"] + ."&title=&artist="; + + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, TUNEIN_API_URL . $qry_str); + curl_setopt($ch, CURLOPT_FAILONERROR, 1); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($ch, CURLOPT_TIMEOUT, 30); + + $jsonData = curl_exec($ch); + if (curl_error($ch)) { + Logging::error("Failed to reach TuneIn: ". curl_errno($ch)." - ". curl_error($ch) . " - " . curl_getinfo($ch, CURLINFO_EFFECTIVE_URL)); + if (curl_error($ch) == "The requested URL returned error: 403 Forbidden") { + $this->getElement("enable_tunein")->setErrors(array(_("Invalid TuneIn Settings. Please ensure your TuneIn settings are correct and try again."))); + + // Set values to what the user entered since the form is invalid so they + // don't have to enter in the values again and can see what they entered. + $this->getElement("enable_tunein")->setValue($data["enable_tunein"]); + $this->getElement("tunein_partner_key")->setValue($data["tunein_partner_key"]); + $this->getElement("tunein_partner_id")->setValue($data["tunein_partner_id"]); + $this->getElement("tunein_station_id")->setValue($data["tunein_station_id"]); + + return false; + } + } + curl_close($ch); + + $arr = json_decode($jsonData, true); + Logging::info($arr); + } else { + return true; + } + } +} diff --git a/airtime_mvc/application/views/scripts/form/preferences_tunein.phtml b/airtime_mvc/application/views/scripts/form/preferences_tunein.phtml index a3cdb0d83..f7ae2bc84 100644 --- a/airtime_mvc/application/views/scripts/form/preferences_tunein.phtml +++ b/airtime_mvc/application/views/scripts/form/preferences_tunein.phtml @@ -1,6 +1,12 @@
- element->getElement('enable_tunein')->render() ?> + + element->getElement("enable_tunein")->hasErrors()) { + echo $this->element->getElement('enable_tunein')->renderErrors(); + } + ?> + element->getElement('enable_tunein')->renderViewHelper() ?> + element->getElement('enable_tunein')->renderLabel() ?> element->getElement('tunein_station_id')->render() ?> diff --git a/airtime_mvc/public/css/styles.css b/airtime_mvc/public/css/styles.css index b80cbfe8e..8fc43eda6 100644 --- a/airtime_mvc/public/css/styles.css +++ b/airtime_mvc/public/css/styles.css @@ -3248,3 +3248,7 @@ dd .stream-status { padding-bottom: 0px; padding-top: 13px; } + +.enable-tunein { + font-weight:bold; +} \ No newline at end of file diff --git a/airtime_mvc/public/js/airtime/preferences/preferences.js b/airtime_mvc/public/js/airtime/preferences/preferences.js index d1aea4091..0491d335c 100644 --- a/airtime_mvc/public/js/airtime/preferences/preferences.js +++ b/airtime_mvc/public/js/airtime/preferences/preferences.js @@ -152,6 +152,10 @@ $(document).ready(function() { return false; }).next().hide(); + if ($("#tunein-settings").find(".errors").length > 0) { + $(".collapsible-content#tunein-settings").show(); + } + /* No longer using AJAX for this form. Zend + our code makes it needlessly hard to deal with. -- Albert $('#pref_save').live('click', function() { var data = $('#pref_form').serialize();