JqueryTrait.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. <?php
  2. namespace ZN\ViewObjects;
  3. trait JqueryTrait
  4. {
  5. /*
  6. * Seçici seçimi
  7. *
  8. * $(this), $("#custom"), $(".example")
  9. *
  10. * @var string this
  11. */
  12. protected $selector = 'this';
  13. /*
  14. * Fonksiyon bloğu
  15. *
  16. * function(data){alert("example");}
  17. *
  18. * @var string
  19. */
  20. protected $callback = '';
  21. protected $tag = false;
  22. protected $jqueryCdn = false;
  23. protected $jqueryUiCdn = false;
  24. public function __construct($tag = false, $jqueryCdn = false, $jqueryUiCdn = false)
  25. {
  26. $this->tag = $tag;
  27. $this->jqueryCdn = $jqueryCdn;
  28. $this->jqueryUiCdn = $jqueryUiCdn;
  29. }
  30. protected function _tag($code)
  31. {
  32. if( $this->tag === true )
  33. {
  34. return \Script::open(true, $this->jqueryCdn, $this->jqueryUiCdn).$code.\Script::close();
  35. }
  36. return $code;
  37. }
  38. protected function _nailConvert($str, $nail = '"')
  39. {
  40. if( $nail === '"')
  41. {
  42. $str = str_replace('"', "'", $str);
  43. }
  44. return $str;
  45. }
  46. /******************************************************************************************
  47. * SELECTOR *
  48. *******************************************************************************************
  49. | Genel Kullanım: Seçiciyi belirlemek için kullanılır. |
  50. @param string $selector
  51. @return $this
  52. | |
  53. ******************************************************************************************/
  54. public function selector($selector = '')
  55. {
  56. $this->selector = $selector;
  57. return $this;
  58. }
  59. /******************************************************************************************
  60. * CALLBACK *
  61. *******************************************************************************************
  62. | Genel Kullanım: Sorgunun fonksiyon bloğunu oluşturmak için kullanılır. |
  63. @param string $params
  64. @param string $callback
  65. @return $this
  66. | |
  67. ******************************************************************************************/
  68. public function callback($params = '', $callback = '')
  69. {
  70. $this->callback = \JQ::func($params, $callback);
  71. return $this;
  72. }
  73. /******************************************************************************************
  74. * FUNC *
  75. *******************************************************************************************
  76. | Genel Kullanım: Callback işlevinin diğer adıdır. |
  77. @param string $params
  78. @param string $callback
  79. @return $this
  80. | |
  81. ******************************************************************************************/
  82. public function func($params = '', $callback = '')
  83. {
  84. $this->callback($params, $callback);
  85. return $this;
  86. }
  87. /******************************************************************************************
  88. * BOOL TO STR *
  89. *******************************************************************************************
  90. | Genel Kullanım: Parametre mantıksal türden string türe dönüştürülüyor. |
  91. @param bool $bool
  92. @return string
  93. | |
  94. ******************************************************************************************/
  95. protected function _boolToStr($bool = true)
  96. {
  97. if( $bool === true )
  98. {
  99. return 'true';
  100. }
  101. elseif( $bool === false )
  102. {
  103. return 'false';
  104. }
  105. else
  106. {
  107. return $bool;
  108. }
  109. }
  110. /******************************************************************************************
  111. * IS KEY SELECTOR *
  112. *******************************************************************************************
  113. | Genel Kullanım: Parametrenin seçici bir anahtar olup olmadığı kontrol ediliyor. |
  114. @param string $data
  115. @return bool
  116. | |
  117. ******************************************************************************************/
  118. protected function _isKeySelector($data)
  119. {
  120. $keyword = ['document', 'this', 'window'];
  121. if( in_array($data, $keyword) )
  122. {
  123. return true;
  124. }
  125. else
  126. {
  127. return false;
  128. }
  129. }
  130. /******************************************************************************************
  131. * IS FUNC *
  132. *******************************************************************************************
  133. | Genel Kullanım: Parametrenin fonksiyon olup olmadığı kontrol ediliyor. |
  134. @param array $array
  135. @return string
  136. | |
  137. ******************************************************************************************/
  138. protected function _isFunc($data)
  139. {
  140. if( preg_match('/function.*\(.*\)/', $data) )
  141. {
  142. return true;
  143. }
  144. else
  145. {
  146. return false;
  147. }
  148. }
  149. /******************************************************************************************
  150. * IS JQUERY *
  151. *******************************************************************************************
  152. | Genel Kullanım: Parametrenin fonksiyon olup olmadığı kontrol ediliyor. |
  153. @param array $array
  154. @return string
  155. | |
  156. ******************************************************************************************/
  157. protected function _isJquery($data)
  158. {
  159. if( preg_match('/(\$|jQuery)(\.\w+)*\(.*\)/i', $data) )
  160. {
  161. return true;
  162. }
  163. else
  164. {
  165. return false;
  166. }
  167. }
  168. /******************************************************************************************
  169. * OBJECT *
  170. *******************************************************************************************
  171. | Genel Kullanım: Parametrenin veri türüne göre object türe dönüştürülme yapılır. |
  172. @param string/array $array
  173. @return string
  174. | |
  175. ******************************************************************************************/
  176. protected function _object($array)
  177. {
  178. $object = '';
  179. if( is_array($array) )
  180. {
  181. $object = '';
  182. $object .= "{";
  183. if( ! empty($array)) foreach($array as $k => $v)
  184. {
  185. $object .= $k.":".$v.", ";
  186. }
  187. $object = substr($object, 0, -2);
  188. $object .= "}";
  189. }
  190. else
  191. {
  192. $object = EOL."\"$array\"";
  193. }
  194. return $object;
  195. }
  196. /******************************************************************************************
  197. * PARAMS *
  198. *******************************************************************************************
  199. | Genel Kullanım: Parametrenin ne tür veri içerdiğinin kontrolü yapılır. |
  200. @param array $array
  201. @return string
  202. | |
  203. ******************************************************************************************/
  204. protected function _params($array = [])
  205. {
  206. $implode = '';
  207. if( is_array($array) )
  208. {
  209. if( ! empty($array) ) foreach( $array as $v )
  210. {
  211. if( ! empty($v) )
  212. {
  213. $implode .= \JQ::stringControl($v).",";
  214. }
  215. }
  216. $implode = rtrim($implode, ",");
  217. }
  218. else
  219. {
  220. if( ! empty($array) )
  221. {
  222. $implode = \JQ::stringControl($array);
  223. }
  224. }
  225. return $implode;
  226. }
  227. }