fmsystem-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Fmsystem-commits] [11375] frontend: try to ease the workload to find da


From: Sigurd Nes
Subject: [Fmsystem-commits] [11375] frontend: try to ease the workload to find data
Date: Thu, 17 Oct 2013 13:20:52 +0000

Revision: 11375
          http://svn.sv.gnu.org/viewvc/?view=rev&root=fmsystem&revision=11375
Author:   sigurdne
Date:     2013-10-17 13:20:52 +0000 (Thu, 17 Oct 2013)
Log Message:
-----------
frontend: try to ease the workload to find data

Modified Paths:
--------------
    trunk/frontend/inc/class.borental.inc.php
    trunk/frontend/inc/class.sorental.inc.php
    trunk/frontend/inc/class.uifrontend.inc.php
    trunk/frontend/templates/base/frontend.xsl
    trunk/rental/inc/class.bofellesdata.inc.php
    trunk/rental/inc/class.soparty.inc.php

Modified: trunk/frontend/inc/class.borental.inc.php
===================================================================
--- trunk/frontend/inc/class.borental.inc.php   2013-10-15 14:29:33 UTC (rev 
11374)
+++ trunk/frontend/inc/class.borental.inc.php   2013-10-17 13:20:52 UTC (rev 
11375)
@@ -5,7 +5,8 @@
        phpgw::import_class('rental.socontract_price_item');
        include_class('rental', 'contract', 'inc/model/');
 
-    class frontend_borental {
+    class frontend_borental
+    {
 
        public static function contract_exist_per_location($contract_id, 
$location_code, $contract_state_identifier)
        {
@@ -62,12 +63,14 @@
                        return false;
        }
 
+       //FIXME : Sigurd 16 okt 2013: not used?
        public static function get_first_contract_per_location($location_code)
        {
                $contracts_per_location = 
phpgwapi_cache::session_get('frontend', 'contracts_per_location');
                return $contracts_per_location[$location_code][0];
        }
 
+       //FIXME : Sigurd 16 okt 2013: not used?
                public static function 
get_first_contract_in_per_location($location_code)
        {
                $contracts_in_per_location = 
phpgwapi_cache::session_get('frontend', 'contracts_in_per_location');
@@ -78,11 +81,11 @@
          *
          * @param integer $org_unit_ids
          */
-        public static function get_property_locations($array)
+        public static function get_property_locations($array,$top_org_units)
         {
 
 // _debug_array($array);die();
- //                    return self::get_property_locations_lean($array);
+                       return 
self::get_property_locations_lean($array,$top_org_units);
  
                $property_locations = array();
                $property_locations_active = array();
@@ -205,8 +208,11 @@
                        }
                }
 
+               //Not used ?
                phpgwapi_cache::session_set('frontend', 
'contracts_per_location', $contracts_per_location);
+               //Not used ?
                phpgwapi_cache::session_set('frontend', 
'contracts_in_per_location', $contracts_in_per_location);
+               //Not used ?
                phpgwapi_cache::session_set('frontend', 
'contracts_ex_per_location', $contracts_ex_per_location);
                phpgwapi_cache::session_set('frontend', 
'rented_area_per_location', $rented_area_per_location);
                phpgwapi_cache::session_set('frontend', 
'total_price_per_location', $rented_price_per_location);
@@ -227,9 +233,9 @@
 
         /**
          *
-         * @param integer $org_unit_ids
+         * @param array $org_unit_ids
          */
-        public static function get_property_locations_lean($array)
+        public static function 
get_property_locations_lean($array,$top_org_units)
         {
 
                $property_locations = array();
@@ -257,63 +263,83 @@
                                {
                                        continue;
                                }
-                               $parties = 
rental_soparty::get_instance()->get(null, null, null, null, null, null, 
array('org_unit_id' => $row['ORG_UNIT_ID']));
+               //              $parties = 
rental_soparty::get_instance()->get(null, null, null, null, null, null, 
array('org_unit_id' => $row['ORG_UNIT_ID']));
+                       //      $parties = array_keys($parties);
+                               $parties = 
self::get_all_parties($top_org_units);
                        }
                        else
                        {
                                $parties = 
rental_soparty::get_instance()->get(null, null, null, null, null, null, 
array('email' => $row));
+                               $parties = array_keys($parties);
                        }
 
                        $contracts = array();
                        $composites = array();
 
-                       $soparty        = CreateObject('frontend.sorental');
+                       $sorental       = CreateObject('frontend.sorental');
 
-
                        //For all parties connected to the internal 
organization unit
-                       foreach($parties as $party_id => $party)
-                       {
-                                       $locations = 
$soparty->get_location($party_id);
+                               $locations = $sorental->get_location($parties);
 
-
-                       }
                }
 
-       // contracts ->  composite -> units -> location_code
+                       return $locations;
+               }
+               
 
