PageRenderTime 61ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/html/AppCode/expressionengine/third_party/matrix/ext.matrix.php

https://github.com/w3bg/www.hsifin.com
PHP | 198 lines | 119 code | 35 blank | 44 comment | 18 complexity | 0b28256b9b528558a156fa264b7962b7 MD5 | raw file
Possible License(s): AGPL-3.0
  1. <?php if (! defined('APP_VER')) exit('No direct script access allowed');
  2. if (! defined('MATRIX_VER'))
  3. {
  4. // get the version from config.php
  5. require PATH_THIRD.'matrix/config.php';
  6. define('MATRIX_VER', $config['version']);
  7. }
  8. /**
  9. * Matrix Extension Class for ExpressionEngine 2
  10. *
  11. * @package Matrix
  12. * @author Brandon Kelly <brandon@pixelandtonic.com>
  13. * @copyright Copyright (c) 2010 Pixel & Tonic, LLC
  14. */
  15. class Matrix_ext {
  16. var $name = 'Matrix';
  17. var $version = MATRIX_VER;
  18. var $settings_exist = 'n';
  19. var $docs_url = 'http://pixelandtonic.com/matrix';
  20. /**
  21. * Extension Constructor
  22. */
  23. function Matrix_ext()
  24. {
  25. $this->EE =& get_instance();
  26. // -------------------------------------------
  27. // Prepare Cache
  28. // -------------------------------------------
  29. if (! isset($this->EE->session->cache['matrix']))
  30. {
  31. $this->EE->session->cache['matrix'] = array();
  32. }
  33. $this->cache =& $this->EE->session->cache['matrix'];
  34. }
  35. // --------------------------------------------------------------------
  36. /**
  37. * Activate Extension
  38. */
  39. function activate_extension()
  40. {
  41. $this->EE->db->insert('extensions', array(
  42. 'class' => 'Matrix_ext',
  43. 'hook' => 'channel_entries_tagdata',
  44. 'method' => 'channel_entries_tagdata',
  45. 'settings' => '',
  46. 'priority' => 10,
  47. 'version' => $this->version,
  48. 'enabled' => 'y'
  49. ));
  50. }
  51. /**
  52. * Update Extension
  53. */
  54. function update_extension($current = FALSE)
  55. {
  56. if (! $current || $current == $this->version)
  57. {
  58. return FALSE;
  59. }
  60. $this->EE->db->where('class', 'Matrix_ext');
  61. $this->EE->db->update('extensions', array('version' => $this->version));
  62. }
  63. /**
  64. * Disable Extension
  65. */
  66. function disable_extension()
  67. {
  68. $this->EE->db->query('DELETE FROM exp_extensions WHERE class = "Matrix_ext"');
  69. }
  70. // --------------------------------------------------------------------
  71. /**
  72. * Get Fields
  73. */
  74. private function _get_fields()
  75. {
  76. if (! isset($this->cache['fields']))
  77. {
  78. $this->EE->db->select('field_id, field_name, field_settings');
  79. $this->EE->db->where('field_type', 'matrix');
  80. $query = $this->EE->db->get('channel_fields');
  81. $fields = $query->result_array();
  82. foreach ($fields as &$field)
  83. {
  84. $field['field_settings'] = unserialize(base64_decode($field['field_settings']));
  85. }
  86. $this->cache['fields'] = $fields;
  87. }
  88. return $this->cache['fields'];
  89. }
  90. // --------------------------------------------------------------------
  91. /**
  92. * channel_entries_tagdata hook
  93. */
  94. function channel_entries_tagdata($tagdata, $row, &$Channel)
  95. {
  96. // has this hook already been called?
  97. if ($this->EE->extensions->last_call)
  98. {
  99. $tagdata = $this->EE->extensions->last_call;
  100. }
  101. // save the Channel ref
  102. $this->cache['Channel'] =& $Channel;
  103. // iterate through each field
  104. foreach($this->_get_fields() as $field)
  105. {
  106. // is the tag even being used here?
  107. if (strpos($tagdata, LD.$field['field_name']) !== FALSE)
  108. {
  109. $offset = 0;
  110. while (preg_match('/'.LD.$field['field_name'].'(:(\w+))?(\s+.*)?'.RD.'/sU', $tagdata, $matches, PREG_OFFSET_CAPTURE, $offset))
  111. {
  112. $tag_pos = $matches[0][1];
  113. $tag_len = strlen($matches[0][0]);
  114. $tagdata_pos = $tag_pos + $tag_len;
  115. $endtag = LD.'/'.$field['field_name'].(isset($matches[1][0]) ? $matches[1][0] : '').RD;
  116. $endtag_len = strlen($endtag);
  117. $endtag_pos = strpos($tagdata, $endtag, $tagdata_pos);
  118. $tag_func = (isset($matches[2][0]) && $matches[2][0]) ? 'replace_'.$matches[2][0] : '';
  119. // get the params
  120. $params = array();
  121. if (isset($matches[3][0]) && $matches[3][0] && preg_match_all('/\s+([\w-:]+)\s*=\s*([\'\"])([^\2]*)\2/sU', $matches[3][0], $param_matches))
  122. {
  123. for ($j = 0; $j < count($param_matches[0]); $j++)
  124. {
  125. $params[$param_matches[1][$j]] = $param_matches[3][$j];
  126. }
  127. }
  128. // is this a tag pair?
  129. $field_tagdata = ($endtag_pos !== FALSE)
  130. ? substr($tagdata, $tagdata_pos, $endtag_pos - $tagdata_pos)
  131. : '';
  132. // -------------------------------------------
  133. // Call the tag's method
  134. // -------------------------------------------
  135. if (! class_exists('Matrix_ft'))
  136. {
  137. require_once PATH_THIRD.'matrix/ft.matrix'.EXT;
  138. }
  139. $Matrix_ft = new Matrix_ft();
  140. $Matrix_ft->row = $row;
  141. $Matrix_ft->field_id = $field['field_id'];
  142. $Matrix_ft->field_name = $field['field_name'];
  143. $Matrix_ft->entry_id = $row['entry_id'];
  144. $Matrix_ft->settings = array_merge($row, $field['field_settings']);
  145. if (! $tag_func || ! method_exists($Matrix_ft, $tag_func))
  146. {
  147. $tag_func = 'replace_tag';
  148. }
  149. $new_tagdata = $Matrix_ft->$tag_func(NULL, $params, $field_tagdata);
  150. // update tagdata
  151. $tagdata = substr($tagdata, 0, $tag_pos)
  152. . $new_tagdata
  153. . substr($tagdata, ($endtag_pos !== FALSE ? $endtag_pos+$endtag_len : $tagdata_pos));
  154. // update offset
  155. $offset = $tag_pos;
  156. }
  157. }
  158. }
  159. // unset Channel ref
  160. unset($this->cache['Channel']);
  161. return $tagdata;
  162. }
  163. }