[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/lib/alfresco/Service/ -> ContentData.php (source)

   1  <?php
   2  /*

   3   * Copyright (C) 2005-2010 Alfresco Software Limited.

   4   *

   5   * This file is part of Alfresco

   6   *

   7   * Alfresco is free software: you can redistribute it and/or modify

   8   * it under the terms of the GNU Lesser General Public License as published by

   9   * the Free Software Foundation, either version 3 of the License, or

  10   * (at your option) any later version.

  11   *

  12   * Alfresco is distributed in the hope that it will be useful,

  13   * but WITHOUT ANY WARRANTY; without even the implied warranty of

  14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the

  15   * GNU Lesser General Public License for more details.

  16   *

  17   * You should have received a copy of the GNU Lesser General Public License

  18   * along with Alfresco. If not, see <http://www.gnu.org/licenses/>.

  19   */
  20  
  21  require_once($CFG->libdir."/alfresco/Service/Functions.php");
  22  
  23  class ContentData extends BaseObject
  24  {    
  25      private $_isPopulated = false;
  26      private $_isDirty = false;
  27      
  28      private $_node;
  29      private $_property;
  30      
  31      private $_mimetype;
  32      private $_size;
  33      private $_encoding;    
  34      private $_url;
  35      private $_newContent;
  36      private $_newFileContent;
  37      
  38  	public function __construct($node, $property, $mimetype=null, $encoding=null, $size=-1)
  39      {
  40          $this->_node = $node;
  41          $this->_property = $property;
  42          $this->_mimetype = $mimetype;
  43          $this->_encoding = $encoding;
  44          if ($size != -1)
  45          {
  46              $this->size = $size;
  47          }        
  48          $this->_isPopulated = false;
  49      }    
  50      
  51  	public function setPropertyDetails($node, $property)
  52      {
  53          $this->_node = $node;
  54          $this->_property = $property;
  55      }
  56      
  57  	public function __toString()
  58      {
  59          $this->populateContentData();
  60          return "mimetype=".$this->mimetype."|encoding=".$this->encoding."|size=".$this->size;
  61      }
  62      
  63  	public function getNode()
  64      {
  65          return $this->_node;
  66      }
  67      
  68  	public function getProperty()
  69      {
  70          return $this->_property;
  71      }
  72      
  73  	public function getIsDirty()
  74      {
  75          return $this->_isDirty;
  76      }
  77      
  78  	public function getMimetype()
  79      {
  80          $this->populateContentData();
  81          return $this->_mimetype;
  82      }
  83      
  84  	public function setMimetype($mimetype)
  85      {
  86          $this->populateContentData();
  87          $this->_mimetype = $mimetype;
  88      }
  89      
  90  	public function getSize()
  91      {
  92          $this->populateContentData();
  93          return $this->_size;
  94      }
  95      
  96  	public function getEncoding()
  97      {
  98          $this->populateContentData();
  99          return $this->_encoding;
 100      }
 101      
 102  	public function setEncoding($encoding)
 103      {
 104          $this->populateContentData();
 105          $this->_encoding = $encoding;
 106      }
 107      
 108  	public function getUrl()
 109      {
 110          // TODO what should be returned if the content has been updated??

 111          
 112          $this->populateContentData();
 113          $result = null;
 114          if ($this->_url != null)
 115          {    
 116              $result = $this->_url."?ticket=".$this->_node->session->ticket;
 117          }
 118          return $result;
 119      }
 120      
 121  	public function getGuestUrl()
 122      {
 123          // TODO what should be returned if the content has been updated??

 124          
 125          $this->populateContentData();    
 126          $result = null;
 127          if ($this->_url != null)
 128          {    
 129              $result = $this->_url."?guest=true";
 130          }
 131          return $result;
 132      }
 133      
 134  	public function getContent()
 135      {
 136          $this->populateContentData();
 137          
 138          $result = null;            
 139          if ($this->_isDirty == true)
 140          {
 141              if ($this->_newFileContent != null)
 142              {
 143                  $handle = fopen($this->_newFileContent, "rb");
 144                  $result = stream_get_contents($handle);
 145                  fclose($handle);    
 146              }
 147              else if ($this->_newContent != null)
 148              {
 149                  $result = $this->_newContent;    
 150              }    
 151          }
 152          else
 153          {
 154              if ($this->getUrl() != null)
 155              {
 156                  $handle = fopen($this->getUrl(), "rb");
 157                  $result = stream_get_contents($handle);
 158                  fclose($handle);    
 159              }
 160          }
 161          return $result;
 162      }
 163      
 164  	public function setContent($content)
 165      {
 166          $this->populateContentData();
 167          $this->_isDirty = true;
 168          $this->_newContent = $content;            
 169      }
 170      
 171  	public function writeContentFromFile($fileName)
 172      {
 173          $this->populateContentData();
 174          $this->_isDirty = true;
 175          $this->_newFileContent = $fileName;        
 176      }
 177      
 178  	public function readContentToFile($fileName)
 179      {
 180          $handle = fopen($fileName, "wb");
 181          fwrite($handle, $this->getContent());
 182          fclose($handle);    
 183      }
 184      
 185  	public function onBeforeSave(&$statements, $where)
 186      {
 187          if ($this->_isDirty == true)
 188          {
 189              // Check mimetype has been set

 190              if ($this->_mimetype == null)
 191              {
 192                  throw new Exception("A mime type for the content property ".$this->_property." on node ".$this->_node->__toString()." must be set");
 193              }
 194              
 195              // If a file has been specified then read content from there

 196              //$content = null;

 197              if ($this->_newFileContent != null)
 198              {
 199                  // Upload the content to the repository

 200                  $contentData = upload_file($this->node->session, $this->_newFileContent, $this->_mimetype, $this->_encoding);
 201                  
 202                  // Set the content property value

 203                  $this->addStatement(
 204                      $statements, 
 205                      "update", 
 206                      array("property" => array(
 207                                  "name" => $this->property,
 208                                  "isMultiValue" => false,
 209                                  "value" => $contentData)) + $where);    
 210              }
 211              else
 212              {
 213                  // Add the writeContent statement

 214                  $this->addStatement(
 215                          $statements, 
 216                          "writeContent", 
 217                          array(
 218                              "property" => $this->_property,
 219                              "content" => $this->_newContent,
 220                              "format" => array(
 221                                  "mimetype" => $this->_mimetype,
 222                                  "encoding" => $this->_encoding)) + 
 223                              $where); 
 224              } 
 225          }
 226      }
 227      
 228  	public function onAfterSave()
 229      {
 230          $this->_isDirty = false;
 231          $this->_isPopulated = false;
 232          $this->_mimetype = null;
 233          $this->__size = null;
 234          $this->__encoding = null;    
 235          $this->__url = null;
 236          $this->__newContent = null;
 237      }
 238      
 239  	private function populateContentData()
 240      {
 241          //echo "isPopulated:".$this->_isPopulated."; node:".$this->_node."; property:".$this->_property."<br>";

 242          if ($this->_isPopulated == false && $this->_node != null && $this->_property != null && $this->_node->isNewNode == false)
 243          {            
 244              $result = $this->_node->session->contentService->read( array(
 245                                                                  "items" => array(
 246                                                                      "nodes" => array(
 247                                                                          "store" => $this->_node->store->__toArray(),
 248                                                                          "uuid" => $this->_node->id)),            
 249                                                                  "property" => $this->_property) );
 250              if (isset($result->content) == true)
 251              {                                        
 252                  if (isset($result->content->length) == true)
 253                  {                                                                
 254                      $this->_size = $result->content->length;
 255                  }
 256                  if (isset($result->content->format->mimetype) == true)
 257                  {                                                                
 258                      $this->_mimetype = $result->content->format->mimetype;
 259                  }
 260                  if (isset($result->content->format->encoding) == true)
 261                  {
 262                      $this->_encoding = $result->content->format->encoding;
 263                  }
 264                  if (isset($result->content->url) == true)
 265                  {
 266                      $this->_url = $result->content->url;
 267                  }
 268              }                                                            
 269              
 270              $this->_isPopulated = true;
 271          }
 272      }
 273      
 274  	private function addStatement(&$statements, $statement, $body)
 275      {        
 276          $result = array();    
 277          if (array_key_exists($statement, $statements) == true)    
 278          {
 279              $result = $statements[$statement];
 280          }
 281          $result[] = $body;
 282          $statements[$statement] = $result;
 283      }
 284  }
 285  ?>


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1