+               /**
+               * Get the org_units by hierarchical inheritance
+               */
+               function get_all_parties($top_org_units)
+               {
+                       static $parties =array(); // cache result
+                       
+                       //already calculated
+                       if($parties)
+                       {
+                               return $parties;
+                       }
+/*
+                       $bt = debug_backtrace();
+                       echo "<b>frontend_borental::{$bt[0]['function']} Called 
from file: {$bt[0]['file']} line: {$bt[0]['line']}</b><br/>";
+                       unset($bt);
+*/
 
+                       $org_units = array();
+                       $selected_org_unit = phpgw::get_var('org_unit_id');     
                // New organisational unit selected from organisational units 
list
+                       if($selected_org_unit == 'all')
+                       {
+                               foreach($top_org_units as $entry)
+                               {
+                                       $org_units[] = $entry['ORG_UNIT_ID'];
+                               }
+                       }
+                       else
+                       {
+                               $org_units[] = $selected_org_unit;
+                       }
+                       
 
-_debug_array(count($parties));die();
+                       $bofellesdata = CreateObject('rental.bofellesdata');
 
- /*
- Array
-(
-    [0] => Array
-        (
-            [loc1_name] => DIVERSE SKOLER
-            [loc2_name] => DIVERSE SKOLE
-            [location_code] => 2000-01
-            [address] => 
-            [area_net] => 0
-            [area_gros] => 0
-        )
+                       foreach ($org_units as $org_unit)
+                       {
+                               
$bofellesdata->get_org_unit_ids_from_top($org_unit); 
+                       }
 
-    [1] => Array
-        (
-            [loc1_name] => BERGEN RÅDHUS
-            [loc2_name] => BERGEN RÅDHUS NYE
-            [location_code] => 1102-01
-            [address] => Rådhusgaten 10
-            [area_net] => 0
-            [area_gros] => 11277
-        )
+                       $all_unit_ids = array_unique($bofellesdata->unit_ids);
+ 
+                       $parties        = 
execMethod('frontend.sorental.get_parties', $all_unit_ids);
 
-)
 
- */
- 
- 
- 
-        }
+                       return $parties;
+               }
 
+
+               public static function get_total_cost_and_area($org_units = 
array())
+               {
+               $sorental       = CreateObject('frontend.sorental');
+               return $sorental->get_total_cost_and_area($org_units);
+               }
+
     }

Modified: trunk/frontend/inc/class.sorental.inc.php
===================================================================
--- trunk/frontend/inc/class.sorental.inc.php   2013-10-15 14:29:33 UTC (rev 
11374)
+++ trunk/frontend/inc/class.sorental.inc.php   2013-10-17 13:20:52 UTC (rev 
11375)
@@ -100,40 +100,145 @@
                        }
                }
 
-               public function get_location($party_id)
+               public function get_location($parties)
                {
-                       $party_id = (int) $party_id;
+                       $parties = (array) $parties;
+                       if(!$parties)
+                       {
+                               return array();
+                       }
 
-               _debug_array($party_id);
+                       $ts = time();
+                       
+                       $filtermethod = 'WHERE rental_party.id IN (' . 
implode(',', $parties) . ')'; 
+                       //active contract
+                       $filtermethod .= " AND ({$ts} >= date_start AND 
(date_end IS NULL OR {$ts} <= date_end))";
+                       
+                       $sql = "SELECT DISTINCT location_code FROM"
+                       . " rental_contract {$this->_db->join} 
rental_contract_party ON (rental_contract.id = 
rental_contract_party.contract_id)"
+                       . " {$this->_db->join} rental_party ON (rental_party.id 
= rental_contract_party.party_id)"
+                       . " {$this->_db->join} rental_contract_composite ON 
(rental_contract.id = rental_contract_composite.contract_id)"
+                       . " {$this->_db->join} rental_composite ON 
(rental_contract_composite.composite_id = rental_composite.id)"
+                       . " {$this->_db->join} rental_unit ON 
(rental_composite.id = rental_unit.composite_id)"
+                       . " {$filtermethod}";
 
