Horde_Kolab_Storage_Object::save PHP Method

save() public method

Store the modified object in the backend.
public save ( Horde_Kolab_Storage_Object_Writer $data ) : boolean | string
$data Horde_Kolab_Storage_Object_Writer The data writer.
return boolean | string The return value of the append operation.
    public function save(Horde_Kolab_Storage_Object_Writer $data)
    {
        list($headers, $body) = $this->_getDriver()->fetchComplete($this->_getFolder(), $this->getBackendId());
        $mime_id = Horde_Kolab_Storage_Object_MimeType::matchMimePartToObjectType($body, $this->getType());
        if ($mime_id === false) {
            throw new Horde_Kolab_Storage_Object_Exception(sprintf('Missing expected mime type (%s) in object "%s" in folder "%s"!', Horde_Kolab_Storage_Object_MimeType::getMimeTypeFromObjectType($this->getType()), $this->getBackendId(), $this->_getFolder()));
        }
        $this->_content = $body->getPart($mime_id)->getContents(array('stream' => true));
        $body->alterPart($mime_id, $this->createFreshKolabPart($data->save($this)));
        $body->buildMimeIds();
        // Update attachments.
        if (isset($this['_attachments'])) {
            foreach ($this['_attachments'] as $name => $attachment) {
                foreach ($body->getParts() as $part) {
                    if ($part->getName() === $name) {
                        $body->removePart($part->getMimeId());
                        break;
                    }
                }
                if (!is_null($attachment)) {
                    $part = new Horde_Mime_Part();
                    $part->setType($attachment['type']);
                    $part->setContents($attachment['content']);
                    $part->setName($name);
                    $body->addPart($part);
                }
                $body->buildMimeIds();
            }
        }
        $this->_mime_part_id = Horde_Kolab_Storage_Object_MimeType::matchMimePartToObjectType($body, $this->getType());
        $old_uid = $this->getBackendId();
        // Save message.
        $result = $this->_appendMessage($body, $headers);
        $this->_getDriver()->deleteMessages($this->_getFolder(), array($old_uid));
        $this->_getDriver()->expunge($this->_getFolder());
        return $result;
    }

Usage Example

Example #1
0
    public function testStore()
    {
        $driver = new Horde_Kolab_Storage_Stub_Driver('user');
        $driver->setMessage('INBOX', 1, file_get_contents(__DIR__ . '/../../../../fixtures/note.eml'));
        $factory = new Horde_Kolab_Format_Factory();
        $writer = new Horde_Kolab_Storage_Object_Writer_Format($factory);
        $object = new Horde_Kolab_Storage_Object();
        $object->setDriver($driver);
        $folder = $this->getMock('Horde_Kolab_Storage_Folder');
        $folder->expects($this->once())->method('getPath')->will($this->returnValue('INBOX'));
        $folder->expects($this->once())->method('getType')->will($this->returnValue('note'));
        $structure = $driver->fetchComplete('INBOX', 1);
        $object->load(1, $folder, $writer, $structure[1]);
        $object->setData(array('summary' => 'NEW', 'description' => 'test', 'uid' => 'ABC1234'));
        $object->save($writer);
        $result = $driver->messages['INBOX'][2];
        $result = preg_replace(array('/=20/', '/Date: .*/', '/boundary=".*"/', '/--=_.*/', '/<creation-date>[^<]*/', '/<last-modification-date>[^<]*/', '/\\r\\n/', '/=\\n/'), array(' ', 'Date: ', 'boundary=""', '--=_', '<creation-date>', '<last-modification-date>', "\n", ''), $result);
        $this->assertEquals('From: user
To: user
Date: 
Subject: ABC1234
User-Agent: Horde::Kolab::Storage v@version@
MIME-Version: 1.0
X-Kolab-Type: application/x-vnd.kolab.note
Content-Type: multipart/mixed; boundary="";
 name="Kolab Groupware Data"
Content-Disposition: attachment; filename="Kolab Groupware Data"

This message is in MIME format.

--=_
Content-Type: text/plain; charset=utf-8; name="Kolab Groupware Information"
Content-Disposition: inline; filename="Kolab Groupware Information"

This is a Kolab Groupware object. To view this object you will need an email
client that understands the Kolab Groupware format. For a list of such email
clients please visit http://www.kolab.org/content/kolab-clients
--=_
Content-Type: application/x-vnd.kolab.note; name=kolab.xml
Content-Disposition: inline; x-kolab-type=xml; filename=kolab.xml

<?xml version="1.0" encoding="UTF-8"?>
<note version="1.0">
  <uid>ABC1234</uid>
  <body/>
  <categories/>
  <creation-date></creation-date>
  <last-modification-date></last-modification-date>
  <sensitivity>public</sensitivity>
  <product-id>Horde_Kolab_Format_Xml-@version@ (api version: 2)</product-id>
  <summary>NEW</summary>
  <x-test>other client</x-test>
  <background-color>#000000</background-color>
  <foreground-color>#ffff00</foreground-color>
</note>

--=_
', $result);
    }
All Usage Examples Of Horde_Kolab_Storage_Object::save