Changeset 19816
- Timestamp:
- 07/02/09 09:52:05 (9 months ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
plugins/csDoctrineActAsAttachablePlugin/branches/1.3/lib/template/Attachable.php
r19666 r19816 12 12 { 13 13 protected $_options = array('types' => array(), 14 'attachableModels' => array());14 'attachableModels' => array()); 15 15 16 16 /** … … 39 39 * @return Doctrine_Query 40 40 */ 41 public function getAttachmentsQuery()42 {43 $object = $this->getInvoker();44 45 return Doctrine::getTable('Attachment')41 public function getAttachmentsQuery() 42 { 43 $object = $this->getInvoker(); 44 45 return Doctrine::getTable('Attachment') 46 46 ->createQuery() 47 ->addWhere('Attachment.object_class = ?', get_class($object))48 ->addWhere('Attachment.object_id = ?', $object->getId());49 }47 ->addWhere('Attachment.object_class = ?', get_class($object)) 48 ->addWhere('Attachment.object_id = ?', $object->getId()); 49 } 50 50 51 51 /** … … 54 54 * @return Doctrine_Collection|false 55 55 */ 56 public function getAttachments()57 {58 $object = $this->getInvoker();59 60 return $object->getAttachmentsQuery()->execute();61 }56 public function getAttachments() 57 { 58 $object = $this->getInvoker(); 59 60 return $object->getAttachmentsQuery()->execute(); 61 } 62 62 63 63 /** … … 70 70 * @see addAttachment 71 71 */ 72 public function setAttachments($attachments)73 {74 foreach ($attachments as $attachment)75 {76 $this->addAttachment($attachment);77 }72 public function setAttachments($attachments) 73 { 74 foreach ($attachments as $attachment) 75 { 76 $this->addAttachment($attachment); 77 } 78 78 } 79 79 … … 83 83 * @return boolean 84 84 */ 85 public function hasAttachments()86 {87 return ($this->getAttachments()->count() > 0);88 }85 public function hasAttachments() 86 { 87 return ($this->getAttachments()->count() > 0); 88 } 89 89 90 90 /** … … 93 93 * @return boolean 94 94 */ 95 public function hasAttachmentsOfType($type)96 {97 return ($this->getAttachmentsByType($type)->count() > 0);98 }95 public function hasAttachmentsOfType($type) 96 { 97 return ($this->getAttachmentsByType($type)->count() > 0); 98 } 99 99 100 100 /** … … 105 105 * @return void 106 106 */ 107 public function addAttachment(Attachment $attachment)108 {109 $object = $this->getInvoker();110 111 $attachment->setObjectClass(get_class($object));112 if(!$object['id'])113 {114 $object->save();115 }116 $attachment->setObjectId($object->getId());117 $attachment->save();118 }119 120 /**107 public function addAttachment(Attachment $attachment) 108 { 109 $object = $this->getInvoker(); 110 111 $attachment->setObjectClass(get_class($object)); 112 if(!$object['id']) 113 { 114 $object->save(); 115 } 116 $attachment->setObjectId($object->getId()); 117 $attachment->save(); 118 } 119 120 /** 121 121 * get record attachments of specific type 122 122 * … … 125 125 * @return Doctrine_Collection 126 126 */ 127 public function getAttachmentsByType($type)128 {129 if(in_array($type, $this->_options['types']))130 {131 return Doctrine::getTable('Attachment')127 public function getAttachmentsByType($type) 128 { 129 if(in_array($type, $this->_options['types'])) 130 { 131 return Doctrine::getTable('Attachment') 132 132 ->createQuery() 133 133 ->where('object_class = ?', get_class($this->getInvoker())) … … 135 135 ->andWhere('type = ?', strtolower($type)) 136 136 ->execute(); 137 }138 $table = strtolower($type) == 'other' ? 'Attachment' : sfInflector::classify($type.'_attachment');139 140 return new Doctrine_Collection($table);141 }137 } 138 $table = strtolower($type) == 'other' ? 'Attachment' : sfInflector::classify($type.'_attachment'); 139 140 return new Doctrine_Collection($table); 141 } 142 142 143 143 /** … … 148 148 * @see getAttachableConfig() 149 149 */ 150 public function getSupportedAttachmentTypes()151 {152 return $this->getAttachableConfig('types');153 }150 public function getSupportedAttachmentTypes() 151 { 152 return $this->getAttachableConfig('types'); 153 } 154 154 155 155 /** … … 160 160 * @return mixed 161 161 */ 162 public function getAttachableConfig($index = null)163 {164 if($index)165 {166 return $this->_options[$index];167 }168 return $this->_options;169 }162 public function getAttachableConfig($index = null) 163 { 164 if($index) 165 { 166 return $this->_options[$index]; 167 } 168 return $this->_options; 169 } 170 170 171 171 /** … … 176 176 * @see getAttachmentsByType() 177 177 */ 178 public function getVideoAttachments()179 {180 return $this->getAttachmentsByType('Video');181 }178 public function getVideoAttachments() 179 { 180 return $this->getAttachmentsByType('Video'); 181 } 182 182 183 183 /** … … 188 188 * @see getAttachmentsByType() 189 189 */ 190 public function getImageAttachments()191 {192 return $this->getAttachmentsByType('Image');193 }190 public function getImageAttachments() 191 { 192 return $this->getAttachmentsByType('Image'); 193 } 194 194 195 195 /** … … 200 200 * @see getAttachmentsByType() 201 201 */ 202 public function getDocumentAttachments()203 {204 return $this->getAttachmentsByType('Document');202 public function getDocumentAttachments() 203 { 204 return $this->getAttachmentsByType('Document'); 205 205 } 206 206 … … 212 212 * @see getAttachmentsByType() 213 213 */ 214 public function getOtherAttachments()215 {216 return $this->getAttachmentsByType('Other');217 }214 public function getOtherAttachments() 215 { 216 return $this->getAttachmentsByType('Other'); 217 } 218 218 219 219 /** … … 224 224 * @see getAttachmentsByType() 225 225 */ 226 public function getModelAttachments() 227 { 228 return $this->getAttachmentsByType('Model'); 229 } 230 231 //This method doesn't work for some reason! 232 public function __call($method, $arguments) 233 { 234 foreach ($this->_options['types'] as $type) 235 { 236 if(sfInflector::camelize('get_'.$type.'_attachments') == $method) 237 { 238 return $this->getAttachmentsByType($type); 239 } 240 } 241 242 parent::__call($method, $arguments); 243 } 226 public function getModelAttachments() 227 { 228 return $this->getAttachmentsByType('Model'); 229 } 244 230 }