-                       //FIXME something clever
-                       $sql = "SELECT * FROM somewhere WHERE id={$party_id}";
 
                        $this->_db->query($sql,__LINE__,__FILE__);
 
                        $values = array();
                        while ($this->_db->next_record())
                        {
-                               $values = array
+                               $values[] = array
                                (
                                        'location_code'         => 
$this->_db->f('location_code', true),
                                );
                        }
+                       
+                       foreach ($values as &$entry)
+                       {
 
-/*
-            [loc1_name] => BERGEN RÅDHUS
-            [loc2_name] => BERGEN RÅDHUS NYE
-            [location_code] => 1102-01
-            [address] => Rådhusgaten 10
-            [area_net] => 0
-            [area_gros] => 11277
-*/
+                               $location_code = $entry['location_code'];
+                               // We get the data from the property module
+                               $data = 
execMethod('property.bolocation.read_single', array('location_code' => 
$location_code, 'extra' => array('view' => true)));
 
+                               $stop_search = false;
+                               for($i = 1; !$stop_search; $i++)
+                               {
+                                       $loc_name = "loc{$i}_name";
+                                       if(array_key_exists($loc_name, $data))
+                                       {
+                                               $entry[$loc_name] =  
$data[$loc_name];
+                                       }
+                                       else
+                                       {
+                                               $stop_search = true;
+                                       }
+                               }
 
+                               $entry['address'] = $data['street_name'].' 
'.$data['street_number'];
+                               foreach($data['attributes'] as $attributes)
+                               {
+                                       switch($attributes['column_name'])
+                                       {
+                                               case 'area_gross':
+                                                       $entry['area_gros'] = 
$attributes['value'];
+                                                       break;
+                                               case 'area_net':
+                                                       $entry['area_net'] = 
$attributes['value'];
+                                                       break;
+                                               case 'bruttoareal':
+                                                       $entry['area_gros'] = 
$attributes['value'];
+                                                       break;
+                                               case 'nettoareal':
+                                                       $entry['area_net'] = 
$attributes['value'];
+                                                       break;
+                                       }
+                               }
+                       } 
+
                        return $values;
+               }
 
+
+               /**
+               * translate from org_unit to party.id
+               **/
+               function get_parties($org_units)
+               {
+                       if(!$org_units)
+                       {
+                               return array();
+                       }
+                       
+                       $sql = 'SELECT id FROM rental_party WHERE org_enhet_id 
IN (' . implode(',', $org_units) . ')'; 
+_debug_array($sql);
+                       $this->_db->query($sql,__LINE__,__FILE__);
+
+                       $values = array();
+
+                       while ($this->_db->next_record())
+                       {
+                               $values[] = $this->_db->f('id');
+                       }
+
+                       return $values;
                }
 
+               public function get_total_cost_and_area($org_units = array())
+               {
+                       if(!$org_units)
+                       {
+                               return array();
+                       }
 
+                       $ts = time();
+                       $filtermethod = 'WHERE rental_party.id IN (' . 
implode(',', $org_units) . ')'; 
+                       //active contract
+                       $filtermethod .= " AND ({$ts} >= 
rental_contract.date_start AND (rental_contract.date_end IS NULL OR {$ts} <= 
rental_contract.date_end))";
+                       
+                       $sql = "SELECT sum(total_price::numeric) AS 
sum_total_price FROM"
+                       . " rental_contract {$this->_db->join} 
rental_contract_party ON (rental_contract.id = 
rental_contract_party.contract_id)"
+                       . " {$this->_db->join} rental_party ON (rental_party.id 
= rental_contract_party.party_id)"
+                       . " {$this->_db->join} rental_contract_price_item ON 
(rental_contract.id  = rental_contract_price_item.contract_id)"
+                       . " {$filtermethod} AND NOT is_one_time";
 
+                       $this->_db->query($sql,__LINE__,__FILE__);
+
+                       $values = array();
+                       $this->_db->next_record();
+                       $values['sum_total_price'] = 
$this->_db->f('sum_total_price');
+
+                       $sql = "SELECT 
sum(rental_contract.rented_area::numeric) AS sum_total_area FROM"
+                       . " rental_contract {$this->_db->join} 
rental_contract_party ON (rental_contract.id = 
rental_contract_party.contract_id)"
+                       . " {$this->_db->join} rental_party ON (rental_party.id 
= rental_contract_party.party_id)"
+                       . " {$filtermethod}";
+
+
+                       $this->_db->query($sql,__LINE__,__FILE__);
+
+                       $this->_db->next_record();
+                       $values['sum_total_area'] = 
$this->_db->f('sum_total_area');
+
+                       return $values;
+               }
        }

