StreamSettingSubForm.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <?php
  2. class Application_Form_StreamSettingSubForm extends Zend_Form_SubForm
  3. {
  4. private $prefix;
  5. private $setting;
  6. private $stream_types;
  7. private $stream_bitrates;
  8. public function init()
  9. {
  10. }
  11. public function setPrefix($prefix)
  12. {
  13. $this->prefix = $prefix;
  14. }
  15. public function setSetting($setting)
  16. {
  17. $this->setting = $setting;
  18. }
  19. public function setStreamTypes($stream_types)
  20. {
  21. $this->stream_types = $stream_types;
  22. }
  23. public function setStreamBitrates($stream_bitrates)
  24. {
  25. $this->stream_bitrates = $stream_bitrates;
  26. }
  27. public function startForm()
  28. {
  29. $prefix = "s".$this->prefix;
  30. $stream_number = $this->prefix;
  31. $setting = $this->setting;
  32. $stream_types = $this->stream_types;
  33. $stream_bitrates = $this->stream_bitrates;
  34. $this->setIsArray(true);
  35. $this->setElementsBelongTo($prefix."_data");
  36. $disable_all = Application_Model_Preference::GetEnableStreamConf() == "false";
  37. $enable = new Zend_Form_Element_Checkbox('enable');
  38. $enable->setLabel(_('Enabled:'))
  39. ->setValue($setting[$prefix.'_enable'] == 'true' ? 1 : 0)
  40. ->setDecorators(array('ViewHelper'));
  41. if ($disable_all) {
  42. $enable->setAttrib("disabled", "disabled");
  43. }
  44. $this->addElement($enable);
  45. $type = new Zend_Form_Element_Select('type');
  46. $type->setLabel(_("Stream Type:"))
  47. ->setMultiOptions($stream_types)
  48. ->setValue(isset($setting[$prefix.'_type'])?$setting[$prefix.'_type']:0)
  49. ->setDecorators(array('ViewHelper'));
  50. if ($disable_all) {
  51. $type->setAttrib("disabled", "disabled");
  52. }
  53. $this->addElement($type);
  54. $bitrate = new Zend_Form_Element_Select('bitrate');
  55. $bitrate->setLabel(_("Bit Rate:"))
  56. ->setMultiOptions($stream_bitrates)
  57. ->setValue(isset($setting[$prefix.'_bitrate'])?$setting[$prefix.'_bitrate']:0)
  58. ->setDecorators(array('ViewHelper'));
  59. if ($disable_all) {
  60. $bitrate->setAttrib("disabled", "disabled");
  61. }
  62. $this->addElement($bitrate);
  63. $output = new Zend_Form_Element_Select('output');
  64. $output->setLabel(_("Service Type:"))
  65. ->setMultiOptions(array("icecast"=>"Icecast", "shoutcast"=>"SHOUTcast"))
  66. ->setValue(isset($setting[$prefix.'_output'])?$setting[$prefix.'_output']:"icecast")
  67. ->setDecorators(array('ViewHelper'));
  68. if ($disable_all) {
  69. $output->setAttrib("disabled", "disabled");
  70. }
  71. $this->addElement($output);
  72. $channels = new Zend_Form_Element_Select('channels');
  73. $channels->setLabel(_("Channels:"))
  74. ->setMultiOptions(array("mono"=>_("1 - Mono"), "stereo"=>_("2 - Stereo")))
  75. ->setValue(isset($setting[$prefix.'_channels']) ? $setting[$prefix.'_channels'] : "stereo")
  76. ->setDecorators(array('ViewHelper'));
  77. if ($disable_all) {
  78. $channels->setAttrib("disabled", "disabled");
  79. }
  80. $this->addElement($channels);
  81. $host = new Zend_Form_Element_Text('host');
  82. $host->setLabel(_("Server"))
  83. ->setValue(isset($setting[$prefix.'_host'])?$setting[$prefix.'_host']:"")
  84. ->setValidators(array(
  85. array('regex', false, array('/^[0-9a-zA-Z-_.]+$/', 'messages' => _('Invalid character entered')))))
  86. ->setDecorators(array('ViewHelper'));
  87. if ($disable_all) {
  88. $host->setAttrib("disabled", "disabled");
  89. }
  90. $host->setAttrib('alt', 'domain');
  91. $this->addElement($host);
  92. $port = new Zend_Form_Element_Text('port');
  93. $port->setLabel(_("Port"))
  94. ->setValue(isset($setting[$prefix.'_port'])?$setting[$prefix.'_port']:"")
  95. ->setValidators(array(new Zend_Validate_Between(array('min'=>0, 'max'=>99999))))
  96. ->addValidator('regex', false, array('pattern'=>'/^[0-9]+$/', 'messages'=>array('regexNotMatch'=>_('Only numbers are allowed.'))))
  97. ->setDecorators(array('ViewHelper'));
  98. if ($disable_all) {
  99. $port->setAttrib("disabled", "disabled");
  100. }
  101. $this->addElement($port);
  102. $pass = new Zend_Form_Element_Text('pass');
  103. $pass->setLabel(_("Password"))
  104. ->setValue(isset($setting[$prefix.'_pass'])?$setting[$prefix.'_pass']:"")
  105. ->setValidators(array(
  106. array('regex', false, array('/^[^ &<>]+$/', 'messages' => _('Invalid character entered')))))
  107. ->setDecorators(array('ViewHelper'));
  108. if ($disable_all) {
  109. $pass->setAttrib("disabled", "disabled");
  110. }
  111. $pass->setAttrib('alt', 'regular_text');
  112. $this->addElement($pass);
  113. $genre = new Zend_Form_Element_Text('genre');
  114. $genre->setLabel(_("Genre"))
  115. ->setValue(isset($setting[$prefix.'_genre'])?$setting[$prefix.'_genre']:"")
  116. ->setDecorators(array('ViewHelper'));
  117. if ($disable_all) {
  118. $genre->setAttrib("disabled", "disabled");
  119. }
  120. $this->addElement($genre);
  121. $url = new Zend_Form_Element_Text('url');
  122. $url->setLabel(_("URL"))
  123. ->setValue(isset($setting[$prefix.'_url'])?$setting[$prefix.'_url']:"")
  124. ->setValidators(array(
  125. array('regex', false, array('/^[0-9a-zA-Z\-_.:\/]+$/', 'messages' => _('Invalid character entered')))))
  126. ->setDecorators(array('ViewHelper'));
  127. if ($disable_all) {
  128. $url->setAttrib("disabled", "disabled");
  129. }
  130. $url->setAttrib('alt', 'url');
  131. $this->addElement($url);
  132. $name = new Zend_Form_Element_Text('name');
  133. $name->setLabel(_("Name"))
  134. ->setValue(isset($setting[$prefix.'_name'])?$setting[$prefix.'_name']:"")
  135. ->setDecorators(array('ViewHelper'));
  136. if ($disable_all) {
  137. $name->setAttrib("disabled", "disabled");
  138. }
  139. $this->addElement($name);
  140. $description = new Zend_Form_Element_Text('description');
  141. $description->setLabel(_("Description"))
  142. ->setValue(isset($setting[$prefix.'_description'])?$setting[$prefix.'_description']:"")
  143. ->setDecorators(array('ViewHelper'));
  144. if ($disable_all) {
  145. $description->setAttrib("disabled", "disabled");
  146. }
  147. $this->addElement($description);
  148. $mount = new Zend_Form_Element_Text('mount');
  149. $mount->setLabel(_("Mount Point"))
  150. ->setValue(isset($setting[$prefix.'_mount'])?$setting[$prefix.'_mount']:"")
  151. ->setValidators(array(
  152. array('regex', false, array('/^[^ &<>]+$/', 'messages' => _('Invalid character entered')))))
  153. ->setDecorators(array('ViewHelper'));
  154. if ($disable_all) {
  155. $mount->setAttrib("disabled", "disabled");
  156. }
  157. $mount->setAttrib('alt', 'regular_text');
  158. $this->addElement($mount);
  159. $user = new Zend_Form_Element_Text('user');
  160. $user->setLabel(_("Username"))
  161. ->setValue(isset($setting[$prefix.'_user'])?$setting[$prefix.'_user']:"")
  162. ->setValidators(array(
  163. array('regex', false, array('/^[^ &<>]+$/', 'messages' => _('Invalid character entered')))))
  164. ->setDecorators(array('ViewHelper'));
  165. if ($disable_all) {
  166. $user->setAttrib("disabled", "disabled");
  167. }
  168. $user->setAttrib('alt', 'regular_text');
  169. $this->addElement($user);
  170. $adminUser = new Zend_Form_Element_Text('admin_user');
  171. $adminUser->setLabel(_("Admin User"))
  172. ->setValue(Application_Model_StreamSetting::getAdminUser($prefix))
  173. ->setValidators(array(
  174. array('regex', false, array('/^[^ &<>]+$/', 'messages' => _('Invalid character entered')))))
  175. ->setDecorators(array('ViewHelper'));
  176. if ($disable_all) {
  177. $adminUser->setAttrib("disabled", "disabled");
  178. }
  179. $adminUser->setAttrib('alt', 'regular_text');
  180. $this->addElement($adminUser);
  181. $adminPass = new Zend_Form_Element_Password('admin_pass');
  182. $adminPass->setLabel(_("Admin Password"))
  183. ->setValue(Application_Model_StreamSetting::getAdminPass($prefix))
  184. ->setValidators(array(
  185. array('regex', false, array('/^[^ &<>]+$/', 'messages' => _('Invalid character entered')))))
  186. ->setDecorators(array('ViewHelper'));
  187. if ($disable_all) {
  188. $adminPass->setAttrib("disabled", "disabled");
  189. }
  190. $adminPass->setAttrib('alt', 'regular_text');
  191. $this->addElement($adminPass);
  192. $liquidsopa_error_msg = '<div class="stream-status status-info"><h3>'._('Getting information from the server...').'</h3></div>';
  193. $this->setDecorators(array(
  194. array('ViewScript', array('viewScript' => 'form/stream-setting-form.phtml', "stream_number"=>$stream_number, "enabled"=>$enable->getValue(), "liquidsoap_error_msg"=>$liquidsopa_error_msg))
  195. ));
  196. }
  197. public function isValid ($data)
  198. {
  199. $f_data = $data['s'.$this->prefix."_data"];
  200. $isValid = parent::isValid($f_data);
  201. if ($f_data['enable'] == 1) {
  202. if ($f_data['host'] == '') {
  203. $element = $this->getElement("host");
  204. $element->addError(_("Server cannot be empty."));
  205. $isValid = false;
  206. }
  207. if ($f_data['port'] == '') {
  208. $element = $this->getElement("port");
  209. $element->addError(_("Port cannot be empty."));
  210. $isValid = false;
  211. }
  212. if ($f_data['output'] == 'icecast') {
  213. if ($f_data['mount'] == '') {
  214. $element = $this->getElement("mount");
  215. $element->addError(_("Mount cannot be empty with Icecast server."));
  216. $isValid = false;
  217. }
  218. }
  219. }
  220. return $isValid;
  221. }
  222. }