forked from pimcore/pimcore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Resource.php
138 lines (121 loc) · 4.84 KB
/
Resource.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
/**
* Pimcore
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://www.pimcore.org/license
*
* @copyright Copyright (c) 2009-2010 elements.at New Media Solutions GmbH (http://www.elements.at)
* @license http://www.pimcore.org/license New BSD License
*/
class Search_Backend_Data_Resource extends Pimcore_Model_Resource_Abstract {
/**
* @param Element_Interface $element
* @return void
*/
public function getForElement($element) {
try {
if($element instanceof Document){
$maintype = "document";
} else if ($element instanceof Asset){
$maintype = "asset";
} else if ($element instanceof Object_Abstract){
$maintype = "object";
} else {
throw Exception("unknown type of element with id [ ".$element->getId()." ] ");
}
$data = $this->db->fetchRow("SELECT * FROM search_backend_data WHERE id= ? AND maintype = ? ", array($element->getId(),$maintype));
if(is_array($data)){
$this->assignVariablesToModel($data);
$this->model->setId(new Search_Backend_Data_Id($element));
}
}
catch (Exception $e){}
}
/**
*
* @param string $name
* @return void
*/
public function save() {
try {
try {
$this->create();
} catch (Exception $e) {
//probably dulplicate
$this->update();
}
} catch (Exception $e) {
Logger::error($e);
}
}
/**
* Create a new record in database
*
* @return boolean
*/
protected function create() {
if($this->model->getId() instanceof Search_Backend_Data_Id){
$this->db->insert("search_backend_data", array(
"id" => $this->model->getId()->getId(),
"fullpath" => $this->model->getFullPath(),
"maintype" => $this->model->getId()->getType(),
"type" => $this->model->getId()->getType(),
"subtype" => $this->model->getSubtype(),
"published" => $this->model->isPublished(),
"creationdate" => $this->model->getCreationDate(),
"modificationdate" => $this->model->getmodificationDate(),
"userowner" => $this->model->getUserOwner(),
"usermodification" => $this->model->getUserModification(),
"data" => $this->model->getData(),
"localizeddata" => $this->model->getLocalizedData(),
"fieldcollectiondata" => $this->model->getFieldcollectionData(),
"properties" => $this->model->getProperties()
));
} else {
Logger::alert("Cannot create Search_Backend_Data, ID is empty");
}
}
/**
* Save changes to database
*
* @return void
*/
protected function update() {
if($this->model->getId() instanceof Search_Backend_Data_Id){
$data["id"] = $this->model->getId()->getId();
$data["fullpath"] = $this->model->getFullPath();
$data["maintype"] = $this->model->getId()->getType();
$data["type"] = $this->model->getType();
$data["subtype"] = $this->model->getSubtype();
$data["published"] = $this->model->isPublished();
$data["creationdate"] = $this->model->getCreationDate();
$data["modificationdate"] = $this->model->getmodificationDate();
$data["userowner"] = $this->model->getUserOwner();
$data["usermodification"] = $this->model->getUserModification();
$data["data"] = $this->model->getData();
$data["localizeddata"] = $this->model->getLocalizedData();
$data["fieldcollectiondata"] = $this->model->getFieldcollectionData();
$data["properties"] = $this->model->getProperties();
$this->db->update("search_backend_data", $data, "id='" . $data["id"] . "' AND maintype ='" . $data["maintype"] . "'");
} else {
Logger::alert("Cannot update Search_Backend_Data, ID is empty");
}
}
/**
* Deletes from database
*
* @return void
*/
public function delete() {
if($this->model->getId() instanceof Search_Backend_Data_Id ){
$this->db->delete("search_backend_data", "id='" . $this->model->getId()->getId() . "' AND maintype ='" .$this->model->getId()->getType() . "'");
} else {
Logger::alert("Cannot delete Search_Backend_Data, ID is empty");
}
}
}