/pimcore/models/Object/Fieldcollection/Definition.php

https://github.com/ngocanh/pimcore · PHP · 293 lines · 189 code · 38 blank · 66 comment · 11 complexity · 27cec88e2ff10f680b311aac3e1da41c MD5 · raw file

  1. <?php
  2. /**
  3. * Pimcore
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://www.pimcore.org/license
  11. *
  12. * @category Pimcore
  13. * @package Object_Fieldcollection
  14. * @copyright Copyright (c) 2009-2010 elements.at New Media Solutions GmbH (http://www.elements.at)
  15. * @license http://www.pimcore.org/license New BSD License
  16. */
  17. class Object_Fieldcollection_Definition extends Pimcore_Model_Abstract {
  18. /**
  19. * @var string
  20. */
  21. public $key;
  22. /**
  23. * @var string
  24. */
  25. public $parentClass;
  26. /**
  27. * @var array
  28. */
  29. public $layoutDefinitions;
  30. /**
  31. * @return string
  32. */
  33. public function getKey() {
  34. return $this->key;
  35. }
  36. /**
  37. * @param string $key
  38. * @return void
  39. */
  40. public function setKey($key) {
  41. $this->key = $key;
  42. }
  43. /**
  44. * @return string
  45. */
  46. public function getParentClass() {
  47. return $this->parentClass;
  48. }
  49. /**
  50. * @param string $parentClass
  51. * @return void
  52. */
  53. public function setParentClass($parentClass) {
  54. $this->parentClass = $parentClass;
  55. }
  56. /**
  57. * @return array
  58. */
  59. public function getLayoutDefinitions() {
  60. return $this->layoutDefinitions;
  61. }
  62. /**
  63. * @param array $layoutDefinitions
  64. * @return void
  65. */
  66. public function setLayoutDefinitions($layoutDefinitions) {
  67. $this->layoutDefinitions = $layoutDefinitions;
  68. $this->fieldDefinitions = array();
  69. $this->extractDataDefinitions($this->layoutDefinitions);
  70. }
  71. /**
  72. * @return array
  73. */
  74. public function getFieldDefinitions() {
  75. return $this->fieldDefinitions;
  76. }
  77. /**
  78. * @param array $fieldDefinitions
  79. * @return void
  80. */
  81. public function setFieldDefinitions($fieldDefinitions) {
  82. $this->fieldDefinitions = $fieldDefinitions;
  83. }
  84. /**
  85. * @param string $key
  86. * @param Object_Class_Data $data
  87. * @return void
  88. */
  89. public function setFieldDefinition($key, $data) {
  90. $this->fieldDefinitions[$key] = $data;
  91. }
  92. /**
  93. * @return Object_Data
  94. */
  95. public function getFieldDefinition($key) {
  96. if (array_key_exists($key, $this->fieldDefinitions)) {
  97. return $this->fieldDefinitions[$key];
  98. }
  99. return false;
  100. }
  101. /**
  102. * @param array|Object_Class_Layout|Object_Class_Data $def
  103. * @return void
  104. */
  105. public function extractDataDefinitions($def) {
  106. if ($def instanceof Object_Class_Layout) {
  107. if ($def->hasChilds()) {
  108. foreach ($def->getChilds() as $child) {
  109. $this->extractDataDefinitions($child);
  110. }
  111. }
  112. }
  113. if ($def instanceof Object_Class_Data) {
  114. $this->setFieldDefinition($def->getName(), $def);
  115. }
  116. }
  117. public static function getByKey ($key) {
  118. $fieldCollectionFolder = PIMCORE_CLASS_DIRECTORY . "/fieldcollections";
  119. $fieldFile = $fieldCollectionFolder . "/" . $key . ".psf";
  120. if(is_file($fieldFile)) {
  121. $fcData = file_get_contents($fieldFile);
  122. $fc = unserialize($fcData);
  123. return $fc;
  124. }
  125. throw new Exception("Field-Collection with key: " . $key . " does not exist.");
  126. }
  127. public function save () {
  128. if(!$this->getKey()) {
  129. throw new Exception("A field-collection needs a key to be saved!");
  130. }
  131. $fieldCollectionFolder = PIMCORE_CLASS_DIRECTORY . "/fieldcollections";
  132. // create folder if not exist
  133. if(!is_dir($fieldCollectionFolder)) {
  134. mkdir($fieldCollectionFolder);
  135. }
  136. $serialized = serialize($this);
  137. $definitionFile = $fieldCollectionFolder . "/" . $this->getKey() . ".psf";
  138. if(!is_writable(dirname($definitionFile)) || (is_file($definitionFile) && !is_writable($definitionFile))) {
  139. throw new Exception("Cannot write definition file in: " . $definitionFile . " please check write permission on this directory.");
  140. }
  141. file_put_contents($definitionFile,$serialized);
  142. chmod($definitionFile, 0766);
  143. $extendClass = "Object_Fieldcollection_Data_Abstract";
  144. if ($this->getParentClass()) {
  145. $extendClass = $this->getParentClass();
  146. }
  147. // create class file
  148. $cd = '<?php ';
  149. $cd .= "\n\n";
  150. $cd .= "class Object_Fieldcollection_Data_" . ucfirst($this->getKey()) . " extends " . $extendClass . " {";
  151. $cd .= "\n\n";
  152. $cd .= 'public $type = "' . $this->getKey() . "\";\n";
  153. if (is_array($this->getFieldDefinitions()) && count($this->getFieldDefinitions())) {
  154. foreach ($this->getFieldDefinitions() as $key => $def) {
  155. $cd .= "public $" . $key . ";\n";
  156. }
  157. }
  158. $cd .= "\n\n";
  159. if (is_array($this->getFieldDefinitions()) && count($this->getFieldDefinitions())) {
  160. $relationTypes = array();
  161. foreach ($this->getFieldDefinitions() as $key => $def) {
  162. /**
  163. * @var $def Object_Class_Data
  164. */
  165. $cd .= $def->getGetterCodeFieldcollection($this);
  166. $cd .= $def->getSetterCodeFieldcollection($this);
  167. // $cd .= '/**' . "\n";
  168. // $cd .= '* @return ' . $def->getPhpdocType() . "\n";
  169. // $cd .= '*/' . "\n";
  170. // $cd .= "public function get" . ucfirst($key) . " () {\n";
  171. // $cd .= "\t return " . '$this->' . $key . ";\n";
  172. // $cd .= "}\n\n";
  173. //
  174. // $cd .= '/**' . "\n";
  175. // $cd .= '* @param ' . $def->getPhpdocType() . ' $' . $key . "\n";
  176. // $cd .= "* @return void\n";
  177. // $cd .= '*/' . "\n";
  178. // $cd .= "public function set" . ucfirst($key) . " (" . '$' . $key . ") {\n";
  179. // $cd .= "\t" . '$this->' . $key . " = " . '$' . $key . ";\n";
  180. // $cd .= "}\n\n";
  181. }
  182. }
  183. $cd .= "}\n";
  184. $cd .= "\n";
  185. $fieldClassFolder = PIMCORE_CLASS_DIRECTORY . "/Object/Fieldcollection/Data";
  186. if(!is_dir($fieldClassFolder)) {
  187. mkdir($fieldClassFolder,0766,true);
  188. }
  189. $classFile = $fieldClassFolder . "/" . ucfirst($this->getKey()) . ".php";
  190. if(!is_writable(dirname($classFile)) || (is_file($classFile) && !is_writable($classFile))) {
  191. throw new Exception("Cannot write definition file in: " . $classFile . " please check write permission on this directory.");
  192. }
  193. file_put_contents($classFile,$cd);
  194. chmod($classFile,0766);
  195. // update classes
  196. $classList = new Object_Class_List();
  197. $classes = $classList->load();
  198. if(is_array($classes)){
  199. foreach($classes as $class){
  200. foreach ($class->getFieldDefinitions() as $fieldDef) {
  201. if($fieldDef instanceof Object_Class_Data_Fieldcollections) {
  202. if(in_array($this->getKey(), $fieldDef->getAllowedTypes())) {
  203. $this->getResource()->createUpdateTable($class);
  204. break;
  205. }
  206. }
  207. }
  208. }
  209. }
  210. }
  211. public function delete () {
  212. $fieldCollectionFolder = PIMCORE_CLASS_DIRECTORY . "/fieldcollections";
  213. $fieldFile = $fieldCollectionFolder . "/" . $this->getKey() . ".psf";
  214. @unlink($fieldFile);
  215. $fieldClassFolder = PIMCORE_CLASS_DIRECTORY . "/Object/Fieldcollection/Data";
  216. $fieldClass = $fieldClassFolder . "/" . ucfirst($this->getKey()) . ".php";
  217. @unlink($fieldClass);
  218. // update classes
  219. $classList = new Object_Class_List();
  220. $classes = $classList->load();
  221. if(is_array($classes)){
  222. foreach($classes as $class){
  223. foreach ($class->getFieldDefinitions() as $fieldDef) {
  224. if($fieldDef instanceof Object_Class_Data_Fieldcollections) {
  225. if(in_array($this->getKey(), $fieldDef->getAllowedTypes())) {
  226. $this->getResource()->delete($class);
  227. break;
  228. }
  229. }
  230. }
  231. }
  232. }
  233. }
  234. }