Beispiel #1
0
 public function begin()
 {
     $root = \Roles::retrieveRoot();
     $this->roles = $root->getDescendants();
 }
Beispiel #2
0
 /**
  * @param \Roles $role
  * @param $error
  * @return bool|\Roles
  * @throws \Exception
  */
 protected function _save(\Roles $role, &$error)
 {
     $current_status = $role->getStatus();
     $data = $this->post('role', 'ARRAY', []);
     $role->hydrate($data);
     if (!isset($data['admin_access'])) {
         $role->setAdminAccess(0);
     }
     $parentId = $this->post('role_parent', 'INT', 0);
     if ($parentId == 0) {
         $parent = \Roles::retrieveRoot();
     } else {
         $parent = \Roles::retrieveById($parentId);
         if (!$parent) {
             $error['parent'] = array(t('Role parent not found with id:' . $parentId));
             return false;
         }
     }
     $is_new = $role->isNew();
     $role->beginTransaction();
     try {
         if ($is_new) {
             //always save
             $role->setParentId($parent->getId());
             $role->setStatus($parent->getStatus());
             $role->insertAsLastChildOf($parent);
             //dispatch event
             $this->dispatch('onCreateNewRole', new CMSBackendEvent($this, ['role' => $role]));
         } else {
             $currentParent = $role->getParent();
             if ($parent->getId() == $role->getId()) {
                 //something f****d
                 $error['parent'] = t('Could not move to child of itself!');
             }
             if ($currentParent->getId() != $parent->getId()) {
                 //move tree
                 $role->moveToLastChildOf($parent);
                 $role->setParentId($parent->getId());
                 $role->setStatus($parent->getStatus());
                 if (!$role->save()) {
                     //save information first
                     $this->dispatch('onUpdateRole', new CMSBackendEvent($this, ['role' => $role]));
                     if (!$role->isValid()) {
                         $failures = $role->getValidationFailures();
                         foreach ($failures as $failure) {
                             $error[$failure->getColumn()] = $failure->getMessage();
                         }
                     }
                 }
             } else {
                 //simply save information
                 if ($role->save()) {
                     //dispatch event update
                     $this->dispatch('onUpdateRole', new CMSBackendEvent($this, ['role' => $role]));
                 } else {
                     if (!$role->isValid()) {
                         $failures = $role->getValidationFailures();
                         foreach ($failures as $failure) {
                             $error[$failure->getColumn()] = $failure->getMessage();
                         }
                     }
                 }
                 if (!empty($error)) {
                     $role->rollBack();
                     return false;
                 }
             }
             //end simply save information
             if ($current_status != $role->getStatus() && $role->getStatus() == \Roles::STATUS_INACTIVE) {
                 //change status to INACTIVE
                 $role->changeDescendantsStatus(\Roles::STATUS_INACTIVE);
             }
         }
         if ($role->isValid()) {
             $role->commit();
             return $role;
         }
         $role->rollBack();
     } catch (\Exception $e) {
         $role->rollBack();
         throw $e;
     }
     $failures = $role->getValidationFailures();
     foreach ($failures as $failure) {
         $error[$failure->getColumn()] = $failure->getMessage();
     }
     return false;
 }