Exemplo n.º 1
0
 public function editAction()
 {
     if (!$this->_helper->requireUser()->isValid()) {
         return;
     }
     $viewer = $this->_helper->api()->user()->getViewer();
     $classified = Engine_Api::_()->getItem('classified', $this->_getParam('classified_id'));
     if (!Engine_Api::_()->core()->hasSubject('classified')) {
         Engine_Api::_()->core()->setSubject($classified);
     }
     $this->view->classified = $classified;
     // Check auth
     if (!$this->_helper->requireSubject()->isValid()) {
         return;
     }
     if (!$this->_helper->requireAuth()->setAuthParams($classified, $viewer, 'edit')->isValid()) {
         return;
     }
     // Get navigation
     $this->view->navigation = $navigation = Engine_Api::_()->getApi('menus', 'core')->getNavigation('classified_main', array(), 'classified_main_manage');
     // Prepare form
     $this->view->form = $form = new Classified_Form_Edit(array('item' => $classified));
     $form->removeElement('photo');
     //die("<pre>".print_r($aliasValues->toArray(),true)."</pre>");
     //$customfieldform->getFieldsValuesByAlias($classified);
     $this->view->album = $album = $classified->getSingletonAlbum();
     $this->view->paginator = $paginator = $album->getCollectiblesPaginator();
     $paginator->setCurrentPageNumber($this->_getParam('page'));
     $paginator->setItemCountPerPage(100);
     foreach ($paginator as $photo) {
         $subform = new Classified_Form_Photo_Edit(array('elementsBelongTo' => $photo->getGuid()));
         $subform->removeElement('title');
         $subform->populate($photo->toArray());
         $form->addSubForm($subform, $photo->getGuid());
         $form->cover->addMultiOption($photo->getIdentity(), $photo->getIdentity());
     }
     // Save classified entry
     $saved = $this->_getParam('saved');
     if (!$this->getRequest()->isPost() || $saved) {
         if ($saved) {
             $url = $this->_helper->url->url(array('user_id' => $viewer->getIdentity(), 'classified_id' => $classified->getIdentity()), 'classified_entry_view');
             $savedChangesNotice = Zend_Registry::get('Zend_Translate')->_("Your changes were saved. Click %s to view your listing.", '<a href="' . $url . '">here</a>');
             $form->addNotice($savedChangesNotice);
         }
         // prepare tags
         $classifiedTags = $classified->tags()->getTagMaps();
         //$form->getSubForm('custom')->saveValues();
         $tagString = '';
         foreach ($classifiedTags as $tagmap) {
             if ($tagString !== '') {
                 $tagString .= ', ';
             }
             $tagString .= $tagmap->getTag()->getTitle();
         }
         $this->view->tagNamePrepared = $tagString;
         $form->tags->setValue($tagString);
         // etc
         $form->populate($classified->toArray());
         $auth = Engine_Api::_()->authorization()->context;
         $roles = array('owner', 'owner_member', 'owner_member_member', 'owner_network', 'registered', 'everyone');
         foreach ($roles as $role) {
             if (1 === $auth->isAllowed($classified, $role, 'view')) {
                 $form->auth_view->setValue($role);
             }
             if (1 === $auth->isAllowed($classified, $role, 'comment')) {
                 $form->auth_comment->setValue($role);
             }
         }
         return;
     }
     if (!$form->isValid($this->getRequest()->getPost())) {
         return;
     }
     // Process
     // handle save for tags
     $values = $form->getValues();
     $tags = preg_split('/[,]+/', $values['tags']);
     $tags = array_filter(array_map("trim", $tags));
     $db = Engine_Db_Table::getDefaultAdapter();
     $db->beginTransaction();
     try {
         $classified->setFromArray($values);
         $classified->modified_date = date('Y-m-d H:i:s');
         $classified->tags()->setTagMaps($viewer, $tags);
         $classified->save();
         $cover = $values['cover'];
         // Process
         foreach ($paginator as $photo) {
             $subform = $form->getSubForm($photo->getGuid());
             $subValues = $subform->getValues();
             $subValues = $subValues[$photo->getGuid()];
             unset($subValues['photo_id']);
             if (isset($cover) && $cover == $photo->photo_id) {
                 $classified->photo_id = $photo->file_id;
                 $classified->save();
             }
             if (isset($subValues['delete']) && $subValues['delete'] == '1') {
                 if ($classified->photo_id == $photo->file_id) {
                     $classified->photo_id = 0;
                     $classified->save();
                 }
                 $photo->delete();
             } else {
                 $photo->setFromArray($subValues);
                 $photo->save();
             }
         }
         // Save custom fields
         $customfieldform = $form->getSubForm('fields');
         $customfieldform->setItem($classified);
         $customfieldform->saveValues();
         // CREATE AUTH STUFF HERE
         $auth = Engine_Api::_()->authorization()->context;
         $roles = array('owner', 'owner_member', 'owner_member_member', 'owner_network', 'registered', 'everyone');
         if (!empty($values['auth_view'])) {
             $auth_view = $values['auth_view'];
         } else {
             $auth_view = "everyone";
         }
         $viewMax = array_search($auth_view, $roles);
         foreach ($roles as $i => $role) {
             $auth->setAllowed($classified, $role, 'view', $i <= $viewMax);
         }
         $roles = array('owner', 'owner_member', 'owner_member_member', 'owner_network', 'registered', 'everyone');
         if (!empty($values['auth_comment'])) {
             $auth_comment = $values['auth_comment'];
         } else {
             $auth_comment = "everyone";
         }
         $commentMax = array_search($auth_comment, $roles);
         foreach ($roles as $i => $role) {
             $auth->setAllowed($classified, $role, 'comment', $i <= $commentMax);
         }
         $db->commit();
     } catch (Exception $e) {
         $db->rollBack();
         throw $e;
     }
     $db->beginTransaction();
     try {
         // Rebuild privacy
         $actionTable = Engine_Api::_()->getDbtable('actions', 'activity');
         foreach ($actionTable->getActionsByObject($classified) as $action) {
             $actionTable->resetActivityBindings($action);
         }
         $db->commit();
     } catch (Exception $e) {
         $db->rollBack();
         throw $e;
     }
     return $this->_helper->redirector->gotoRoute(array('action' => 'manage'), 'classified_general', true);
 }
Exemplo n.º 2
0
 public function editAction()
 {
     if (!$this->_helper->requireAuth()->setAuthParams(null, null, 'photo.edit')->isValid()) {
         return;
     }
     $photo = Engine_Api::_()->core()->getSubject();
     $this->view->form = $form = new Classified_Form_Photo_Edit();
     if (!$this->getRequest()->isPost()) {
         $form->populate($photo->toArray());
         return;
     }
     if (!$form->isValid($this->getRequest()->getPost())) {
         return;
     }
     // Process
     $db = Engine_Api::_()->getDbtable('photos', 'group')->getAdapter();
     $db->beginTransaction();
     try {
         $photo->setFromArray($form->getValues())->save();
         $db->commit();
     } catch (Exception $e) {
         $db->rollBack();
         throw $e;
     }
     return $this->_forward('success', 'utility', 'core', array('messages' => array('Changes saved'), 'layout' => 'default-simple', 'parentRefresh' => true, 'closeSmoothbox' => true));
 }