Modified: trunk/frontend/inc/class.uifrontend.inc.php
===================================================================
--- trunk/frontend/inc/class.uifrontend.inc.php 2013-10-15 14:29:33 UTC (rev 
11374)
+++ trunk/frontend/inc/class.uifrontend.inc.php 2013-10-17 13:20:52 UTC (rev 
11375)
@@ -56,19 +56,19 @@
 
                        $this->config   = 
CreateObject('phpgwapi.config','frontend');
                        $this->config->read();
-                       $use_fellesdata = 
$this->config->config_data['use_fellesdata'];
-                       $logo_path = $this->config->config_data['logo_path'];
+                       $use_fellesdata = 
$this->config->config_data['use_fellesdata'];
+                       $logo_path              = 
$this->config->config_data['logo_path'];
 
                        // Get the mode: in frame or full screen
-                       $mode = phpgwapi_cache::session_get('frontend', 
'noframework');
-                       $noframework = isset($mode) ? $mode : true;
+                       $mode                   = 
phpgwapi_cache::session_get('frontend', 'noframework');
+                       $noframework    = isset($mode) ? $mode : true;
 
                        /* Get the tabs and check to see whether the user has 
specified a tab or has a selected tab on session */
-                       $tabs = $this->get_tabs();
-                       $location_id = phpgw::get_var('location_id', 'int', 
'REQUEST');
-                       $tab = isset($location_id) ? $location_id : 
phpgwapi_cache::session_get('frontend','tab');
-                       $selected = isset($tab) && $tab ? $tab : 
array_shift(array_keys($tabs));
-                       $this->tabs = 
$GLOBALS['phpgw']->common->create_tabs($tabs, $selected);
+                       $tabs                   = $this->get_tabs();
+                       $location_id    = phpgw::get_var('location_id', 'int', 
'REQUEST');
+                       $tab                    = isset($location_id) ? 
$location_id : phpgwapi_cache::session_get('frontend','tab');
+                       $selected               = isset($tab) && $tab ? $tab : 
array_shift(array_keys($tabs));
+                       $this->tabs             = 
$GLOBALS['phpgw']->common->create_tabs($tabs, $selected);
                        phpgwapi_cache::session_set('frontend','tab',$selected);
 
                        // Get header state
@@ -79,8 +79,8 @@
 
                        // Get navigation parameters
                        $param_selected_location = phpgw::get_var('location');  
                // New location selected from locations list
-                       $param_selected_org_unit = 
phpgw::get_var('org_unit_id');                       // New organisational unit 
selected from organisational units list
-                       $param_only_org_unit = phpgw::get_var('org_enhet_id');  
// Frontend access from rental module regarding specific organisational unit
+                       $param_selected_org_unit = 
phpgw::get_var('org_unit_id');               // New organisational unit 
selected from organisational units list
+                       $param_only_org_unit = phpgw::get_var('org_enhet_id');  
                // Frontend access from rental module regarding specific 
organisational unit
 
                        //Refresh organisation list
                        $refresh = phpgw::get_var('refresh','bool');
@@ -118,7 +118,7 @@
                                $this->header_state['selected_org_unit'] = 
$param_selected_org_unit;
 
                                //Update locations according to organisational 
unit specification
-                               $property_locations = 
frontend_borental::get_property_locations($org_unit_ids);
+                               $property_locations = 
frontend_borental::get_property_locations($org_unit_ids, 
$this->header_state['org_unit']);
                                $property_locations_update = true;
 
                        }
