/**
 * Return value of a given config option
 * 
 * Output of this helper is not cleaned!
 *
 * @param array $params
 * @param Smarty $smarty
 * @return mixed
 */
function smarty_function_config_option($params, &$smarty)
{
    $name = array_var($params, 'name');
    if (empty($name)) {
        return new InvalidParamError('name', $name, '$name value is required');
    }
    // if
    if (isset($params['user']) && instance_of($params['user'], 'User')) {
        return UserConfigOptions::getValue($name, $params['user']);
        //    } elseif(isset($params['project']) && instance_of($params['project'], 'Project')) {
        //      return ProjectConfigOptions::getValue($name, $params['project']);
    } elseif (isset($params['company']) && instance_of($params['company'], 'Company')) {
        return CompanyConfigOptions::getValue($name, $params['company']);
    } else {
        return ConfigOptions::getValue($name);
    }
    // if
}
 /**
  * Delete from database
  *
  * @param void
  * @return boolean
  */
 function delete()
 {
     $delete = parent::delete();
     if ($delete && !is_error($delete)) {
         switch ($this->getType()) {
             case COMPANY_CONFIG_OPTION:
                 CompanyConfigOptions::deleteByOption($this->getName());
                 break;
             case USER_CONFIG_OPTION:
                 UserConfigOptions::deleteByOption($this->getName());
                 break;
             case PROJECT_CONFIG_OPTION:
                 ProjectConfigOptions::deleteByOption($this->getName());
                 break;
         }
         // if
     }
     // if
     return $delete;
 }
 /**
  * Return config option value
  *
  * @param string $name
  * @return mixed
  */
 function getConfigValue($name)
 {
     return CompanyConfigOptions::getValue($name, $this);
 }
 /**
  * Send client address details
  * 
  * @param void
  * @return void
  */
 function company_details()
 {
     if (!$this->request->isAsyncCall()) {
         $this->httpError(HTTP_ERR_BAD_REQUEST);
     }
     // if
     $client_id = $this->request->get('company_id');
     $client_company = Companies::findById($client_id);
     if (!instance_of($client_company, 'Company')) {
         $this->httpError(HTTP_ERR_NOT_FOUND, ' ');
     }
     // if
     $company_address = CompanyConfigOptions::getValue('office_address', $client_company);
     echo $company_address;
     die;
 }
 /**
  * Edit Company Info
  * 
  * @param void
  * @return null
  */
 function edit()
 {
     if ($this->active_company->isNew()) {
         $this->httpError(HTTP_ERR_NOT_FOUND);
     }
     // if
     $this->wireframe->print_button = false;
     if ($this->request->isApiCall() && !$this->request->isSubmitted()) {
         $this->httpError(HTTP_ERR_BAD_REQUEST);
     }
     // if
     if (!$this->active_company->canEdit($this->logged_user)) {
         $this->httpError(HTTP_ERR_FORBIDDEN);
     }
     // if
     if ($this->active_company->isNew()) {
         $this->httpError(HTTP_ERR_NOT_FOUND);
     }
     // if
     $options = array('office_address', 'office_phone', 'office_fax', 'office_homepage');
     $company_data = $this->request->post('company');
     if (!is_array($company_data)) {
         $company_data = array_merge(array('name' => $this->active_company->getName()), CompanyConfigOptions::getValues($options, $this->active_company));
     }
     // if
     $this->smarty->assign('company_data', $company_data);
     if ($this->request->isSubmitted()) {
         db_begin_work();
         $old_name = $this->active_company->getName();
         $this->active_company->setAttributes($company_data);
         $save = $this->active_company->save();
         if ($save && !is_error($save)) {
             foreach ($options as $option) {
                 $value = trim(array_var($company_data, $option));
                 if ($option == 'office_homepage' && $value && strpos($value, '://') === false) {
                     $value = 'http://' . $value;
                 }
                 // if
                 if ($value == '') {
                     CompanyConfigOptions::removeValue($option, $this->active_company);
                 } else {
                     CompanyConfigOptions::setValue($option, $value, $this->active_company);
                 }
                 // if
             }
             // foreach
             if ($this->active_company->getIsOwner()) {
                 cache_remove('owner_company');
                 // force cache refresh on next load
             }
             // if
             db_commit();
             if ($this->request->getFormat() == FORMAT_HTML) {
                 flash_success("Company :name has been updated", array('name' => $old_name));
                 $this->redirectToUrl($this->active_company->getViewUrl());
             } else {
                 $this->serveData($this->active_company, 'company');
             }
             // if
         } else {
             db_rollback();
             if ($this->request->getFormat() == FORMAT_HTML) {
                 $this->smarty->assign('errors', $save);
             } else {
                 $this->serveData($save);
             }
             // if
         }
         // if
     }
     // if
 }