| | 32 | * @var integer |
|---|
| | 33 | */ |
|---|
| | 34 | var $sitegroup = 0; |
|---|
| | 35 | |
|---|
| | 36 | /** |
|---|
| | 37 | * This method looks for one level of styling in the tree |
|---|
| | 38 | */ |
|---|
| | 39 | function probeStyle($name, $up = 0) |
|---|
| | 40 | { |
|---|
| | 41 | $qb = new MidgardQueryBuilder('midgard_style'); |
|---|
| | 42 | $qb->add_constraint('up', '=', $up); |
|---|
| | 43 | $qb->add_constraint('name', '=', $name); |
|---|
| | 44 | $qb->add_constraint('sitegroup', '=', $this->sitegroup); |
|---|
| | 45 | $styles = @$qb->execute(); |
|---|
| | 46 | |
|---|
| | 47 | if (count($styles) == 0) |
|---|
| | 48 | { |
|---|
| | 49 | // This part of the style path is missing, create |
|---|
| | 50 | $new_style = new midgard_style(); |
|---|
| | 51 | $new_style->up = $up; |
|---|
| | 52 | $new_style->name = $name; |
|---|
| | 53 | $new_style->sitegroup = $this->sitegroup; |
|---|
| | 54 | |
|---|
| | 55 | $stat = $new_style->create(); |
|---|
| | 56 | if (!$stat) |
|---|
| | 57 | { |
|---|
| | 58 | PEAR::raiseError("Failed to create Midgard style \"{$subStyleNames}\", check config directives in the Midgard conf.d file \"{$init_file}\". Error was " . mgd_errstr(), mgd_errno(), PEAR_ERROR_DIE); |
|---|
| | 59 | return null; |
|---|
| | 60 | } |
|---|
| | 61 | |
|---|
| | 62 | if ($up == 0) |
|---|
| | 63 | { |
|---|
| | 64 | // Aegir v1 compatibility fix |
|---|
| | 65 | $new_style->parameter('Display', 'Shared', 'YES'); |
|---|
| | 66 | } |
|---|
| | 67 | |
|---|
| | 68 | return $new_style; |
|---|
| | 69 | } |
|---|
| | 70 | |
|---|
| | 71 | return $styles[0]; |
|---|
| | 72 | } |
|---|
| | 73 | |
|---|
| | 74 | function probeSitegroup() |
|---|
| | 75 | { |
|---|
| | 76 | $sitegroup_config = $this->config->get('midgard_sitegroup'); |
|---|
| | 77 | if ($sitegroup_config == 'root') |
|---|
| | 78 | { |
|---|
| | 79 | // PEAR doesn't allow 0 values so we have to type root manually |
|---|
| | 80 | $this->sitegroup = 0; |
|---|
| | 81 | return true; |
|---|
| | 82 | } |
|---|
| | 83 | |
|---|
| | 84 | $sitegroup = mgd_get_sitegroup($sitegroup_config); |
|---|
| | 85 | if ($sitegroup) |
|---|
| | 86 | { |
|---|
| | 87 | // We found SG from the database |
|---|
| | 88 | $this->sitegroup = $sitegroup->id; |
|---|
| | 89 | return true; |
|---|
| | 90 | } |
|---|
| | 91 | |
|---|
| | 92 | PEAR::raiseError("Failed to load Midgard sitegroup \"{$sitegroup_config}\". Error: " . mgd_errstr(), mgd_errno(), PEAR_ERROR_DIE); |
|---|
| | 93 | return false; |
|---|
| | 94 | } |
|---|
| | 95 | |
|---|
| | 96 | /** |
|---|
| 53 | | /* |
|---|
| 54 | | Get style elements path from baseinstalldir attribute |
|---|
| 55 | | ie. /template_Textual/layouts |
|---|
| 56 | | */ |
|---|
| 57 | | $parts = explode("/",$atts["baseinstalldir"]); |
|---|
| 58 | | $path_items = array(); |
|---|
| 59 | | for($i=0;$i<count($parts);$i++) { |
|---|
| 60 | | if(!empty($parts[$i])) |
|---|
| 61 | | $path_items[] = $parts[$i]; |
|---|
| | 127 | /** |
|---|
| | 128 | * Get style elements path from baseinstalldir attribute |
|---|
| | 129 | * ie. /template_Textual/layouts |
|---|
| | 130 | */ |
|---|
| | 131 | $style_parts = explode('/', $atts['baseinstalldir']); |
|---|
| | 132 | $style = null; |
|---|
| | 133 | $up = 0; |
|---|
| | 134 | foreach ($style_parts as $style) |
|---|
| | 135 | { |
|---|
| | 136 | // Sanitize the style names |
|---|
| | 137 | $style_name = str_replace('/', '_', trim($style)); |
|---|
| | 138 | if (empty($style_name)) |
|---|
| | 139 | { |
|---|
| | 140 | // This part doesn't have a proper name, skip |
|---|
| | 141 | continue; |
|---|
| | 142 | } |
|---|
| | 143 | |
|---|
| | 144 | $style = $this->probeStyle($style_name, $up); |
|---|
| | 145 | |
|---|
| | 146 | if (!is_null($style)) |
|---|
| | 147 | { |
|---|
| | 148 | $up = $style->id; |
|---|
| | 149 | } |
|---|
| 63 | | |
|---|
| 64 | | /* |
|---|
| 65 | | We assume that this is the root style |
|---|
| 66 | | ie. template_Textual |
|---|
| 67 | | */ |
|---|
| 68 | | $rootStyleName = $path_items[0]; |
|---|
| 69 | | |
|---|
| 70 | | /* |
|---|
| 71 | | Collect rest of the path to own array, |
|---|
| 72 | | we will check if exists or create all |
|---|
| 73 | | these. |
|---|
| 74 | | */ |
|---|
| 75 | | $subStyleNames = array(); |
|---|
| 76 | | for($i=1;$i<count($path_items);$i++) { |
|---|
| 77 | | $subStyleNames[] = $path_items[$i]; |
|---|
| 78 | | } |
|---|
| 79 | | |
|---|
| 80 | | // Initialize Midgard session |
|---|
| 81 | | mgd_config_init($init_file); |
|---|
| 82 | | |
|---|
| 83 | | /* |
|---|
| 84 | | Theres no substyles so lets create the element in ROOT |
|---|
| 85 | | */ |
|---|
| 86 | | if(empty($subStyleNames)) { |
|---|
| 87 | | |
|---|
| 88 | | $qb = new MidgardQueryBuilder('midgard_style'); |
|---|
| 89 | | $qb->add_constraint('up', '=', 0); |
|---|
| 90 | | $qb->add_constraint('name', '=', $style_name); |
|---|
| 91 | | $styles = @$qb->execute(); |
|---|
| 92 | | |
|---|
| 93 | | if (count($styles) == 0) |
|---|
| 94 | | { |
|---|
| 95 | | // Create missing style template |
|---|
| 96 | | $new_style = new midgard_style(); |
|---|
| 97 | | $new_style->up = 0; |
|---|
| 98 | | $new_style->name = $style_name; |
|---|
| 99 | | $stat = $new_style->create(); |
|---|
| 100 | | if (!$stat) |
|---|
| 101 | | { |
|---|
| 102 | | PEAR::raiseError("Failed to create Midgard style \"{$style_name}\", check config directives in the Midgard conf.d file \"{$init_file}\". Error was " . mgd_errstr()); |
|---|
| 103 | | } |
|---|
| 104 | | $style = new midgard_style(); |
|---|
| 105 | | $style->get_by_id($new_style->id); |
|---|
| 106 | | } |
|---|
| 107 | | else |
|---|
| 108 | | { |
|---|
| 109 | | $style = $styles[0]; |
|---|
| 110 | | } |
|---|
| 111 | | |
|---|
| 112 | | // Aegir v1 compatibility fix |
|---|
| 113 | | $style->parameter('Display', 'Shared', 'YES'); |
|---|
| 114 | | |
|---|
| | 151 | |
|---|
| | 152 | if (!is_null($style)) |
|---|
| | 153 | { |
|---|
| | 154 | // We have the style in DB, update the element contents |
|---|
| 144 | | PEAR::raiseError("Failed to update element \"{$element_name}\" under Midgard style \"{$style->name}\", error " . mgd_errstr()); |
|---|
| 145 | | die(); |
|---|
| 146 | | } |
|---|
| 147 | | |
|---|
| 148 | | } else { |
|---|
| 149 | | |
|---|
| 150 | | $qb = new MidgardQueryBuilder('midgard_style'); |
|---|
| 151 | | $qb->add_constraint('name', '=', $rootStyleName); |
|---|
| 152 | | $styles = @$qb->execute(); |
|---|
| 153 | | |
|---|
| 154 | | if(count($styles) == 0) { |
|---|
| 155 | | PEAR::raiseError("Failed to find Midgard style \"{$rootStyleName}\", check config directives in the Midgard conf.d file \"{$init_file}\". Error was " . mgd_errstr()); |
|---|
| 156 | | } else { |
|---|
| 157 | | $root_style = $styles[0]; |
|---|
| 158 | | |
|---|
| 159 | | /* |
|---|
| 160 | | We loop through all the substyles |
|---|
| 161 | | we have collected and check if they exist, |
|---|
| 162 | | Create all that doesn't and finally |
|---|
| 163 | | save the element under the last substyle |
|---|
| 164 | | */ |
|---|
| 165 | | |
|---|
| 166 | | $subStyleCount = count($subStyleNames); |
|---|
| 167 | | $lastSubStyle = ''; |
|---|
| 168 | | $tmpIDs = array(); |
|---|
| 169 | | for($i=0;$i<$subStyleCount;$i++) { |
|---|
| 170 | | $parentID = ($i==0)?$root_style->id:$tmpIDs[$i-1]; |
|---|
| 171 | | |
|---|
| 172 | | $qb = new MidgardQueryBuilder('midgard_style'); |
|---|
| 173 | | $qb->add_constraint('up', '=', $parentID); |
|---|
| 174 | | $qb->add_constraint('name', '=', $subStyleNames[$i]); |
|---|
| 175 | | $sstyles = @$qb->execute(); |
|---|
| 176 | | |
|---|
| 177 | | if (count($sstyles) == 0) |
|---|
| 178 | | { |
|---|
| 179 | | $new_sstyle = new midgard_style(); |
|---|
| 180 | | $new_sstyle->up = $parentID; |
|---|
| 181 | | $new_sstyle->name = $subStyleNames[$i]; |
|---|
| 182 | | $tmpIDs[$i] = $new_sstyle->id; |
|---|
| 183 | | |
|---|
| 184 | | $stat = $new_sstyle->create(); |
|---|
| 185 | | if (!$stat) |
|---|
| 186 | | { |
|---|
| 187 | | PEAR::raiseError("Failed to create Midgard style \"{$subStyleNames}\", check config directives in the Midgard conf.d file \"{$init_file}\". Error was " . mgd_errstr()); |
|---|
| 188 | | } |
|---|
| 189 | | $sstyle = new midgard_style(); |
|---|
| 190 | | $sstyle->get_by_id($new_sstyle->id); |
|---|
| 191 | | } |
|---|
| 192 | | else |
|---|
| 193 | | { |
|---|
| 194 | | $sstyle = $sstyles[0]; |
|---|
| 195 | | $tmpIDs[$i] = $sstyle->id; |
|---|
| 196 | | } |
|---|
| 197 | | |
|---|
| 198 | | if($i==$subStyleCount-1) { |
|---|
| 199 | | $lastSubStyle = $sstyle; |
|---|
| 200 | | } |
|---|
| 201 | | } |
|---|
| 202 | | |
|---|
| 203 | | $qb = new MidgardQueryBuilder('midgard_element'); |
|---|
| 204 | | $qb->add_constraint('style', '=', $lastSubStyle->id); |
|---|
| 205 | | $qb->add_constraint('name', '=', $element_name); |
|---|
| 206 | | $elements = @$qb->execute(); |
|---|
| 207 | | |
|---|
| 208 | | if (count($elements) == 0) |
|---|
| 209 | | { |
|---|
| 210 | | // Create missing element |
|---|
| 211 | | $new_element = new midgard_element(); |
|---|
| 212 | | $new_element->style = $lastSubStyle->id; |
|---|
| 213 | | $new_element->name = $element_name; |
|---|
| 214 | | $stat = $new_element->create(); |
|---|
| 215 | | if (!$stat) |
|---|
| 216 | | { |
|---|
| 217 | | PEAR::raiseError("Failed to create element \"{$element_name}\" under Midgard style \"{$lastSubStyle->name}\", error " . mgd_errstr()); |
|---|
| 218 | | } |
|---|
| 219 | | $element = new midgard_element(); |
|---|
| 220 | | $element->get_by_id($new_element->id); |
|---|
| 221 | | } |
|---|
| 222 | | else |
|---|
| 223 | | { |
|---|
| 224 | | $element = $elements[0]; |
|---|
| 225 | | } |
|---|
| 226 | | |
|---|
| 227 | | $element->value = file_get_contents($installing_paths[3]); |
|---|
| 228 | | $stat = $element->update(); |
|---|
| 229 | | if (!$stat) |
|---|
| 230 | | { |
|---|
| 231 | | PEAR::raiseError("Failed to update element \"{$element_name}\" under Midgard style \"{$style->name}\", error " . mgd_errstr()); |
|---|
| 232 | | die(); |
|---|
| 233 | | } |
|---|
| 234 | | |
|---|
| | 186 | PEAR::raiseError("Failed to update element \"{$element_name}\" under Midgard style \"{$style->name}\", error " . mgd_errstr(), mgd_errno(), PEAR_ERROR_DIE); |
|---|