@@ -126,7 +126,8 @@
                        else if(isset($param_only_org_unit) && 
$param_only_org_unit)
                        {
                                //TODO: check permissions
-                               if($use_fellesdata){
+                               if($use_fellesdata)
+                               {
                                        $name_and_result_number = 
frontend_bofellesdata::get_instance()->get_organisational_unit_info($param_only_org_unit);
 
                                        //Specify unit
@@ -145,7 +146,7 @@
                                        
$this->header_state['selected_org_unit'] = $param_only_org_unit;
 
                                        //Update locations
-                                       $property_locations = 
frontend_borental::get_property_locations($org_unit_ids);
+                                       $property_locations = 
frontend_borental::get_property_locations($org_unit_ids, 
$this->header_state['org_unit']);
                                        $property_locations_update = true;
 
                                        $noframework = false; // In regular 
frames
@@ -177,13 +178,13 @@
 
                                        //Update locations
                                        //FIXME Sigurd 15. okt 2013: deselect 
'all' on initial view
-                                       //$property_locations = 
frontend_borental::get_property_locations($org_units);
+                                       //$property_locations = 
frontend_borental::get_property_locations($org_units, 
$this->header_state['org_unit']);
                                }
                                else
                                {
                                        //If no organisational database is in 
use: get rented properties based on username
                                        $usernames[] = 
$GLOBALS['phpgw_info']['user']['account_lid'];
-                                       $property_locations = 
frontend_borental::get_property_locations($usernames);
+                                       $property_locations = 
frontend_borental::get_property_locations($usernames, 
$this->header_state['org_unit']);
                                }
 
                                $property_locations_update = true;
@@ -207,6 +208,7 @@
 
                                $this->header_state['locations'] = 
$property_locations;
                                $this->header_state['number_of_locations'] = 
count($property_locations);
+                               //FIXME
                                $this->calculate_totals($property_locations);
                        }
 
@@ -392,34 +394,18 @@
                        $this->header_state['name_of_user'] = $name_of_user;
                }
 
+
+
                function calculate_totals($property_locations)
                {
-
                        // Calculate
-                       $total_area = 0;
-               //      $rented_area_per_location = 
phpgwapi_cache::user_get('frontend','rented_area_per_location', 
$GLOBALS['phpgw_info']['user']['account_id']);
-                       $rented_area_per_location = 
phpgwapi_cache::session_get('frontend','rented_area_per_location');
-                       foreach($rented_area_per_location as $location_code => 
$area_per_location)
-                       {
+                       $parties = frontend_borental::get_all_parties();
+       
+                       $totals = 
frontend_borental::get_total_cost_and_area($parties);
+                       $this->header_state['total_price'] = 
number_format($totals['sum_total_price'], 0, ","," ")." kr";
+                       $this->header_state['total_area'] = 
number_format($totals['sum_total_area'], 0, ",", " ")." kvm";
+                       
 
-                               
if($this->location_in_selection($location_code,$property_locations))
-                               {
-                                       $total_area += $area_per_location;
-                               }
-                       }
-
-                       $total_price = 0;
-               //      $total_price_per_location = 
phpgwapi_cache::user_get('frontend','total_price_per_location', 
$GLOBALS['phpgw_info']['user']['account_id']);
-                       $total_price_per_location = 
phpgwapi_cache::session_get('frontend','total_price_per_location');
-                       foreach($total_price_per_location as $location_code => 
$price_per_location)
-                       {
-                               
if($this->location_in_selection($location_code,$property_locations))
-                               {
-                                       $total_price += $price_per_location;
-                               }
-                       }
-                       $this->header_state['total_price'] = 
number_format($total_price, 0, ","," ")." kr";
-                       $this->header_state['total_area'] = 
number_format($total_area, 0, ",", " ")." kvm";
                }
 
                function location_in_selection($location_code, 
$property_locations)

Modified: trunk/frontend/templates/base/frontend.xsl
===================================================================
--- trunk/frontend/templates/base/frontend.xsl  2013-10-15 14:29:33 UTC (rev 
11374)
+++ trunk/frontend/templates/base/frontend.xsl  2013-10-17 13:20:52 UTC (rev 
11375)
@@ -1,10 +1,11 @@
 <xsl:template match="header" xmlns:php="http://php.net/xsl";>
+       <xsl:variable name="messages_url"><xsl:value-of 
select="php:function('get_phpgw_link', '/index.php', 
'menuaction:frontend.uimessages.index')" /></xsl:variable>
        <div id="wrapper">
        <div id="header">
                <div id="login-bar">
                        <ul class="user_menu">
                                <li><em><img 
src="frontend/templates/base/images/16x16/user_red.png"  class="list_image" 
/></em><xsl:value-of select="name_of_user"/> | <a 
href="http://portico/pe/preferences/changepassword.php";>Bytt passord</a></li>
-                               <li><a 
href="index.php?menuaction=frontend.uimessages.index" 
class="list_image"><em><img 
src="frontend/templates/base/images/16x16/email.png" 
class="list_image"/></em><xsl:value-of select="new_messages"/></a></li>
+                                       <li><a href="{$messages_url}" 
class="list_image"><em><img 
src="frontend/templates/base/images/16x16/email.png" 
class="list_image"/></em><xsl:value-of select="new_messages"/></a></li>
                                <li>
                                        <a href="logout.php"  
