/** * Apply style information to object * If object has no style information yet, then create new style node * If object style information is similar to other object's style info, then apply the same style name * And if object old style information was not shared with other objects then delete old style info * Else leave old style info * Else just add new style description * * @param string $style_name * @param string $name * @param mixed $value * @param OpenDocument_StyledElement $object * @return string $style_name */ public function applyStyle($style_name, $name, $value, OpenDocument_StyledElement $object) { //check if other nodes have the same style name $nodes = $this->cursor->getElementsByTagName('*'); $count = 0; foreach ($nodes as $node) { if ($node->hasAttributeNS(self::NS_TEXT, 'style-name') && $node->getAttributeNS(self::NS_TEXT, 'style-name') == $style_name) { $count++; if ($count > 1) { break; } } } $generate = false; //get style node if ($count > 1) { $style = $this->getStyleNode($style_name)->cloneNode(true); $this->styles->appendChild($style); $generate = true; $style_name = uniqid('tmp'); //$object->generateStyleName(); $style->setAttributeNS(self::NS_STYLE, 'name', $style_name); } else { $style = $this->getStyleNode($style_name); } if (empty($style)) { if (empty($style_name)) { $generate = true; $style_name = uniqid('tmp'); } $style = $this->contentDOM->createElementNS(self::NS_STYLE, 'style'); $style->setAttributeNS(self::NS_STYLE, 'name', $style_name); $style->setAttributeNS(self::NS_STYLE, 'family', 'paragraph'); $style->setAttributeNS(self::NS_STYLE, 'parent-style-name', 'Standard'); $this->styles->appendChild($style); } $nodes = $style->getElementsByTagNameNS(self::NS_STYLE, 'text-properties'); if ($nodes->length) { $text_properties = $nodes->item(0); } else { $text_properties = $this->contentDOM->createElementNS(self::NS_STYLE, 'text-properties'); $style->appendChild($text_properties); } $text_properties->setAttribute($name, $value); //find alike style $nodes = $this->styles->getElementsByTagNameNS(self::NS_STYLE, 'style'); foreach ($nodes as $node) { if (!$style->isSameNode($node) && $this->compareChildNodes($style, $node)) { $style->parentNode->removeChild($style); return $node->getAttributeNS(self::NS_STYLE, 'name'); } } if ($generate) { $style_name = $object->generateStyleName(); $style->setAttributeNS(self::NS_STYLE, 'name', $style_name); } return $style->getAttributeNS(self::NS_STYLE, 'name'); }