/**
  *  This method adds/saves a user
  *
  *  @param $id           If we are editing, $id is the user id. If we are adding, $id is the parent_id
  *  @param $edit         Boolean flag that defines if we are editing $id or adding to $id
  *  @param $formvalues   (Optional) Custom array with user attributes
  *
  *  @returns    YDResult
  */
 function _saveFormDetails($id, $edit, $formvalues = null)
 {
     // check form validation
     if (!$this->_form->validate($formvalues)) {
         return YDResult::warning(t('form errors'), $this->_form->getErrors());
     }
     // get form values EXCLUDING spans
     $values = $this->_form->getValues();
     // check if we are editing or adding an element
     if ($edit) {
         // create userobject node
         $userobject = array();
         $userobject['type'] = 'YDCMUser';
         if (isset($values['name'])) {
             $userobject['reference'] = $values['name'];
         }
         if (isset($values['state'])) {
             $userobject['state'] = $values['state'];
         }
         // update userobject
         $uobj = new YDCMUserobject();
         $res = $uobj->updateNode($userobject, $id);
         // create user row
         $user = array();
         if (isset($values['password'])) {
             $user['password'] = md5($values['password']);
         }
         if (isset($values['username'])) {
             $user['username'] = $values['username'];
         }
         if (isset($values['name'])) {
             $user['name'] = $values['name'];
         }
         if (isset($values['email'])) {
             $user['email'] = $values['email'];
         }
         if (isset($values['other'])) {
             $user['other'] = $values['other'];
         }
         if (isset($values['lang_id'])) {
             $user['lang_id'] = $values['lang_id'];
         }
         if (isset($values['template'])) {
             $user['template'] = $values['template'];
         }
         // check login schedule dates
         if (isset($user['login_start'])) {
             $user['login_start'] = YDStringUtil::formatDate($values['login_start'], 'datetimesql');
         }
         if (isset($user['login_end'])) {
             $user['login_end'] = YDStringUtil::formatDate($values['login_end'], 'datetimesql');
         }
         // update user
         $this->resetValues();
         $this->setValues($user);
         $this->where('user_id = ' . $id);
         $res = $this->update();
         // check update result and return
         if ($res > 0) {
             return YDResult::ok(t('ydcmuser mess updated'), $res);
         } else {
             return YDResult::warning(t('ydcmuser mess impossible to update'), $res);
         }
     } else {
         // check if parent it is set in argument or was choosen in the group selectbox
         if (is_null($id)) {
             $id = $values['group'];
         }
         // create userobject node
         $userobject = array();
         $userobject['type'] = 'YDCMUser';
         $userobject['reference'] = $values['name'];
         $userobject['state'] = isset($values['state']) ? $values['state'] : 0;
         // insert a new node in userobject and get the new id for user row creation
         $uobj = new YDCMUserobject();
         $res = $uobj->addNode($userobject, intval($id));
         // create user row
         $user = array();
         // add REQUIRED values
         $user['user_id'] = $res;
         $user['password'] = md5($values['password']);
         $user['username'] = $values['username'];
         $user['lang_id'] = $values['lang_id'];
         $user['template'] = $values['template'];
         $user['name'] = isset($values['name']) ? $values['name'] : '';
         $user['email'] = isset($values['email']) ? $values['email'] : '';
         $user['other'] = isset($values['other']) ? $values['other'] : '';
         $user['login_counter'] = 0;
         $user['login_start'] = YDStringUtil::formatDate(0, 'datetimesql');
         $user['login_end'] = YDStringUtil::formatDate(0, 'datetimesql');
         $user['login_last'] = YDStringUtil::formatDate(0, 'datetimesql');
         $user['login_current'] = YDStringUtil::formatDate(0, 'datetimesql');
         // reset object
         $this->resetValues();
         $this->setValues($user);
         // insert values
         if ($this->insert()) {
             return YDResult::ok(t('ydcmuser mess created'), $res);
         } else {
             return YDResult::fatal(t('ydcmuser mess impossible to create'), $res);
         }
     }
 }
 /**
  *  This method adds/saves a group
  *
  *  @param $id           If we are editing, $id is the group id. If we are adding, $id is the parent_id
  *  @param $edit         Boolean flag that defines if we are editing $id or adding to $id
  *  @param $formvalues   (Optional) Custom array with attributes
  *
  *  @returns    YDResult
  */
 function _saveFormDetails($id, $edit, $formvalues = null)
 {
     // check form validation
     if (!$this->_form->validate($formvalues)) {
         return YDResult::warning(t('form errors'), $this->_form->getErrors());
     }
     // get form values EXCLUDING spans
     $values = $this->_form->getValues();
     // check if we are editing or adding an element
     if ($edit) {
         // create userobject node
         $userobject = array();
         $userobject['type'] = 'YDCMGroup';
         $userobject['reference'] = $values['name'];
         $userobject['state'] = 1;
         // update userobject
         $uobj = new YDCMUserobject();
         $res = $uobj->updateNode($userobject, $id);
         // create user row
         $group = array();
         $group['name'] = $values['name'];
         $group['description'] = $values['description'];
         // update user
         $this->resetValues();
         $this->setValues($group);
         $this->where('group_id = ' . $id);
         // update and sum lines afected to userobject
         $res += $this->update();
         // if we are using the permission system, update and sum lines afected in permission table
         if (isset($this->editing_PERMOBJ)) {
             $res += $this->editing_PERMOBJ->saveFormEdit($id, $formvalues);
         }
         // check update from node update and from group update
         if ($res > 0) {
             return YDResult::ok(t('ydcmgroup mess details updated'), $res);
         } else {
             return YDResult::warning(t('ydcmgroup mess details not updated'), $res);
         }
     } else {
         // create userobject node
         $userobject = array();
         $userobject['type'] = 'YDCMGroup';
         $userobject['reference'] = $values['name'];
         $userobject['state'] = 1;
         // check default parent id
         if (is_null($id)) {
             $id = 1;
         }
         // TODO: check if group is valid (and, eg, is not a user node)
         // update userobject and get new id
         $uobj = new YDCMUserobject();
         $nodeID = $uobj->addNode($userobject, intval($id));
         // init result count
         $res = $nodeID;
         // if we are using the permission system, add permissions and sum lines afected in permission table
         if (isset($this->editing_PERMOBJ)) {
             $res += $this->editing_PERMOBJ->saveFormNew($id, $nodeID, $formvalues);
         }
         // create user row
         $group = array();
         $group['group_id'] = intval($nodeID);
         $group['name'] = $values['name'];
         $group['description'] = $values['description'];
         // reset object
         $this->resetAll();
         $this->setValues($group);
         // insert values
         if ($this->insert()) {
             return YDResult::ok(t('ydcmgroup mess created'), $res);
         } else {
             return YDResult::fatal(t('ydcmgroup mess impossible to create'), $res);
         }
     }
 }