AjaxText.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. /**
  3. *
  4. * PHP Pro Bid $Id$ YqSCCXZrduxkVzdajWCxBU1487As/vsV5r+y/0I4Suw=
  5. *
  6. * @link http://www.phpprobid.com
  7. * @copyright Copyright (c) 2014 Online Ventures Software LTD & CodeCube SRL
  8. * @license http://www.phpprobid.com/license Commercial License
  9. *
  10. * @version 7.0
  11. */
  12. /**
  13. * ajax text custom form element
  14. *
  15. * creates an element which initially is a simple text, which if clicked will be transformed in an
  16. * editable text box with attached save/cancel buttons
  17. */
  18. namespace Ppb\Form\Element;
  19. use Cube\Form\Element;
  20. class AjaxText extends Element
  21. {
  22. const ELEMENT_CLASS = 'ajax-text';
  23. const INPUT_NAME = 'textContent';
  24. /**
  25. *
  26. * type of element - override the variable from the parent class
  27. *
  28. * @var string
  29. */
  30. protected $_element = 'ajaxText';
  31. /**
  32. *
  33. * base url of the application
  34. *
  35. * @var string
  36. */
  37. protected $_baseUrl;
  38. /**
  39. *
  40. * ajax post url for the success button
  41. *
  42. * @var string
  43. */
  44. protected $_postUrl;
  45. /**
  46. *
  47. * class constructor
  48. *
  49. * @param string $name
  50. */
  51. public function __construct($name)
  52. {
  53. parent::__construct($this->_element, $name);
  54. $this->setBodyCode(
  55. "<script type=\"text/javascript\">" . "\n"
  56. . " $(document).ready(function() { " . "\n"
  57. . " $('." . self::ELEMENT_CLASS . " .ajax-text-span').on('click', function() { " . "\n"
  58. . " var el = $(this).closest('label'); " . "\n"
  59. . " var v = el.find('.ajax-text-span').html(); " . "\n"
  60. . " el.find('[name=\"" . self::INPUT_NAME . "\"]').val(v); " . "\n"
  61. . " el.find('.ajax-text-span').hide(); " . "\n"
  62. . " el.find('.input-group').show(); " . "\n"
  63. . " }); " . "\n"
  64. . " $('." . self::ELEMENT_CLASS . " .btn-success').on('click', function() {
  65. var el = $(this).closest('label');
  66. var v = el.find('[name=\"" . self::INPUT_NAME . "\"]').val();
  67. var postUrl = el.attr('data-post-url');
  68. $.post(
  69. postUrl,
  70. {
  71. comments: v
  72. },
  73. function (data) {
  74. el.find('.ajax-text-span').html(v).show();
  75. el.find('.input-group').hide();
  76. },
  77. 'json'
  78. );
  79. }); " . "\n"
  80. . " $('." . self::ELEMENT_CLASS . " .btn-danger').on('click', function() { " . "\n"
  81. . " var el = $(this).closest('label'); " . "\n"
  82. . " el.find('.ajax-text-span').show(); " . "\n"
  83. . " el.find('.input-group').hide(); " . "\n"
  84. . " }); " . "\n"
  85. . " }); " . "\n"
  86. . "</script>");
  87. $this->setHeaderCode(
  88. "<style type=\"text/css\">" . "\n"
  89. . "." . self::ELEMENT_CLASS . " { " . "\n"
  90. . " display: block; " . "\n"
  91. . "} " . "\n"
  92. . "." . self::ELEMENT_CLASS . " .input-group { " . "\n"
  93. . " display: none; " . "\n"
  94. . "} " . "\n"
  95. . "." . self::ELEMENT_CLASS . " span.ajax-text-span { " . "\n"
  96. . " cursor: text; " . "\n"
  97. . " padding-bottom: 2px; " . "\n"
  98. . " border-bottom: 1px dotted #999; " . "\n"
  99. . "} " . "\n"
  100. . "</style>");
  101. }
  102. /**
  103. *
  104. * set ajax post url
  105. *
  106. * @param string $postUrl
  107. * @return $this
  108. */
  109. public function setPostUrl($postUrl)
  110. {
  111. $this->_postUrl = $postUrl;
  112. return $this;
  113. }
  114. /**
  115. *
  116. * get ajax post url
  117. *
  118. * @throws \RuntimeException
  119. * @return string
  120. */
  121. public function getPostUrl()
  122. {
  123. if (!$this->_postUrl) {
  124. throw new \RuntimeException("The post url for the AjaxText form element must be set.");
  125. }
  126. return $this->_postUrl;
  127. }
  128. /**
  129. *
  130. * render element
  131. *
  132. * @return string
  133. */
  134. public function render()
  135. {
  136. $translate = $this->getTranslate();
  137. $value = $this->getValue();
  138. return '<label class="' . self::ELEMENT_CLASS . '" data-post-url="' . $this->getPostUrl() . '">'
  139. . '<span class="ajax-text-span" title="' . $translate->_('Edit Text') . '">'
  140. . $value
  141. . '</span>'
  142. . '<div class="input-group">'
  143. . ' <input type="text" name="' . self::INPUT_NAME . '" '
  144. . $this->renderAttributes()
  145. . 'value="' . $value . '" '
  146. . $this->_endTag . ' '
  147. . ' <span class="input-group-btn"> '
  148. . ' <button class="btn btn-success" type="button"><i class="fa fa-check"></i></button> '
  149. . ' <button class="btn btn-danger" type="button"><i class="fa fa-times"></i></button> '
  150. . ' </span>'
  151. . '</div>'
  152. . '</label>';
  153. }
  154. }