Guest User

Untitled

a guest
Jun 23rd, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.99 KB | None | 0 0
  1. <?php
  2.  
  3. define_safe('__ENTRY_OK__', 0);
  4. define_safe('__ENTRY_FIELD_ERROR__', 100);
  5.  
  6. Class Entry extends Object{
  7.  
  8. var $_fields;
  9. var $_Parent;
  10. var $_data;
  11. var $creationDate;
  12. var $_engine;
  13.  
  14. function __construct(&$parent){
  15. $this->_Parent =& $parent;
  16. $this->_fields = array();
  17. $this->_data = array();
  18.  
  19. ## Since we are not sure where the Admin object is, inspect
  20. ## all the parent objects
  21. $this->catalogueParentObjects();
  22.  
  23. if(class_exists('Administration')) $this->_engine = Administration::instance();
  24. elseif(class_exists('Frontend')) $this->_engine = Frontend::instance();
  25. else trigger_error(__('No suitable engine object found'), E_USER_ERROR);
  26.  
  27. $this->creationDate = DateTimeObj::getGMT('c'); //$this->_engine->getDateObj();
  28. }
  29.  
  30. function set($field, $value){
  31. $this->_fields[$field] = $value;
  32. }
  33.  
  34. function get($field=NULL){
  35. if($field == NULL) return $this->_fields;
  36. return $this->_fields[$field];
  37. }
  38.  
  39. function fetchAllAssociatedEntryCounts(){
  40.  
  41. $section = $this->_Parent->sectionManager->fetch($this->get('section_id'));
  42. $associated_sections = $section->fetchAssociatedSections();
  43.  
  44. if(!is_array($associated_sections) || empty($associated_sections)) return NULL;
  45.  
  46. $counts = array();
  47.  
  48. foreach($associated_sections as $as){
  49.  
  50. $field = $this->_Parent->fieldManager->fetch($as['child_section_field_id']);
  51.  
  52. $parent_section_field_id = $as['parent_section_field_id'];
  53.  
  54. $search_value = NULL;
  55.  
  56. if(!is_null($parent_section_field_id)){
  57. $search_value = $field->fetchAssociatedEntrySearchValue(
  58. $this->getData($as['parent_section_field_id']),
  59. $as['parent_section_field_id'],
  60. $this->get('id')
  61. );
  62. }
  63.  
  64. else{
  65. $search_value = $this->get('id');
  66. }
  67.  
  68. $counts[$as['child_section_id']] = $field->fetchAssociatedEntryCount($search_value);
  69.  
  70. }
  71.  
  72. return $counts;
  73.  
  74. }
  75.  
  76. function checkPostData($data, &$errors, $ignore_missing_fields=false){
  77. $errors = NULL;
  78. $status = __ENTRY_OK__;
  79.  
  80. if(!isset($this->_ParentCatalogue['sectionmanager'])) $SectionManager = new SectionManager($this->_engine);
  81. else $SectionManager = $this->_ParentCatalogue['sectionmanager'];
  82.  
  83. $section = $SectionManager->fetch($this->get('section_id'));
  84. $schema = $section->fetchFieldsSchema();
  85.  
  86. foreach($schema as $info){
  87. $result = NULL;
  88.  
  89. $field = $this->_ParentCatalogue['entrymanager']->fieldManager->fetch($info['id']);
  90.  
  91. if($ignore_missing_fields && !isset($data[$field->get('element_name')])) continue;
  92.  
  93. if(Field::__OK__ != $field->checkPostFieldData((isset($data[$info['element_name']]) ? $data[$info['element_name']] : NULL), $message, $this->get('id'))){
  94. $strict = false;
  95. $status = __ENTRY_FIELD_ERROR__;
  96.  
  97. $errors[$info['id']] = $message;
  98. }
  99.  
  100. }
  101.  
  102. return $status;
  103. }
  104.  
  105. function setDataFromPost($data, &$error, $simulate=false, $ignore_missing_fields=false){
  106.  
  107. $error = NULL;
  108.  
  109. $status = __ENTRY_OK__;
  110.  
  111. // Entry has no ID, create it:
  112. if(!$this->get('id') && $simulate == false) {
  113.  
  114. $fields = $this->get();
  115. $fields['creation_date'] = DateTimeObj::get('Y-m-d H:i:s');
  116. $fields['creation_date_gmt'] = DateTimeObj::getGMT('Y-m-d H:i:s');
  117.  
  118. $this->_engine->Database->insert($fields, 'tbl_entries');
  119. if(!$entry_id = $this->_engine->Database->getInsertID()) return __ENTRY_FIELD_ERROR__;
  120. $this->set('id', $entry_id);
  121. }
  122.  
  123. if(!isset($this->_ParentCatalogue['sectionmanager'])) $SectionManager = new SectionManager($this->_engine);
  124. else $SectionManager = $this->_ParentCatalogue['sectionmanager'];
  125.  
  126. $section = $SectionManager->fetch($this->get('section_id'));
  127.  
  128. /*if(!is_object($section)){
  129. print_r($this->get());
  130. die();
  131. }*/
  132. header('content-type: text/plain'); var_dump(debug_backtrace(false)); exit;
  133. $schema = $section->fetchFieldsSchema();
  134.  
  135. foreach($schema as $info){
  136. $result = NULL;
  137.  
  138. $field = $this->_ParentCatalogue['entrymanager']->fieldManager->fetch($info['id']);
  139.  
  140. if($ignore_missing_fields && !isset($data[$field->get('element_name')])) continue;
  141.  
  142. $result = $field->processRawFieldData(
  143. (isset($data[$info['element_name']]) ? $data[$info['element_name']] : NULL), $s, $simulate, $this->get('id'), $m
  144. );
  145.  
  146. if($s != Field::__OK__){
  147. $status = __ENTRY_FIELD_ERROR__;
  148. $error = array('field_id' => $info['id'], 'message' => $m);
  149. }
  150.  
  151. $this->setData($info['id'], $result);
  152. }
  153.  
  154. // Failed to create entry, cleanup
  155. if($status != __ENTRY_OK__ and !is_null($entry_id)) {
  156. $this->_engine->Database->delete('tbl_entries', " `id` = '$entry_id' ");
  157. }
  158.  
  159. return $status;
  160. }
  161.  
  162. function setData($field_id, $data){
  163. $this->_data[$field_id] = $data;
  164. }
  165.  
  166. function getData($field_id=NULL){
  167. if(!$field_id) return $this->_data;
  168. return $this->_data[$field_id];
  169. }
  170.  
  171. function findDefaultData(){
  172.  
  173. if(!isset($this->_ParentCatalogue['sectionmanager'])) $SectionManager = new SectionManager($this->_engine);
  174. else $SectionManager = $this->_ParentCatalogue['sectionmanager'];
  175.  
  176. $section = $SectionManager->fetch($this->get('section_id'));
  177. $schema = $section->fetchFields();
  178.  
  179. foreach($schema as $field){
  180. if(isset($this->_data[$field->get('field_id')])) continue;
  181.  
  182. $field->processRawFieldData(NULL, $result, $status, false, $this->get('id'), $message);
  183. $this->setData($field->get('field_id'), $result);
  184. }
  185.  
  186. if(!$this->get('creation_date')) $this->set('creation_date', DateTimeObj::get('c'));
  187.  
  188. if(!$this->get('creation_date_gmt')) $this->set('creation_date_gmt', DateTimeObj::getGMT('c'));
  189.  
  190. }
  191.  
  192. function commit(){
  193. $this->findDefaultData();
  194. return ($this->get('id') ? $this->_ParentCatalogue['entrymanager']->edit($this) : $this->_ParentCatalogue['entrymanager']->add($this));
  195. }
  196.  
  197. }
Add Comment
Please, Sign In to add comment