public function externalPhotoAction() { if (!$this->_helper->requireSubject()->isValid()) { return; } $user = Engine_Api::_()->core()->getSubject(); // Get photo $photo = Engine_Api::_()->getItemByGuid($this->_getParam('photo')); if (!$photo || !$photo instanceof Core_Model_Item_Abstract || empty($photo->photo_id)) { $this->_forward('requiresubject', 'error', 'core'); return; } if (!$photo->authorization()->isAllowed(null, 'view')) { $this->_forward('requireauth', 'error', 'core'); return; } // Make form $this->view->form = $form = new User_Form_Edit_ExternalPhoto(); $this->view->photo = $photo; if (!$this->getRequest()->isPost()) { return; } if (!$form->isValid($this->getRequest()->getPost())) { return; } // Process $db = $user->getTable()->getAdapter(); $db->beginTransaction(); try { // Get the owner of the photo $photoOwnerId = null; if (isset($photo->user_id)) { $photoOwnerId = $photo->user_id; } else { if (isset($photo->owner_id) && (!isset($photo->owner_type) || $photo->owner_type == 'user')) { $photoOwnerId = $photo->owner_id; } } // if it is from your own profile album do not make copies of the image if ($photo instanceof Album_Model_Photo && ($photoParent = $photo->getParent()) instanceof Album_Model_Album && $photoParent->owner_id == $photoOwnerId && $photoParent->type == 'profile') { // ensure thumb.icon and thumb.profile exist $newStorageFile = Engine_Api::_()->getItem('storage_file', $photo->file_id); $filesTable = Engine_Api::_()->getDbtable('files', 'storage'); if ($photo->file_id == $filesTable->lookupFile($photo->file_id, 'thumb.profile')) { try { $tmpFile = $newStorageFile->temporary(); $image = Engine_Image::factory(); $image->open($tmpFile)->resize(200, 400)->write($tmpFile)->destroy(); $iProfile = $filesTable->createFile($tmpFile, array('parent_type' => $user->getType(), 'parent_id' => $user->getIdentity(), 'user_id' => $user->getIdentity(), 'name' => basename($tmpFile))); $newStorageFile->bridge($iProfile, 'thumb.profile'); @unlink($tmpFile); } catch (Exception $e) { echo $e; die; } } if ($photo->file_id == $filesTable->lookupFile($photo->file_id, 'thumb.icon')) { try { $tmpFile = $newStorageFile->temporary(); $image = Engine_Image::factory(); $image->open($tmpFile); $size = min($image->height, $image->width); $x = ($image->width - $size) / 2; $y = ($image->height - $size) / 2; $image->resample($x, $y, $size, $size, 48, 48)->write($tmpFile)->destroy(); $iSquare = $filesTable->createFile($tmpFile, array('parent_type' => $user->getType(), 'parent_id' => $user->getIdentity(), 'user_id' => $user->getIdentity(), 'name' => basename($tmpFile))); $newStorageFile->bridge($iSquare, 'thumb.icon'); @unlink($tmpFile); } catch (Exception $e) { echo $e; die; } } // Set it $user->photo_id = $photo->file_id; $user->save(); // Insert activity // @todo maybe it should read "changed their profile photo" ? $action = Engine_Api::_()->getDbtable('actions', 'activity')->addActivity($user, $user, 'profile_photo_update', '{item:$subject} changed their profile photo.'); if ($action) { // We have to attach the user himself w/o album plugin Engine_Api::_()->getDbtable('actions', 'activity')->attachActivity($action, $photo); } } else { $user->setPhoto($photo); // Insert activity $action = Engine_Api::_()->getDbtable('actions', 'activity')->addActivity($user, $user, 'profile_photo_update', '{item:$subject} added a new profile photo.'); // Hooks to enable albums to work $newStorageFile = Engine_Api::_()->getItem('storage_file', $user->photo_id); $event = Engine_Hooks_Dispatcher::_()->callEvent('onUserProfilePhotoUpload', array('user' => $user, 'file' => $newStorageFile)); $attachment = $event->getResponse(); if (!$attachment) { $attachment = $newStorageFile; } if ($action) { // We have to attach the user himself w/o album plugin Engine_Api::_()->getDbtable('actions', 'activity')->attachActivity($action, $attachment); } } $db->commit(); } catch (Exception $e) { $db->rollBack(); throw $e; } return $this->_forward('success', 'utility', 'core', array('messages' => array(Zend_Registry::get('Zend_Translate')->_('Set as profile photo')), 'smoothboxClose' => true)); }
public function externalPhotoAction() { if (!$this->_helper->requireSubject()->isValid()) { return; } $user = Engine_Api::_()->core()->getSubject(); // Get photo $photo = Engine_Api::_()->getItemByGuid($this->_getParam('photo')); if (!$photo || !$photo instanceof Core_Model_Item_Collectible || empty($photo->photo_id)) { $this->_forward('requiresubject', 'error', 'core'); return; } if (!$photo->getCollection()->authorization()->isAllowed(null, 'view')) { $this->_forward('requireauth', 'error', 'core'); return; } // Make form $this->view->form = $form = new User_Form_Edit_ExternalPhoto(); $this->view->photo = $photo; if (!$this->getRequest()->isPost()) { return; } if (!$form->isValid($this->getRequest()->getPost())) { return; } // Process $db = $user->getTable()->getAdapter(); $db->beginTransaction(); $photo_id; try { $photo_id = $photo->owner_id; } catch (Exception $e) { $photo_id = $photo->user_id; } try { if ($photo_id == $user->user_id) { // if it is your own album do not make copies of the image $user->photo_id = $photo->file_id; $user->save(); $newStorageFile = Engine_Api::_()->getItem('storage_file', $user->photo_id); } else { // else copy the photo into your own collection $user->setPhoto($photo); $newStorageFile = Engine_Api::_()->getItem('storage_file', $user->photo_id); // Insert activity $action = Engine_Api::_()->getDbtable('actions', 'activity')->addActivity($user, $user, 'profile_photo_update', '{item:$subject} added a new profile photo.'); // Hooks to enable albums to work $event = Engine_Hooks_Dispatcher::_()->callEvent('onUserProfilePhotoUpload', array('user' => $user, 'file' => $newStorageFile)); $attachment = $event->getResponse(); if (!$attachment) { $attachment = $newStorageFile; } // We have to attach the user himself w/o album plugin Engine_Api::_()->getDbtable('actions', 'activity')->attachActivity($action, $attachment); } $db->commit(); return $this->_forward('success', 'utility', 'core', array('messages' => array(Zend_Registry::get('Zend_Translate')->_('Set as profile photo')), 'smoothboxClose' => true)); } catch (Exception $e) { $db->rollBack(); throw $e; } }