link_form.class.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Link form class definition
  5. * @package chamilo.link
  6. */
  7. /**
  8. * Init
  9. */
  10. namespace Link;
  11. use Chamilo;
  12. /**
  13. * Edit/Create link form.
  14. *
  15. * @license /licence.txt
  16. * @author Laurent Opprecht <laurent@opprecht.info>
  17. */
  18. class LinkForm extends \FormValidator
  19. {
  20. protected $link;
  21. function __construct($form_name = 'link', $method = 'post', $action = '', $target = '', $attributes = null, $track_submit = true)
  22. {
  23. parent::__construct($form_name, $method, $action, $target, $attributes, $track_submit);
  24. }
  25. /**
  26. *
  27. * @return \Link\LinkCategory
  28. */
  29. public function get_link()
  30. {
  31. return $this->link;
  32. }
  33. public function set_link($value)
  34. {
  35. $this->link = $value;
  36. }
  37. /**
  38. *
  39. * @param \Link\LinkCategory $link
  40. */
  41. function init($link = null)
  42. {
  43. $this->set_link($link);
  44. $defaults = array();
  45. $defaults['url'] = $link->url ? $link->url : 'http://';
  46. $defaults['title'] = $link->title;
  47. $defaults['description'] = $link->description;
  48. $defaults['category_id'] = $link->category_id;
  49. $defaults['display_order'] = $link->display_order;
  50. $defaults['on_homepage'] = $link->on_homepage;
  51. $defaults['target'] = $link->target;
  52. $this->add_hidden('c_id', $link->c_id);
  53. $this->add_hidden('id', $link->id);
  54. $this->add_hidden('session_id', $link->session_id);
  55. $form_name = $category->id ? get_lang('LinkMod') : get_lang('LinkAdd');
  56. $this->add_header($form_name);
  57. $this->add_textfield('url', get_lang('Url'), $required = true, array('class' => 'span6'));
  58. $this->addRule('url', get_lang('MalformedUrl'), 'regex', '|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i');
  59. $this->add_textfield('title', get_lang('Title'), $required = false, array('class' => 'span6'));
  60. $this->add_textarea('description', get_lang('Description'), array('class' => 'span3'));
  61. $this->add_checkbox('on_homepage', '', get_lang('OnHomepage'));
  62. $id = $link->id;
  63. if ($id) {
  64. $url = Chamilo::url('/main/metadata/index.php', array('eid' => "Link.$id"));
  65. $metadata = '<a class="control-text" href="' . $url . '">' . get_lang('AddMetadata') . '</a>';
  66. $this->add_label(get_lang('Metadata'), $metadata);
  67. }
  68. $options = array();
  69. $options[0] = '--';
  70. $categories = LinkCategoryRepository::instance()->find_by_course($link->c_id, $link->session_id);
  71. foreach ($categories as $category) {
  72. $options[$category->id] = $category->category_title;
  73. }
  74. $this->add_select('category_id', get_lang('Category'), $options);
  75. $targets = array(
  76. '_self' => get_lang('LinkOpenSelf'),
  77. '_blank' => get_lang('LinkOpenBlank'),
  78. '_parent' => get_lang('LinkOpenParent'),
  79. '_top' => get_lang('LinkOpenTop')
  80. );
  81. $this->add_select('target', get_lang('LinkTarget'), $targets);
  82. //$help = '<span class="help-block">' . get_lang('AddTargetOfLinkOnHomepage') . '</span>';
  83. //$this->add_label('', $help);
  84. $this->add_button('save', get_lang('Save'), array('class' => 'btn save'));
  85. $this->setDefaults($defaults);
  86. }
  87. function update_model()
  88. {
  89. $values = $this->exportValues();
  90. $link = $this->get_link();
  91. $link->url = $values['url'];
  92. $link->title = $values['title'];
  93. $link->description = $values['description'];
  94. $link->category_id = $values['category_id'];
  95. $link->on_homepage = isset($values['on_homepage']) ? $values['on_homepage'] : false;
  96. $link->target = $values['target'];
  97. }
  98. }