Ejemplo n.º 1
0
 /**
  * Function that allows child controller access to model data
  * after the data has been saved.
  *
  * @param   JModel  &$model     The data model object.
  * @param   array   $validData  The validated data.
  *
  * @return  void
  *
  * @since   11.1
  */
 protected function postSaveHook(JModelLegacy $model, $validData = array())
 {
     if ($validData['id'] >= 0) {
         // get user object
         $user = JFactory::getUser();
         // if id is 0 get id
         if (0 >= (int) $validData['id']) {
             // Get the created by id
             $created_by = isset($validData['created_by']) && $validData['created_by'] > 0 ? $validData['created_by'] : $user->id;
             // Get a db connection.
             $db = JFactory::getDbo();
             // Create a new query object.
             $query = $db->getQuery(true);
             // Select id of this company
             $query->select($db->quoteName(array('id')));
             $query->from($db->quoteName('#__costbenefitprojection_company'));
             $query->where($db->quoteName('name') . ' = ' . $db->quote($validData['name']));
             $query->where($db->quoteName('email') . ' = ' . $db->quote($validData['email']));
             $query->where($db->quoteName('country') . ' = ' . (int) $validData['country']);
             $query->where($db->quoteName('service_provider') . ' = ' . (int) $validData['service_provider']);
             $query->where($db->quoteName('created_by') . ' = ' . (int) $created_by);
             if (isset($validData['created'])) {
                 $query->where($db->quoteName('created') . ' = ' . $db->quote($validData['created']));
             }
             $db->setQuery($query);
             $db->execute();
             if ($db->getNumRows()) {
                 $validData['id'] = $db->loadResult();
             } else {
                 return;
             }
         }
         // user setup if not set
         if (0 >= (int) $validData['user'] && (int) $validData['id'] > 0) {
             $userIs = CostbenefitprojectionHelper::userIs($user->id);
             if (1 == $userIs) {
                 // this is a company so just use its id
                 $userId = $user->id;
                 // add this user id to this company
                 $validData['user'] = $userId;
                 $model->save($validData);
             } else {
                 // setup config array
                 $newUser = array('name' => $validData['name'], 'email' => $validData['email']);
                 $userId = CostbenefitprojectionHelper::createUser($newUser);
                 if (!is_int($userId)) {
                     $this->setMessage($userId, 'error');
                 } else {
                     // add this user id to this company
                     $validData['user'] = $userId;
                     $model->save($validData);
                 }
             }
         }
         // only continue if we have a company id
         if ((int) $validData['id'] > 0) {
             // get params
             $params = JComponentHelper::getParams('com_costbenefitprojection');
             // get all this users companies
             $hisCompanies = CostbenefitprojectionHelper::hisCompanies($validData['user']);
             if (CostbenefitprojectionHelper::checkArray($hisCompanies)) {
                 // set the user group based on the overall status of its companies
                 $departments = CostbenefitprojectionHelper::getVars('company', $hisCompanies, 'id', 'department');
                 if (in_array(2, $departments)) {
                     $memberGroups = $params->get('advancedmembergroup');
                 } else {
                     $memberGroups = $params->get('memberbasicgroup');
                 }
             } else {
                 // first company so act simply on this company department status
                 if (2 == $validData['department']) {
                     $memberGroups = $params->get('advancedmembergroup');
                 } else {
                     $memberGroups = $params->get('memberbasicgroup');
                 }
             }
             // update the user groups
             JUserHelper::setUserGroups((int) $validData['user'], (array) $memberGroups);
             // Get a db connection.
             $db = JFactory::getDbo();
             // Create a new query object.
             $query = $db->getQuery(true);
             // Select all records in scaling factors the belong to this company
             $query->select($db->quoteName(array('id', 'causerisk', 'published')));
             $query->from($db->quoteName('#__costbenefitprojection_scaling_factor'));
             $query->where($db->quoteName('company') . ' = ' . (int) $validData['id']);
             $db->setQuery($query);
             $db->execute();
             if ($db->getNumRows()) {
                 // load the scaling factors already set
                 $already = $db->loadObjectList();
                 $publish = array();
                 $archive = array();
                 $bucket = array();
                 foreach ($already as $scale) {
                     if (CostbenefitprojectionHelper::checkArray($validData['causesrisks'])) {
                         if (in_array($scale->causerisk, $validData['causesrisks']) && $scale->published != 1) {
                             // publish the scaling factor (update)
                             $publish[$scale->id] = $scale->id;
                         } elseif (!in_array($scale->causerisk, $validData['causesrisks'])) {
                             // archive the scaling factor (update)
                             $archive[$scale->id] = $scale->id;
                         }
                         $bucket[] = $scale->causerisk;
                     } else {
                         // archive the scaling factor (update)
                         $archive[$scale->id] = $scale->id;
                     }
                 }
                 // update the needed records
                 $types = array('publish' => 1, 'archive' => 2);
                 foreach ($types as $type => $int) {
                     if (CostbenefitprojectionHelper::checkArray(${$type})) {
                         foreach (${$type} as $id) {
                             $query = $db->getQuery(true);
                             // Fields to update.
                             $fields = array($db->quoteName('published') . ' = ' . (int) $int);
                             // Conditions for which records should be updated.
                             $conditions = array($db->quoteName('id') . ' = ' . (int) $id);
                             $query->update($db->quoteName('#__costbenefitprojection_scaling_factor'))->set($fields)->where($conditions);
                             $db->setQuery($query);
                             $db->execute();
                         }
                     }
                 }
             }
             if (CostbenefitprojectionHelper::checkArray($validData['causesrisks'])) {
                 // remove those already set from the saved list of causesrisks
                 if (CostbenefitprojectionHelper::checkArray($bucket)) {
                     $insert = array();
                     foreach ($validData['causesrisks'] as $causerisk) {
                         if (!in_array($causerisk, $bucket)) {
                             $insert[] = $causerisk;
                         }
                     }
                 } else {
                     $insert = $validData['causesrisks'];
                 }
             }
             // insert the new records
             if (CostbenefitprojectionHelper::checkArray($insert)) {
                 $created = $db->quote(JFactory::getDate()->toSql());
                 $created_by = JFactory::getUser()->get('id');
                 $company = $validData['id'];
                 // Create a new query object.
                 $query = $db->getQuery(true);
                 // Insert columns.
                 $columns = array('causerisk', 'company', 'mortality_scaling_factor_females', 'mortality_scaling_factor_males', 'presenteeism_scaling_factor_females', 'presenteeism_scaling_factor_males', 'yld_scaling_factor_females', 'yld_scaling_factor_males', 'published', 'created_by', 'created');
                 // setup the values
                 $values = array();
                 foreach ($insert as $new) {
                     $array = array($new, $company, 1, 1, 1, 1, 1, 1, 1, $created_by, $created);
                     $values[] = implode(',', $array);
                 }
                 // Prepare the insert query.
                 $query->insert($db->quoteName('#__costbenefitprojection_scaling_factor'))->columns($db->quoteName($columns))->values(implode('), (', $values));
                 // Set the query using our newly populated query object and execute it.
                 $db->setQuery($query);
                 $done = $db->execute();
                 if ($done) {
                     // we must set the assets
                     foreach ($insert as $causerisk) {
                         // get all the ids. Create a new query object.
                         $query = $db->getQuery(true);
                         $query->select($db->quoteName(array('id')));
                         $query->from($db->quoteName('#__costbenefitprojection_scaling_factor'));
                         $query->where($db->quoteName('causerisk') . ' = ' . (int) $causerisk);
                         $query->where($db->quoteName('company') . ' = ' . (int) $company);
                         $db->setQuery($query);
                         $db->execute();
                         if ($db->getNumRows()) {
                             $aId = $db->loadResult();
                             // make sure the access of asset is set
                             CostbenefitprojectionHelper::setAsset($aId, 'scaling_factor');
                         }
                     }
                 }
             }
         }
     }
     return;
 }