class="header_link"><em><img 
src="frontend/templates/base/images/16x16/door_out.png" 
class="list_image"/></em><xsl:value-of select="php:function('lang', 
'logout')"/></a> 
                                        |
@@ -41,7 +42,7 @@
                                                        </a>
                                                </li>
                                                <li>
-                                                       <form 
action="index.php?menuaction=frontend.uihelpdesk.index" method="post">
+                                                       <form 
action="{form_action}" method="post">
                                                                <select 
size="3" onchange="this.form.submit()" name="org_unit_id">
                                                                        
<xsl:choose>
                                                                                
<xsl:when test="selected_org_unit = 'all'">
@@ -158,7 +159,6 @@
                                                </div>
                                        </td>
                                        <td>
-                                               
                                                <div id="area_and_price" 
style="margin-top: 2em;">
                                                <ul>
                                                        <li 
style="border-style: none none solid none; border-width: 1px; border-color: 
grey; padding-bottom: 5px; "><em><img 
src="frontend/templates/base/images/16x16/house.png" 
class="list_image"/></em><xsl:value-of select="php:function('lang', 
'chosen_unit')"/>:</li>
@@ -170,7 +170,13 @@
                                        <td>
                                                <br/>
                                                <div id="unit_image">
-                                                       <img 
src="index.php?menuaction=frontend.uifrontend.objectimg&amp;loc_code={//header/selected_location}"
 alt="" />
+                                                       <img alt="">
+                                                               <xsl:attribute 
name="src">
+                                                                       
<xsl:value-of select="php:function('get_phpgw_link', '/index.php', 
'menuaction:frontend.uifrontend.objectimg')" />
+                                                                       
<xsl:text>&amp;loc_code=</xsl:text>
+                                                                       
<xsl:value-of select="//header/selected_location"/>
+                                                               </xsl:attribute>
+                                                       </img>
                                                </div>
                                        </td>
                                </tr>

Modified: trunk/rental/inc/class.bofellesdata.inc.php
===================================================================
--- trunk/rental/inc/class.bofellesdata.inc.php 2013-10-15 14:29:33 UTC (rev 
11374)
+++ trunk/rental/inc/class.bofellesdata.inc.php 2013-10-17 13:20:52 UTC (rev 
11375)
@@ -1,11 +1,12 @@
 <?php
