コード例 #1
0
ファイル: project.php プロジェクト: Ovi1/LSPB-jombri
 function saveProject()
 {
     // Check for request forgeries
     JSession::checkToken() or jexit(JText::_('JINVALID_TOKEN'));
     // Initialize variables
     $app = JFactory::getApplication();
     $db = JFactory::getDbo();
     $user = JFactory::getUser();
     $row = JTable::getInstance('project', 'Table');
     $post = $app->input->post->getArray();
     $id = $app->input->get('id', 0, 'int');
     $jbmail = JblanceHelper::get('helper.email');
     // create an instance of the class EmailHelper
     $isNew = false;
     $totalAmount = $app->input->get('totalamount', 0, 'float');
     $config = JblanceHelper::getConfig();
     $reviewProjects = $config->reviewProjects;
     //load the project value if the project is 'edit'
     if ($id > 0) {
         $row->load($id);
     } else {
         $isNew = true;
     }
     // this is a new project
     $post['publisher_userid'] = $user->id;
     //$post['description']  = $app->input->get('description', '', 'string');
     $id_category = $app->input->get('id_category', '', 'array');
     JArrayHelper::toInteger($id_category);
     if (count($id_category) > 0 && !(count($id_category) == 1 && empty($id_category[0]))) {
         $proj_categ = implode(',', $id_category);
     } elseif ($id_category[0] == 0) {
         $proj_categ = 0;
     }
     $post['id_category'] = $proj_categ;
     if ($post['project_type'] == 'COM_JBLANCE_FIXED') {
         $budgetRange = explode('-', $post['budgetrange_fixed']);
     } elseif ($post['project_type'] == 'COM_JBLANCE_HOURLY') {
         $budgetRange = explode('-', $post['budgetrange_hourly']);
     }
     $post['budgetmin'] = $budgetRange[0];
     $post['budgetmax'] = $budgetRange[1];
     //save the commitment value
     $commitment = $app->input->get('commitment', null, 'array');
     $registry = new JRegistry();
     $registry->loadArray($commitment);
     $post['commitment'] = $registry->toString();
     //save the params value
     $params = $app->input->get('params', null, 'array');
     $registry = new JRegistry();
     $registry->loadArray($params);
     $post['params'] = $registry->toString();
     if ($isNew) {
         $now = JFactory::getDate();
         $post['create_date'] = $now->toSql();
         $post['status'] = 'COM_JBLANCE_OPEN';
         //check if the project is to be reviewed by admin. If so, set the approved=0
         if ($reviewProjects) {
             $post['approved'] = 0;
         }
         // deduce `charge per project` if amount is > 0
         $plan = JblanceHelper::whichPlan($user->id);
         $chargePerProject = $plan->buyChargePerProject;
         if ($chargePerProject > 0) {
             $transDtl = JText::_('COM_JBLANCE_CHARGE_PER_PROJECT') . ' - ' . $post['project_title'];
             JblanceHelper::updateTransaction($user->id, $transDtl, $chargePerProject, -1);
             $msg_debit = JText::sprintf('COM_JBLANCE_YOUR_ACCOUNT_DEBITED_WITH_CURRENCY_FOR_POSTING_PROJECT', JblanceHelper::formatCurrency($chargePerProject));
             $app->enqueueMessage($msg_debit);
         }
     }
     // deduce the amount from user's account
     if ($totalAmount > 0) {
         //check if the user has enough fund to promote project
         $totalFund = JblanceHelper::getTotalFund($user->id);
         if ($totalFund < $totalAmount) {
             $msg = JText::_('COM_JBLANCE_BALANCE_INSUFFICIENT_TO_PROMOTE_PROJECT');
             $id_link = '';
             if ($post['id'] > 0) {
                 $id_link = '&id=' . $post['id'];
             }
             $link = JRoute::_('index.php?option=com_jblance&view=project&layout=editproject' . $id_link, false);
             $this->setRedirect($link, $msg, 'error');
             return false;
         } else {
             $post['profit_additional'] = $row->profit_additional + $post['totalamount'];
             $transDtl = JText::_('COM_JBLANCE_PROJECT_PROMOTION_FEE_FOR') . ' - ' . $post['project_title'];
             JblanceHelper::updateTransaction($user->id, $transDtl, $totalAmount, -1);
         }
     }
     if (!$row->save($post)) {
         JError::raiseError(500, $row->getError());
     }
     //save the custom field value for project
     $fields = JblanceHelper::get('helper.fields');
     // create an instance of the class fieldsHelper
     $fields->saveFieldValues('project', $row->id, $post);
     JBMediaHelper::uploadFile($post, $row);
     //remove and upload files
     //update the project posting limit in the plan subscription table
     if ($isNew) {
         $finance = JblanceHelper::get('helper.finance');
         // create an instance of the class FinanceHelper
         $finance->updateProjectLeft($user->id);
     }
     //Trigger the plugin event to feed the activity - buyer pick freelancer
     JPluginHelper::importPlugin('joombri');
     $dispatcher = JDispatcher::getInstance();
     $dispatcher->trigger('onProjectAfterSave', array($row, $isNew));
     //send email to admin if the project needs review (only for new project)
     if ($isNew) {
         if ($reviewProjects) {
             $jbmail->sendAdminProjectPendingApproval($row->id);
         }
     }
     // if the project in "private invite, send him to freelancer selection page else send notification"
     if ($post['is_private_invite']) {
         $msg = JText::_('COM_JBLANCE_PROJECT_SAVED_SUCCESSFULLY') . ' : ' . $row->project_title;
         $return = JRoute::_('index.php?option=com_jblance&view=project&layout=inviteuser&id=' . $row->id, false);
         $this->setRedirect($return, $msg);
     } else {
         //send new project notification if the project doesn't need review
         if (!$reviewProjects) {
             $jbmail->sendNewProjectNotification($row->id, $isNew);
         }
         $msg = JText::_('COM_JBLANCE_PROJECT_SAVED_SUCCESSFULLY') . ' : ' . $row->project_title;
         $return = JRoute::_('index.php?option=com_jblance&view=project&layout=showmyproject', false);
         $this->setRedirect($return, $msg);
     }
 }