Ejemplo n.º 2
0
 /**
  * Save the data from the file to the database
  *
  * @param string  $package Paths to the uploaded package file
  *
  * @return  boolean false on failure
  *
  **/
 protected function save($data, $table)
 {
     // import the data if there is any
     if (CostbenefitprojectionHelper::checkArray($data['array'])) {
         // get user object
         $user = JFactory::getUser();
         // remove header if it has headers
         $id_key = $data['target_headers']['id'];
         $published_key = $data['target_headers']['published'];
         $ordering_key = $data['target_headers']['ordering'];
         // get the first array set
         $firstSet = reset($data['array']);
         // check if first array is a header array and remove if true
         if ($firstSet[$id_key] == 'id' || $firstSet[$published_key] == 'published' || $firstSet[$ordering_key] == 'ordering') {
             array_shift($data['array']);
         }
         // make sure there is still values in array and that it was not only headers
         if (CostbenefitprojectionHelper::checkArray($data['array']) && $user->authorise($table . '.import', 'com_costbenefitprojection') && $user->authorise('core.import', 'com_costbenefitprojection')) {
             // set target.
             $target = array_flip($data['target_headers']);
             // Get a db connection.
             $db = JFactory::getDbo();
             // set some defaults
             $todayDate = JFactory::getDate()->toSql();
             // get global action permissions
             $canDo = CostbenefitprojectionHelper::getActions($table);
             $canEdit = $canDo->get('core.edit');
             $canState = $canDo->get('core.edit.state');
             $canCreate = $canDo->get('core.create');
             $hasAlias = $this->getAliasesUsed($table);
             // prosses the data
             foreach ($data['array'] as $row) {
                 $found = false;
                 if (isset($row[$id_key]) && is_numeric($row[$id_key]) && $row[$id_key] > 0) {
                     // raw items import & update!
                     $query = $db->getQuery(true);
                     $query->select('version')->from($db->quoteName('#__costbenefitprojection_' . $table))->where($db->quoteName('id') . ' = ' . $db->quote($row[$id_key]));
                     // Reset the query using our newly populated query object.
                     $db->setQuery($query);
                     $db->execute();
                     $found = $db->getNumRows();
                 }
                 if ($found && $canEdit) {
                     // update item
                     $id = $row[$id_key];
                     $version = $db->loadResult();
                     // reset all buckets
                     $query = $db->getQuery(true);
                     $fields = array();
                     // Fields to update.
                     foreach ($row as $key => $cell) {
                         // ignore column
                         if ('IGNORE' == $target[$key]) {
                             continue;
                         }
                         // update modified
                         if ('modified_by' == $target[$key]) {
                             continue;
                         }
                         // update modified
                         if ('modified' == $target[$key]) {
                             continue;
                         }
                         // update version
                         if ('version' == $target[$key]) {
                             $cell = (int) $version + 1;
                         }
                         // verify publish authority
                         if ('published' == $target[$key] && !$canState) {
                             continue;
                         }
                         // set to update array
                         if (in_array($key, $data['target_headers']) && is_numeric($cell)) {
                             $fields[] = $db->quoteName($target[$key]) . ' = ' . $cell;
                         } elseif (in_array($key, $data['target_headers']) && is_string($cell)) {
                             $fields[] = $db->quoteName($target[$key]) . ' = ' . $db->quote($cell);
                         } elseif (in_array($key, $data['target_headers']) && is_null($cell)) {
                             // if import data is null then set empty
                             $fields[] = $db->quoteName($target[$key]) . " = ''";
                         }
                     }
                     // load the defaults
                     $fields[] = $db->quoteName('modified_by') . ' = ' . $db->quote($user->id);
                     $fields[] = $db->quoteName('modified') . ' = ' . $db->quote($todayDate);
                     // Conditions for which records should be updated.
                     $conditions = array($db->quoteName('id') . ' = ' . $id);
                     $query->update($db->quoteName('#__costbenefitprojection_' . $table))->set($fields)->where($conditions);
                     $db->setQuery($query);
                     $db->execute();
                 } elseif ($canCreate) {
                     // insert item
                     $query = $db->getQuery(true);
                     // reset all buckets
                     $columns = array();
                     $values = array();
                     $version = false;
                     // Insert columns. Insert values.
                     foreach ($row as $key => $cell) {
                         // ignore column
                         if ('IGNORE' == $target[$key]) {
                             continue;
                         }
                         // remove id
                         if ('id' == $target[$key]) {
                             continue;
                         }
                         // update created
                         if ('created_by' == $target[$key]) {
                             continue;
                         }
                         // update created
                         if ('created' == $target[$key]) {
                             continue;
                         }
                         // Make sure the alias is incremented
                         if ('alias' == $target[$key]) {
                             $cell = $this->getAlias($cell, $table);
                         }
                         // update version
                         if ('version' == $target[$key]) {
                             $cell = 1;
                             $version = true;
                         }
                         // set to insert array
                         if (in_array($key, $data['target_headers']) && is_numeric($cell)) {
                             $columns[] = $target[$key];
                             $values[] = $cell;
                         } elseif (in_array($key, $data['target_headers']) && is_string($cell)) {
                             $columns[] = $target[$key];
                             $values[] = $db->quote($cell);
                         } elseif (in_array($key, $data['target_headers']) && is_null($cell)) {
                             // if import data is null then set empty
                             $columns[] = $target[$key];
                             $values[] = "''";
                         }
                     }
                     // load the defaults
                     $columns[] = 'created_by';
                     $values[] = $db->quote($user->id);
                     $columns[] = 'created';
                     $values[] = $db->quote($todayDate);
                     if (!$version) {
                         $columns[] = 'version';
                         $values[] = 1;
                     }
                     // Prepare the insert query.
                     $query->insert($db->quoteName('#__costbenefitprojection_' . $table))->columns($db->quoteName($columns))->values(implode(',', $values));
                     // Set the query using our newly populated query object and execute it.
                     $db->setQuery($query);
                     $done = $db->execute();
                     if ($done) {
                         $aId = $db->insertid();
                         // make sure the access of asset is set
                         CostbenefitprojectionHelper::setAsset($aId, $table);
                     }
                 } else {
                     return false;
                 }
             }
             return true;
         }
     }
     return false;
 }