PageRenderTime 40ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/model/DataExtension.php

http://github.com/silverstripe/sapphire
PHP | 244 lines | 119 code | 29 blank | 96 comment | 10 complexity | 70757612d2ba506551961b4e8cf4aef0 MD5 | raw file
Possible License(s): BSD-3-Clause, MIT, CC-BY-3.0, GPL-2.0, AGPL-1.0, LGPL-2.1
  1. <?php
  2. /**
  3. * An extension that adds additional functionality to a {@link DataObject}.
  4. *
  5. * @package sapphire
  6. * @subpackage model
  7. */
  8. abstract class DataExtension extends Extension {
  9. /**
  10. * Statics on a {@link DataObject} subclass
  11. * which can be extended by an extension. This list is
  12. * limited for security and performance reasons.
  13. *
  14. * Keys are the static names, and the values are whether or not the value is an array that should
  15. * be merged.
  16. *
  17. * @var array
  18. */
  19. protected static $extendable_statics = array(
  20. 'db' => true,
  21. 'has_one' => true,
  22. 'belongs_to' => true,
  23. 'indexes' => true,
  24. 'defaults' => true,
  25. 'has_many' => true,
  26. 'many_many' => true,
  27. 'belongs_many_many' => true,
  28. 'many_many_extraFields' => true,
  29. 'searchable_fields' => true,
  30. 'api_access' => false,
  31. );
  32. private static $extra_statics_loaded = array();
  33. /**
  34. * Load the extra static definitions for the given extension
  35. * class name, called by {@link Object::add_extension()}
  36. *
  37. * @param string $class Class name of the owner class (or owner base class)
  38. * @param string $extension Class name of the extension class
  39. */
  40. public static function load_extra_statics($class, $extension) {
  41. if(!empty(self::$extra_statics_loaded[$class][$extension])) return;
  42. self::$extra_statics_loaded[$class][$extension] = true;
  43. if(preg_match('/^([^(]*)/', $extension, $matches)) {
  44. $extensionClass = $matches[1];
  45. } else {
  46. user_error("Bad extenion '$extension' - can't find classname", E_USER_WARNING);
  47. return;
  48. }
  49. // @deprecated 2.4 - use extraStatics() now, not extraDBFields()
  50. if(method_exists($extensionClass, 'extraDBFields')) {
  51. Deprecation::notice('2.4', 'DataExtension::extraDBFields() is deprecated. Please use extraStatics() instead.');
  52. $extraStaticsMethod = 'extraDBFields';
  53. } else {
  54. $extraStaticsMethod = 'extraStatics';
  55. }
  56. // If the extension has been manually applied to a subclass, we should ignore that.
  57. if(Object::has_extension(get_parent_class($class), $extensionClass)) return;
  58. // If there aren't any extraStatics we shouldn't try to load them.
  59. if (!method_exists($extensionClass, $extraStaticsMethod) ) return;
  60. $statics = call_user_func(array(singleton($extensionClass), $extraStaticsMethod), $class, $extension);
  61. if($statics) {
  62. foreach($statics as $name => $newVal) {
  63. if(isset(self::$extendable_statics[$name])) {
  64. // Array to be merged
  65. if(self::$extendable_statics[$name]) {
  66. $origVal = Object::uninherited_static($class, $name);
  67. // Can't use add_static_var() here as it would merge the array rather than replacing
  68. Object::set_static($class, $name, array_merge((array)$origVal, $newVal));
  69. // Value to be overwritten
  70. } else {
  71. Object::set_static($class, $name, $newVal);
  72. }
  73. }
  74. }
  75. DataObject::$cache_has_own_table[$class] = null;
  76. DataObject::$cache_has_own_table_field[$class] = null;
  77. }
  78. }
  79. public static function unload_extra_statics($class, $extension) {
  80. self::$extra_statics_loaded[$class][$extension] = false;
  81. }
  82. /**
  83. * Edit the given query object to support queries for this extension
  84. *
  85. * @param SQLQuery $query Query to augment.
  86. */
  87. function augmentSQL(SQLQuery &$query) {
  88. }
  89. /**
  90. * Update the database schema as required by this extension.
  91. *
  92. * When duplicating a table's structure, remember to duplicate the create options
  93. * as well. See {@link Versioned->augmentDatabase} for an example.
  94. */
  95. function augmentDatabase() {
  96. }
  97. /**
  98. * Augment a write-record request.
  99. *
  100. * @param SQLQuery $manipulation Query to augment.
  101. */
  102. function augmentWrite(&$manipulation) {
  103. }
  104. function onBeforeWrite() {
  105. }
  106. function onAfterWrite() {
  107. }
  108. function onBeforeDelete() {
  109. }
  110. function onAfterDelete() {
  111. }
  112. function requireDefaultRecords() {
  113. }
  114. function populateDefaults() {
  115. }
  116. function can($member) {
  117. }
  118. function canEdit($member) {
  119. }
  120. function canDelete($member) {
  121. }
  122. function canCreate($member) {
  123. }
  124. /**
  125. * Define extra database fields
  126. *
  127. * Return a map where the keys are db, has_one, etc, and the values are
  128. * additional fields/relations to be defined.
  129. *
  130. * @param $class since this method might be called on the class directly
  131. * @param $extension since this can help to extract parameters to help set indexes
  132. *
  133. * @return array Returns a map where the keys are db, has_one, etc, and
  134. * the values are additional fields/relations to be defined.
  135. */
  136. function extraStatics($class=null, $extension=null) {
  137. return array();
  138. }
  139. /**
  140. * This function is used to provide modifications to the form in the CMS
  141. * by the extension. By default, no changes are made. {@link DataObject->getCMSFields()}.
  142. *
  143. * Please consider using {@link updateFormFields()} to globally add
  144. * formfields to the record. The method {@link updateCMSFields()}
  145. * should just be used to add or modify tabs, or fields which
  146. * are specific to the CMS-context.
  147. *
  148. * Caution: Use {@link FieldList->addFieldToTab()} to add fields.
  149. *
  150. * @param FieldList $fields FieldList with a contained TabSet
  151. */
  152. function updateCMSFields(FieldList $fields) {
  153. }
  154. /**
  155. * This function is used to provide modifications to the form used
  156. * for front end forms. {@link DataObject->getFrontEndFields()}
  157. *
  158. * Caution: Use {@link FieldList->push()} to add fields.
  159. *
  160. * @param FieldList $fields FieldList without TabSet nesting
  161. */
  162. function updateFrontEndFields(FieldList $fields) {
  163. }
  164. /**
  165. * This is used to provide modifications to the form actions
  166. * used in the CMS. {@link DataObject->getCMSActions()}.
  167. *
  168. * @param FieldList $actions FieldList
  169. */
  170. function updateCMSActions(FieldList $actions) {
  171. }
  172. /**
  173. * this function is used to provide modifications to the summary fields in CMS
  174. * by the extension
  175. * By default, the summaryField() of its owner will merge more fields defined in the extension's
  176. * $extra_fields['summary_fields']
  177. */
  178. function updateSummaryFields(&$fields){
  179. $extra_fields = $this->extraStatics();
  180. if(isset($extra_fields['summary_fields'])){
  181. $summary_fields = $extra_fields['summary_fields'];
  182. // if summary_fields were passed in numeric array,
  183. // convert to an associative array
  184. if($summary_fields && array_key_exists(0, $summary_fields)) {
  185. $summary_fields = array_combine(array_values($summary_fields), array_values($summary_fields));
  186. }
  187. if($summary_fields) $fields = array_merge($fields, $summary_fields);
  188. }
  189. }
  190. /**
  191. * this function is used to provide modifications to the fields labels in CMS
  192. * by the extension
  193. * By default, the fieldLabels() of its owner will merge more fields defined in the extension's
  194. * $extra_fields['field_labels']
  195. */
  196. function updateFieldLabels(&$lables){
  197. $extra_fields = $this->extraStatics();
  198. if(isset($extra_fields['field_labels'])){
  199. $field_labels = $extra_fields['field_labels'];
  200. if($field_labels) $lables = array_merge($lables, $field_labels);
  201. }
  202. }
  203. /**
  204. * Clear any internal caches.
  205. */
  206. function flushCache() {
  207. }
  208. }
  209. ?>