-    class rental_bofellesdata {
-
+    class rental_bofellesdata
+    {
        // Instance variable
-           protected static $bo;
-           protected $connected = false;
-           protected $status;
-           protected $db = null;
+               protected static $bo;
+               protected $connected = false;
+               protected $status;
+               protected $db = null;
+               protected $unit_ids = array();
 
                var $public_functions = array
                        (
@@ -19,13 +20,35 @@
                 */
                public static function get_instance()
                {
-                       if (self::$bo == null) {
+                       if (self::$bo == null)
+                       {
                                self::$bo = CreateObject('rental.bofellesdata');
                        }
                        return self::$bo;
                }
 
 
+
+               /**
+                * Magic get method
+                *
+                * @param string $varname the variable to fetch
+                *
+                * @return mixed the value of the variable sought - null if not 
found
+                */
+               public function __get($varname)
+               {
+                       switch ($varname)
+                       {
+                               case 'unit_ids':
+                                       return $this->unit_ids;
+                                       break;
+                               default:
+                                       return null;
+                       }
+               }
+
+
                /* our simple php ping function */
                function ping($host)
                {
@@ -277,11 +300,11 @@
                                else
                                {
                                        $result[] = array
-                               (
+                                       (
                                                'id' => 
(int)$db->f('ORG_ENHET_ID'),
                                                'name' => 
$db->f('ORG_NAVN',true),
                                                'unit_id' => 
$db->f('RESULTATENHET')
-                               );
+                                       );
                                }
                        }
 
@@ -730,6 +753,99 @@
                        return 0;
                }
 
+
+
+               function org_unit_is_top_level($org_unit_id)
+               {
+                       if(!$db = $this->get_db())
+                       {
+                               return;
+                       }
+
+                       $q = "SELECT * FROM V_ORG_ENHET WHERE 
org_enhet_id=$org_unit_id AND org_nivaa < 4";
+
+                       $result = $this->db->query($q);
+
+                       if($this->db->next_record())
+                       {
+                               return true;
+                       }
+                       else
+                       {
+                               return false;
+                       }
+               }
+
+               function get_org_unit_ids_from_top($org_unit_id)
+               {
+                       if(!$db = $this->get_db())
+                       {
+                               return;
+                       }
+
+                       if(!$org_unit_id)
+                       {
+                               return array();
+                       }
+
+                       $this->unit_ids[] = $org_unit_id;
+
+                       $q = "SELECT V_ORG_KNYTNING.*, ANT_ENHETER_UNDER FROM 
V_ORG_KNYTNING"
+                       . " JOIN V_ORG_ENHET ON (V_ORG_ENHET.ORG_ENHET_ID = 
V_ORG_KNYTNING.ORG_ENHET_ID_KNYTNING ) WHERE 
V_ORG_KNYTNING.ORG_ENHET_ID_KNYTNING=$org_unit_id";
+
+                       if($db->Type == "postgres")
+                       {
+                               $q = strtolower($q);
+                       }
+                       $result = $db->query($q);
+
+                       $org_enhet_field = $db->Type == 'postgres' ? 
'org_enhet_id' : 'ORG_ENHET_ID';
+                       $check_subs = $db->Type == 'postgres' ? 
'ant_enheter_under' : 'ANT_ENHETER_UNDER';
+               
+                       while($db->next_record())
+                       {
+                               $child_org_unit_id = $db->f($org_enhet_field);
+                               $this->unit_ids[] = $child_org_unit_id;
+
+                               if($db->f($check_subs))
+                               {
+                                       
$this->get_org_unit_ids_children($child_org_unit_id);
+                               }
+                       }
+
+                       return $this->unit_ids;
+               }
+
+
+               function get_org_unit_ids_children($org_unit_id)
+               {
+                       $org_unit_id = (int)$org_unit_id;
+                       $db = clone($this->db);
+               
+                       $q = "SELECT V_ORG_KNYTNING.*, ANT_ENHETER_UNDER FROM 
V_ORG_KNYTNING"
+                       . " JOIN V_ORG_ENHET ON (V_ORG_ENHET.ORG_ENHET_ID = 
V_ORG_KNYTNING.ORG_ENHET_ID_KNYTNING ) WHERE 
V_ORG_KNYTNING.ORG_ENHET_ID_KNYTNING=$org_unit_id";
+
+                       if($db->Type == "postgres")
+                       {
+                               $q = strtolower($q);
+                       }
+                       $db->query($q);
+
+                       $org_enhet_field = $db->Type == 'postgres' ? 
'org_enhet_id' : 'ORG_ENHET_ID';
+                       $check_subs = $db->Type == 'postgres' ? 
'ant_enheter_under' : 'ANT_ENHETER_UNDER';
+
+                       while($db->next_record())
+                       {
+                               $child_org_unit_id = $db->f($org_enhet_field);
+                               $this->unit_ids[] = $child_org_unit_id;
+                               if($db->f($check_subs))
+                               {
+                                       
$this->get_org_unit_ids_children($child_org_unit_id);
+                               }
+                       }
+               }
+
+
                protected function log($class, $function)
                {
                        
if(isset($GLOBALS['phpgw_info']['server']['log_levels']['module']['rental']) && 
$GLOBALS['phpgw_info']['server']['log_levels']['module']['rental'])

Modified: trunk/rental/inc/class.soparty.inc.php
===================================================================
--- trunk/rental/inc/class.soparty.inc.php      2013-10-15 14:29:33 UTC (rev 
11374)
+++ trunk/rental/inc/class.soparty.inc.php      2013-10-17 13:20:52 UTC (rev 
11375)
@@ -16,7 +16,6 @@
        const LOCATION_INTERNAL = '.RESPONSIBILITY.INTERNAL';
 
        protected static $so;
-       protected $external_db = null;
 
        /**
         * Get a static reference to the storage object associated with this 
model object
@@ -31,54 +30,6 @@
                return self::$so;
        }
 
-       /* our simple php ping function */
-       function ping($host)
-       {
-                               exec(sprintf('ping -c 1 -W 5 %s', 
escapeshellarg($host)), $res, $rval);
-                               return $rval === 0;
-       }
-
-       public function get_external_db()
-               {
-                       if($this->external_db && is_object($this->external_db))
-                       {
-                               return $this->external_db;
-                       }
-
-                       $config = CreateObject('phpgwapi.config','rental');
-                       $config->read();
-
-                       if(! $config->config_data['external_db_host'] || 
!$this->ping($config->config_data['external_db_host']))
-                       {
-                               $message ="Database server 
{$config->config_data['external_db_host']} is not accessible";
-                               phpgwapi_cache::message_set($message, 'error');
-                               return false;
-                       }
-
-                       $db = createObject('phpgwapi.db', null, null, true);
-
-                       $db->debug = 
!!$config->config_data['external_db_debug'];
-                       $db->Host = $config->config_data['external_db_host'];
-                       $db->Port = $config->config_data['external_db_port'];
-                       $db->Type = $config->config_data['external_db_type'];
-                       $db->Database = 
$config->config_data['external_db_name'];
-                       $db->User = $config->config_data['external_db_user'];
-                       $db->Password = 
$config->config_data['external_db_password'];
-
-                       try
-                       {
-                               $db->connect();
-                               $connected = true;
-                       }
-                       catch(Exception $e)
-                       {
-                               $status = lang('unable_to_connect_to_database');
-                       }
-
-                       $this->external_db = $db;
-                       return $db;
-               }
-
        /**
         * Generate SQL query
         *
@@ -191,7 +142,8 @@
                        }
                }
 
-               if(isset($filters['contract_id'])){
+               if(isset($filters['contract_id']))
+               {
                        $contract_id = 
$this->marshal($filters['contract_id'],'int');
                        if(isset($contract_id) && $contract_id > 0)
                        {
@@ -199,7 +151,8 @@
                        }
                }
 
-               if(isset($filters['not_contract_id'])){
+               if(isset($filters['not_contract_id']))
+               {
                        $contract_id = 
$this->marshal($filters['not_contract_id'],'int');
                        if(isset($contract_id) && $contract_id > 0)
                        {
@@ -207,18 +160,22 @@
                        }
                }
 
-               if(isset($filters['org_unit_id'])){
+               if(isset($filters['org_unit_id']))
+               {
+                       $bofelles = rental_bofellesdata::get_instance();
                        $org_unit_id = 
$this->marshal($filters['org_unit_id'],'string');
                        if(isset($org_unit_id))
                        {
                                //check if org_unit is on top level
-                               if($this->org_unit_is_top_level($org_unit_id)){
+                               
if($bofelles->org_unit_is_top_level($org_unit_id))
+                               {
                                        //get connected units on level 4
-                                       $org_unit_ids_tmp = 
$this->get_org_unit_ids_from_top($org_unit_id);
+                                       $org_unit_ids_tmp = 
$bofelles->get_org_unit_ids_from_top($org_unit_id);
                                        $org_unit_ids = 
implode(',',$org_unit_ids_tmp);
                                        $filter_clauses[] = "party.org_enhet_id 
IN ({$org_unit_ids})";
                                }
-                               else{
+                               else
+                               {
                                        $filter_clauses[] = "party.org_enhet_id 
= {$org_unit_id}";
                                }
                        }
@@ -316,56 +273,12 @@
                {$this->left_join} rental_contract contract ON (contract.id = 
c_p.contract_id)";
 
                $joins = $join_contracts;
+
                return "SELECT {$cols} FROM {$tables} {$joins} WHERE 
{$condition} {$order}";
        }
 
-       function org_unit_is_top_level($org_unit_id)
-       {
-               $q = "select * from V_ORG_ENHET where org_enhet_id=$org_unit_id 
and org_nivaa < 4";
 
-               if(!$external_db = $this->get_external_db())
-               {
-                       return;
-               }
-               $result = $this->external_db->query($q);
 
-               if($this->external_db->next_record()) {
-                       return true;
-               }
-               else
-               {
-                       return false;
-               }
-       }
-
-       function get_org_unit_ids_from_top($org_unit_id){
-               $unit_ids = array();
-               $q="SELECT * FROM V_ORG_KNYTNING WHERE 
V_ORG_KNYTNING.ORG_ENHET_ID_KNYTNING=$org_unit_id";
-
-               if(!$external_db = $this->get_external_db())
-               {
-                       return;
-               }
-               if($external_db->Type == "postgres")
-               {
-                       $q = strtolower($q);
-               }
-               $result = $this->external_db->query($q);
-
-               while($this->external_db->next_record()){
-                       if($external_db->Type == "postgres")
-                       {
-                               $unit_id = 
$this->external_db->f('org_enhet_id');
-                       }
-                       else
-                       {
-                               $unit_id = 
$this->external_db->f('ORG_ENHET_ID');
-                       }
-                       $unit_ids[] = $unit_id;
-               }
-               return $unit_ids;
-       }
-
        /**
         * Function for adding a new party to the database. Updates the party 
object.
         *




reply via email to

[Prev in Thread] Current Thread [Next in Thread]