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:
00077 'log_changes'=>true,
00078 )),
00079 new FieldString(array(
00080 'name'=>'intranet',
00081 'title'=>__('Intranet'),
00082 'tooltip'=>__('Optional: (eg. http:
00083 'log_changes'=>true,
00084 )),
00085 new FieldString(array(
00086 'name'=>'email',
00087 'title'=>__('E-Mail'),
00088 'tooltip'=>__('Optional: (eg. http:
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
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
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
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
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
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 ?>