db/class_company.inc.php

00001 <?php if(!function_exists('startedIndexPhp')) { header("location:../index.php"); exit();}
00002 # streber - a php5 based project management system  (c) 2005-2007  / www.streber-pm.org
00003 # Distributed under the terms and conditions of the GPL as stated in lang/license.html
00004 
00016 global $g_company_fields;
00017 $g_company_fields=array();
00018 addProjectItemFields(&$g_company_fields);
00019 
00020 foreach(array(
00021     new FieldInternal(array(    'name'=>'id',
00022         'default'=>0,
00023         'in_db_object'=>1,
00024         'in_db_item'=>1,
00025     )),
00026     new FieldInternal(array(    'name'=>'state',    ### cached in project-table to speed up queries ###
00027         'default'=>1,
00028         'in_db_object'=>1,
00029         'in_db_item'=>1,
00030     )),
00031     new FieldString(array(
00032         'name'=>'name',
00033         'title'=>__('Name'),
00034         'tooltip'=>__('Required. (e.g. pixtur ag)'),
00035         'log_changes'=>true,
00036     )),
00037     new FieldString(array(
00038         'name'=>'short',
00039         'title'=>__('Short','form field for company'),
00040         'tooltip'=>__('Optional: Short name shown in lists (eg. pixtur)'),
00041         'log_changes'=>true,
00042     )),
00043     new FieldString(array(
00044         'name'=>'tagline',
00045         'title'=>__('Tag line','form field for company'),
00046         'tooltip'=>__('Optional: Additional tagline (eg. multimedia concepts)'),
00047         'log_changes'=>true,
00048     )),
00049     new FieldString(array(
00050         'name'=>'phone',
00051         'title'=>__('Phone','form field for company'),
00052         'tooltip'=>__('Optional: Phone (eg. +49-30-12345678)'),
00053         'log_changes'=>true,
00054     )),
00055     new FieldString(array(
00056         'name'=>'fax',
00057         'title'=>__('Fax','form field for company'),
00058         'tooltip'=>__('Optional: Fax (eg. +49-30-12345678)'),
00059         'log_changes'=>true,
00060     )),
00061     new FieldString(array(
00062         'name'=>'street',
00063         'title'=>__('Street'),
00064         'tooltip'=>__('Optional: (eg. Poststreet 28)'),
00065         'log_changes'=>true,
00066     )),
00067     new FieldString(array(
00068         'name'=>'zipcode',
00069         'title'=>__('Zipcode'),
00070         'tooltip'=>__('Optional: (eg. 12345 Berlin)'),
00071         'log_changes'=>true,
00072     )),
00073     new FieldString(array(
00074         'name'=>'homepage',
00075         'title'=>__('Website'),
00076         'tooltip'=>__('Optional: (eg. http://www.pixtur.de)'),
00077         'log_changes'=>true,
00078     )),
00079     new FieldString(array(
00080         'name'=>'intranet',
00081         'title'=>__('Intranet'),
00082         'tooltip'=>__('Optional: (eg. http://www.pixtur.de/login.php?name=someone)'),
00083         'log_changes'=>true,
00084     )),
00085     new FieldString(array(
00086         'name'=>'email',
00087         'title'=>__('E-Mail'),
00088         'tooltip'=>__('Optional: (eg. http://www.pixtur.de/login.php?name=someone)'),
00089         'log_changes'=>true,
00090     )),
00091     new FieldText(array(
00092         'name'=>'comments',
00093         'title'=>__('Comments','form label for company'),
00094         'tooltip'=>__('Optional'),
00095         'log_changes'=>true,
00096     )),
00097     ### company category ###
00098     new FieldInternal(array(
00099         'name'=>'category',
00100         'view_in_forms' =>false,
00101         'default'=>0,
00102         'log_changes'=>true,
00103     )),
00104 ) as $f) {
00105     $g_company_fields[$f->name]=$f;
00106 }
00107 
00108 
00109 
00110 
00111 
00112 class Company extends DbProjectItem
00113 {
00114 
00115     //=== constructor ================================================
00116     function __construct ($id_or_array=NULL)
00117     {
00118         global $g_company_fields;
00119         $this->fields= &$g_company_fields;
00120 
00121         parent::__construct($id_or_array);
00122         if(!$this->type) {
00123             $this->type= ITEM_COMPANY;
00124         }
00125     }
00126 
00127     #------------------------------------------------------------
00128     # returns link to company-view with short name
00129     #------------------------------------------------------------
00130     public function getLink($show_long=false)
00131     {
00132         global $PH;
00133         if($show_long) {
00134             $out='<span class="item company">'. $PH->getLink('companyView',$this->name,array('company'=>$this->id)).'</span>';
00135         }
00136         else {
00137             $out='<span class="item company">'. $PH->getLink('companyView',$this->getShortWithTitle(),array('company'=>$this->id),'item company',true).'</span>';
00138         }
00139         return $out;
00140     }
00141 
00142 
00148     static function getById($id)
00149     {
00150         $c= new Company($id);
00151         if($c->id) {
00152             return $c;
00153         }
00154         return NULL;
00155     }
00156 
00157 
00164     static function getVisibleById($id)
00165     {
00166 
00167         $companies= Company::getAll(array(
00168                     'order_str'=>NULL,
00169                     'has_id'=>$id));
00170 
00171         if(count($companies) == 1) {
00172             if($companies[0]->id) {
00173                 return $companies[0];
00174             }
00175         }
00176         return NULL;
00177     }
00178 
00182     static function getEditableById($id)
00183     {
00184         global $auth;
00185         if(
00186             $auth->cur_user->user_rights & RIGHT_COMPANY_EDIT
00187         ) {
00188             return Company::getById($id);
00189         }
00190         return NULL;
00191     }
00192 
00193 
00194     static function &queryFromDb($query_string)
00195     {
00196         $dbh = new DB_Mysql;
00197 
00198         $sth= $dbh->prepare($query_string);
00199 
00200         $sth->execute("",1);
00201         $tmp=$sth->fetchall_assoc();
00202         $companies=array();
00203         foreach($tmp as $t) {
00204             $company=new Company($t);
00205             $companies[]=$company;
00206         }
00207         return $companies;
00208     }
00209 
00210 
00211     #------------------------------------------------------------
00212     # get companies from db --> old function
00213     #------------------------------------------------------------
00214     /*public static function &getAll($order_str=NULL, $has_id=NULL, $search=NULL)
00215     {
00216         global $auth;
00217         $prefix = confGet('DB_TABLE_PREFIX');
00218 
00219         $has_id= intval($has_id);
00220 
00221         $str_has_id= $has_id
00222                 ? ('AND c.id='.intval($has_id))
00223                 : '';
00224 
00225         if($search) {
00226             $search= asMatchString($search);
00227             $AND_match= "AND (MATCH (c.name) AGAINST ('".asCleanString($search). "*') or MATCH (c.comments) AGAINST ('".asCleanString($search)."*'  IN BOOLEAN MODE))";
00228         }
00229         else {
00230             $AND_match= '';
00231         }
00232 
00233         ### show all ###
00234         if($auth->cur_user->user_rights & RIGHT_VIEWALL) {
00235             $str=
00236                 "SELECT c.*, ic.* from {$prefix}company c, {$prefix}item ic
00237                 WHERE
00238                       c.state = 1
00239                       $str_has_id
00240                       AND c.id = ic.id
00241                       $AND_match"
00242                 . getOrderByString($order_str, 'c.name');
00243         }
00244 
00245         ### only related companies ###
00246         else {
00247                $str= "SELECT DISTINCT c.*, ic.* from {$prefix}company c, {$prefix}project p, {$prefix}projectperson upp, {$prefix}item ic
00248                  WHERE
00249                         upp.person = {$auth->cur_user->id}
00250                     AND upp.state = 1   /*      /* upp all user projectpersons */
00251 
00252                     /*AND  upp.project = p.id*/  /* all user projects */
00253                      /*AND  p.company = c.id*/    /* all companies */
00254                       /* AND  c.state = 1
00255                        $str_has_id
00256                          AND c.id = ic.id
00257 
00258 
00259                       $AND_match
00260 
00261                 ". getOrderByString($order_str, 'c.name');
00262         }
00263         $companies = self::queryFromDb($str);               # store in variable to return by reference
00264         return $companies;
00265     }*/
00266 
00267     #------------------------------------------------------------
00268     # get specific companies from db
00269     #------------------------------------------------------------
00270     public static function &getAll($args=NULL)
00271     {
00272         global $auth;
00273         $prefix = confGet('DB_TABLE_PREFIX');
00274 
00275         ### default parameter ###
00276         $order_str=NULL;
00277         $has_id=NULL;
00278         $search=NULL;
00279         $comcat=NULL;
00280 
00281         ### filter parameter ###
00282         if($args) {
00283             foreach($args as $key=>$value) {
00284                 if(!isset($$key) && !is_null($$key) && !$$key==="") {
00285                     trigger_error("unknown parameter",E_USER_NOTICE);
00286                 }
00287                 else {
00288                     $$key= $value;
00289                 }
00290             }
00291         }
00292 
00293         #$has_id= intval($has_id);
00294 
00295         $str_has_id= $has_id
00296                 ? ('AND c.id='.intval($has_id))
00297                 : '';
00298 
00299         if($search) {
00300             $search= asMatchString($search);
00301             $AND_match= "AND (MATCH (c.name) AGAINST ('".asCleanString($search). "*') or MATCH (c.comments) AGAINST ('".asCleanString($search)."*'  IN BOOLEAN MODE))";
00302         }
00303         else {
00304             $AND_match= '';
00305         }
00306 
00307         if(is_null($comcat))
00308         {
00309             $str_comcat = '';
00310         }
00311         else
00312         {
00313             $str_comcat = 'AND c.category=' .intval($comcat);
00314         }
00315 
00316         ### show all ###
00317         if($auth->cur_user->user_rights & RIGHT_VIEWALL) {
00318             $str=
00319                 "SELECT c.*, ic.* from {$prefix}company c, {$prefix}item ic
00320                 WHERE
00321                       c.state = 1
00322                       $str_has_id
00323                       AND c.id = ic.id
00324                       $AND_match
00325                       $str_comcat "
00326                 . getOrderByString($order_str, 'c.name');
00327         }
00328 
00329         ### only related companies ###
00330         else {
00331                $str= "SELECT DISTINCT c.*, ic.* from {$prefix}company c, {$prefix}project p, {$prefix}projectperson upp, {$prefix}item ic
00332                  WHERE
00333                         upp.person = {$auth->cur_user->id}
00334                     AND upp.state = 1         /* upp all user projectpersons */
00335 
00336                     AND  upp.project = p.id  /* all user projects */
00337                        AND  p.company = c.id    /* all companies */
00338                        AND  c.state = 1
00339                        $str_has_id
00340                          AND c.id = ic.id
00341 
00342 
00343                       $AND_match
00344                       $str_comcat "
00345                 . getOrderByString($order_str, 'c.name');
00346         }
00347 
00348 
00349         $companies = self::queryFromDb($str);               # store in variable to return by reference
00350         return $companies;
00351 
00352     }
00353 
00354     /*public static function &getCompanies($order_str=NULL, $has_id=NULL, $search=NULL, $comcat=NULL)
00355     {
00356         global $auth;
00357         $prefix = confGet('DB_TABLE_PREFIX');
00358 
00359         $has_id= intval($has_id);
00360 
00361         $str_has_id= $has_id
00362                 ? ('AND c.id='.intval($has_id))
00363                 : '';
00364 
00365         if($search) {
00366             $search= asMatchString($search);
00367             $AND_match= "AND (MATCH (c.name) AGAINST ('".asCleanString($search). "*') or MATCH (c.comments) AGAINST ('".asCleanString($search)."*'  IN BOOLEAN MODE))";
00368         }
00369         else {
00370             $AND_match= '';
00371         }
00372 
00373         if(is_null($comcat))
00374         {
00375             $str_comcat = '';
00376         }
00377         else
00378         {
00379             $str_comcat = 'AND c.category=' .intval($comcat);
00380         }
00381 
00382         ### show all ###
00383         if($auth->cur_user->user_rights & RIGHT_VIEWALL) {
00384             $str=
00385                 "SELECT c.*, ic.* from {$prefix}company c, {$prefix}item ic
00386                 WHERE
00387                       c.state = 1
00388                       $str_has_id
00389                       AND c.id = ic.id
00390                       $AND_match
00391                       $str_comcat "
00392                 . getOrderByString($order_str, 'c.name');
00393         }
00394 
00395         ### only related companies ###
00396         else {
00397                $str= "SELECT DISTINCT c.*, ic.* from {$prefix}company c, {$prefix}project p, {$prefix}projectperson upp, {$prefix}item ic
00398                  WHERE
00399                         upp.person = {$auth->cur_user->id}
00400                     AND upp.state = 1   */      /*upp all user projectpersons */
00401 
00402                     /*AND  upp.project = p.id*/  /* all user projects */
00403                     /*   AND  p.company = c.id*/    /* all companies */
00404                     /*   AND  c.state = 1
00405                        $str_has_id
00406                          AND c.id = ic.id
00407 
00408 
00409                       $AND_match
00410                       $str_comcat "
00411                 . getOrderByString($order_str, 'c.name');
00412         }
00413 
00414 
00415         $companies = self::queryFromDb($str);               # store in variable to return by reference
00416         return $companies;
00417     }*/
00418 
00419     #---------------------------
00420     # get nume tasks
00421     #---------------------------
00422     function getNumOpenProjects()
00423     {
00424         $prefix= confGet('DB_TABLE_PREFIX');
00425         $dbh = new DB_Mysql;
00426         $AND_status_min= "AND status >= ".STATUS_NEW;
00427         $AND_status_max= "AND status <= ".STATUS_OPEN;
00428         $sth= $dbh->prepare("
00429             SELECT
00430             COUNT(*)
00431             FROM {$prefix}project
00432             WHERE company = \"$this->id\"
00433               AND state=1
00434               $AND_status_min
00435               $AND_status_max
00436               "
00437 
00438               );
00439         $sth->execute("",1);
00440         $tmp=$sth->fetchall_assoc();
00441         return $tmp[0]['COUNT(*)'];
00442     }
00443 
00444     #---------------------------
00445     # get projects of company
00446     #---------------------------
00447     function getProjects($f_order_by=NULL, $f_status_min=1, $f_status_max=4)
00448     {
00449         global $auth;
00450         $prefix= confGet('DB_TABLE_PREFIX');
00451 
00452         $status_min= intval($f_status_min);
00453         $status_max= intval($f_status_max);
00454 
00455 
00456 
00457 
00458 
00459        #"SELECT * FROM {$prefix}project WHERE company = \"$this->id\" AND state=1 ORDER BY name"
00460 
00461         ### all projects ###
00462         if($auth->cur_user->user_rights & RIGHT_PROJECT_ASSIGN) {
00463             $str=
00464                 "SELECT p.* from {$prefix}project p
00465                 WHERE
00466                        p.status <= $status_max
00467                    AND p.status >= $status_min
00468                    AND p.company =  $this->id
00469                    AND p.state = 1
00470 
00471                 ". getOrderByString($f_order_by,'name');
00472         }
00473 
00474         ### only assigned projects ###
00475         else {
00476             $str=
00477                 "SELECT p.* from {$prefix}project p, {$prefix}projectperson upp
00478                 WHERE
00479                         upp.person = {$auth->cur_user->id}
00480                     AND upp.state = 1       /* all projectpersons of user */
00481 
00482 
00483                     AND   p.id  = upp.project       /* all projects of user */
00484                     AND   p.company = $this->id     /* all project of this company */
00485                     AND   p.status <= $status_max
00486                     AND   p.status >= $status_min
00487                     AND   p.state = 1
00488 
00489                 ". getOrderByString($f_order_by, 'name');
00490         }
00491 
00492         $dbh = new DB_Mysql;
00493         $sth= $dbh->prepare($str);
00494 
00495 
00496         $sth->execute("",1);
00497         $tmp=$sth->fetchall_assoc();
00498         $projects=array();
00499         foreach($tmp as $t) {
00500             $projects[]=new Project($t);
00501         }
00502         return $projects;
00503     }
00504 
00505     #---------------------------
00506     # get Employments
00507     #---------------------------
00508     function getEmployments()
00509     {
00510         $prefix= confGet('DB_TABLE_PREFIX');
00511         require_once(confGet('DIR_STREBER') . 'db/class_employment.inc.php');
00512 
00513         $dbh = new DB_Mysql;
00514         $sth= $dbh->prepare(
00515             "SELECT em.* FROM {$prefix}employment em, {$prefix}item i
00516               WHERE  i.type= ".ITEM_EMPLOYMENT."
00517               AND    i.state=1
00518               AND   i.id = em.id
00519               AND   em.company = $this->id
00520               "
00521         );
00522         $sth->execute("",1);
00523         $tmp=$sth->fetchall_assoc();
00524         $es=array();
00525         foreach($tmp as $t) {
00526             $es[]=new Employment($t);
00527         }
00528         return $es;
00529     }
00530 
00536     function getPersons()
00537     {
00538         $prefix= confGet('DB_TABLE_PREFIX');
00539         require_once(confGet('DIR_STREBER') . 'db/class_person.inc.php');
00540         require_once(confGet('DIR_STREBER') . 'db/class_employment.inc.php');
00541         $dbh = new DB_Mysql;
00542         $sth= $dbh->prepare(
00543                         "SELECT p.* FROM {$prefix}person p,{$prefix}employment em, {$prefix}item i
00544                         WHERE   i.type= ".ITEM_EMPLOYMENT."
00545                         AND     i.state=1
00546                         AND     i.id= em.id
00547                         AND     em.company = \"$this->id\"
00548                         AND     em.person= p.id
00549                         AND     p.state=1"
00550         );
00551         
00552         $sth->execute("",1);
00553         $tmp=$sth->fetchall_assoc();
00554         $es=array();
00555 
00556         foreach($tmp as $t) {
00557             if($person = Person::getVisibleById($t['id'])) {
00558                $es[]=$person;
00559             }
00560         }
00561         
00562         return $es;
00563     }
00564 
00565 
00566 
00567     #---------------------------
00568     # get PersonLinks
00569     #---------------------------
00570     function getPersonLinks($show_max_number=3)
00571     {
00572         $ps= $this->getPersons();
00573         $buffer= '';
00574         $sep= '';
00575         $num=0;
00576         foreach($ps as $p) {
00577             $buffer.= $sep.$p->getLink();
00578             if(++$num>$show_max_number) {
00579                 break;
00580             }
00581             $sep=", ";
00582         }
00583         return $buffer;
00584     }
00585 
00586     #---------------------------
00587     # is company visible to user?
00588     #---------------------------
00589     function validateView()
00590     {
00591         global $auth;
00592         global $PH;
00593         $prefix= confGet('DB_TABLE_PREFIX');
00594 
00595         ### all ###
00596         if($auth->cur_user->user_rights & RIGHT_COMPANY_VIEWALL) {
00597             return true;
00598         }
00599 
00600 
00601            $str= "SELECT COUNT(*) from {$prefix}company c, {$prefix}project p, {$prefix}projectperson upp
00602              WHERE
00603                     upp.person = {$auth->cur_user->id}
00604                 AND upp.state = 1         /* upp all user projectpersons */
00605 
00606                 AND  p.id = upp.project   /* all user projects */
00607                 AND  c.id = p.company     /* all companies */
00608                 AND  c.id = $this->id
00609                 AND  c.state = 1
00610             ";
00611 
00612         $dbh = new DB_Mysql;
00613         $sth= $dbh->prepare($str);
00614         $sth->execute("",1);
00615         $tmp=$sth->fetchall_assoc();
00616 
00617         $count= $tmp[0]['COUNT(*)'];
00618 
00619         if($count == 1) {
00620             return true;
00621         }
00622         else if($count > 1) {
00623             $PH->abortWarning(__("more than expected"),ERROR_BUG);
00624         }
00625         else{
00626             $PH->abortWarning(__("not available"),ERROR_RIGHTS);
00627         }
00628     }
00629 
00630 
00631 
00632 
00633 }
00634 
00635 
00636 
00637 
00638 ?>

Generated on Sun Mar 4 17:19:27 2007 for streber by  doxygen 1.5.1-p1