PageRenderTime 39ms CodeModel.GetById 8ms RepoModel.GetById 1ms app.codeStats 0ms

/application/models/companies/Company.class.php

https://gitlab.com/x33n/ProjectPier-Core
PHP | 766 lines | 307 code | 84 blank | 375 comment | 48 complexity | d577bf33d231bbccc3df1c796e76d0b2 MD5 | raw file
  1. <?php
  2. /**
  3. * Company class
  4. *
  5. * @http://www.projectpier.org/
  6. */
  7. class Company extends BaseCompany {
  8. /**
  9. * Cached array of active company projects
  10. *
  11. * @var array
  12. */
  13. private $active_projects;
  14. /**
  15. * Cached array of completed projects
  16. *
  17. * @var array
  18. */
  19. private $completed_projects;
  20. /**
  21. * Return display name of company
  22. *
  23. * @access public
  24. * @param void
  25. * @return string
  26. */
  27. function getDisplayName() {
  28. return $this->getName();
  29. } // getDisplayName
  30. /**
  31. * Return array of all company members
  32. *
  33. * @access public
  34. * @param void
  35. * @return array
  36. */
  37. function getContacts() {
  38. return Contacts::findAll(array(
  39. 'conditions' => '`company_id` = ' . DB::escape($this->getId()),
  40. 'order' => '`display_name` ASC'
  41. )); // findAll
  42. } // getContacts
  43. /**
  44. * Return number of company contacts
  45. *
  46. * @access public
  47. * @param void
  48. * @return integer
  49. */
  50. function countContacts() {
  51. return Contacts::count('`company_id` = ' . DB::escape($this->getId()));
  52. } // countContacts
  53. /**
  54. * Return company users
  55. *
  56. * @access public
  57. * @param void
  58. * @return array
  59. */
  60. function getUsers() {
  61. $users_table = Users::instance()->getTableName(true);
  62. $contacts_table = Contacts::instance()->getTableName(true);
  63. $users = array();
  64. $sql = "SELECT $users_table.* FROM $users_table, $contacts_table WHERE ($users_table.`id` = $contacts_table.`user_id` AND $contacts_table.`company_id` = ". DB::escape($this->getId()) . " ) ORDER BY $users_table.`username` ASC";
  65. $rows = DB::executeAll($sql);
  66. if (is_array($rows)) {
  67. foreach ($rows as $row) {
  68. $users[] = Users::instance()->loadFromRow($row);
  69. } // foreach
  70. } // if
  71. return count($users) ? $users : null;
  72. } // getUsers
  73. /**
  74. * Return number of company users
  75. *
  76. * @access public
  77. * @param void
  78. * @return integer
  79. */
  80. function countUsers() {
  81. $users_table = Users::instance()->getTableName(true);
  82. $contacts_table = Contacts::instance()->getTableName(true);
  83. $escaped_pk = is_array($pk_columns = Companies::getPkColumns()) ? '*' : DB::escapeField($pk_columns);
  84. $users = array();
  85. $sql = "SELECT COUNT($users_table.$escaped_pk) AS 'row_count' FROM $users_table, $contacts_table WHERE ($users_table.`id` = $contacts_table.`user_id` AND $contacts_table.`company_id` = ". DB::escape($this->getId()) . " )";
  86. $row = DB::executeOne($sql);
  87. return (integer) array_var($row, 'row_count', 0);
  88. } // countUsers
  89. /**
  90. * Return array of company users on specific project
  91. *
  92. * @access public
  93. * @param Project $project
  94. * @return array
  95. */
  96. function getUsersOnProject(Project $project) {
  97. return ProjectUsers::getCompanyUsersByProject($this, $project);
  98. } // getUsersOnProject
  99. /**
  100. * Return users that have auto assign value set to true
  101. *
  102. * @access public
  103. * @param void
  104. * @return array
  105. */
  106. function getAutoAssignUsers() {
  107. $users_table = Users::instance()->getTableName(true);
  108. $contacts_table = Contacts::instance()->getTableName(true);
  109. $users = array();
  110. $sql = "SELECT $users_table.* FROM $users_table, $contacts_table WHERE ($users_table.`id` = $contacts_table.`user_id` AND $contacts_table.`company_id` = ". DB::escape($this->getId()) . " AND $users_table.`auto_assign` > ". DB::escape(0) . " )";
  111. $rows = DB::executeAll($sql);
  112. if (is_array($rows)) {
  113. foreach ($rows as $row) {
  114. $users[] = Users::instance()->loadFromRow($row);
  115. } // foreach
  116. } // if
  117. return count($users) ? $users : null;
  118. } // getAutoAssignUsers
  119. /**
  120. * Return all client companies
  121. *
  122. * @access public
  123. * @param void
  124. * @return array
  125. */
  126. function getClientCompanies() {
  127. return Companies::getCompanyClients($this);
  128. } // getClientCompanies
  129. /**
  130. * Return number of client companies
  131. *
  132. * @access public
  133. * @param void
  134. * @return integer
  135. */
  136. function countClientCompanies() {
  137. return Companies::count('`client_of_id` = ' . DB::escape($this->getId()));
  138. } // countClientCompanies
  139. /**
  140. * Return all projects that this company is member of
  141. *
  142. * @access public
  143. * @param void
  144. * @return array
  145. */
  146. function getProjects() {
  147. return $this->isOwner() ? Projects::getAll() : ProjectCompanies::getProjectsByCompany($this);
  148. } // getProjects
  149. /**
  150. * Return total number of projects
  151. *
  152. * @access public
  153. * @param void
  154. * @return integer
  155. */
  156. function countProjects() {
  157. if ($this->isOwner()) {
  158. return Projects::count(); // all
  159. } else {
  160. return ProjectCompanies::count('`company_id` = ' . DB::escape($this->getId()));
  161. } // if
  162. } // countProjects
  163. /**
  164. * Return active projects that are owned by this company
  165. *
  166. * @param void
  167. * @return null
  168. */
  169. function getActiveProjects() {
  170. if (is_null($this->active_projects)) {
  171. if ($this->isOwner()) {
  172. $this->active_projects = Projects::findAll(array(
  173. 'conditions' => '`completed_on` = ' . DB::escape(EMPTY_DATETIME)
  174. )); // findAll
  175. } else {
  176. $this->active_projects = ProjectCompanies::getProjectsByCompany($this, '`completed_on` = ' . DB::escape(EMPTY_DATETIME));
  177. } // if
  178. } // if
  179. return $this->active_projects;
  180. } // getActiveProjects
  181. /**
  182. * Return all completed projects
  183. *
  184. * @param void
  185. * @return null
  186. */
  187. function getCompletedProjects() {
  188. if (is_null($this->completed_projects)) {
  189. if ($this->isOwner()) {
  190. $this->completed_projects = Projects::findAll(array(
  191. 'conditions' => '`completed_on` > ' . DB::escape(EMPTY_DATETIME)
  192. )); // findAll
  193. } else {
  194. $this->completed_projects = ProjectCompanies::getProjectsByCompany($this, '`completed_on` > ' . DB::escape(EMPTY_DATETIME));
  195. } // if
  196. } // if
  197. return $this->completed_projects;
  198. } // getCompletedProjects
  199. /**
  200. * Return all milestones scheduled for today
  201. *
  202. * @param void
  203. * @return array
  204. */
  205. function getTodayMilestones() {
  206. return ProjectMilestones::getTodayMilestonesByCompany($this);
  207. } // getTodayMilestones
  208. /**
  209. * Return all late milestones
  210. *
  211. * @param void
  212. * @return array
  213. */
  214. function getLateMilestones() {
  215. return ProjectMilestones::getLateMilestonesByCompany($this);
  216. } // getLateMilestones
  217. /**
  218. * Check if this company is owner company
  219. *
  220. * @param void
  221. * @return boolean
  222. */
  223. function isOwner() {
  224. if ($this->isNew()) {
  225. return false;
  226. } else {
  227. return $this->getClientOfId() == 0;
  228. } // if
  229. } // isOwner
  230. /**
  231. * Returns if company is a favorite
  232. *
  233. * @param void
  234. * @return boolean
  235. */
  236. function isFavorite() {
  237. return $this->getIsFavorite();
  238. } // isFavorite
  239. /**
  240. * Check if this company is part of specific project
  241. *
  242. * @param Project $project
  243. * @return boolean
  244. */
  245. function isProjectCompany(Project $project) {
  246. if ($this->isOwner() && ($project->getCompanyId() == $this->getId())) {
  247. return true;
  248. } // uf
  249. return ProjectCompanies::findById(array('project_id' => $project->getId(), 'company_id' => $this->getId())) instanceof ProjectCompany;
  250. } // isProjectCompany
  251. /**
  252. * This function will return true if we have data to show company address (address, city, country and zipcode)
  253. *
  254. * @access public
  255. * @param void
  256. * @return boolean
  257. */
  258. function hasAddress() {
  259. return trim($this->getAddress()) <> '' &&
  260. trim($this->getCity()) <> '' &&
  261. //trim($this->getZipcode()) <> '' &&
  262. trim($this->getCountry()) <> '';
  263. } // hasAddress
  264. /**
  265. * Check if this company have valid homepage address set
  266. *
  267. * @access public
  268. * @param void
  269. * @return boolean
  270. */
  271. function hasHomepage() {
  272. return trim($this->getHomepage()) <> '' && is_valid_url($this->getHomepage());
  273. } // hasHomepage
  274. /**
  275. * Return name of country
  276. *
  277. * @access public
  278. * @param void
  279. * @return string
  280. */
  281. function getCountryName() {
  282. return lang('country ' . $this->getCountry());
  283. } // getCountryName
  284. /**
  285. * Return location details
  286. *
  287. * @access public
  288. * @param void
  289. * @return string
  290. */
  291. function getLocationDetails() {
  292. $details = '';
  293. $details .= clean($this->getAddress());
  294. $details .= ' ' . clean($this->getAddress2());
  295. $details .= ' ' . clean($this->getZipcode());
  296. $details .= ' ' . clean($this->getCity());
  297. $details .= ' ' . clean($this->getCountryName());
  298. return $details;
  299. } // getCountryName
  300. /**
  301. * Returns true if company info is updated by the user since company is created. Company can be created
  302. * with empty company info
  303. *
  304. * @access public
  305. * @param void
  306. * @return boolean
  307. */
  308. function isInfoUpdated() {
  309. return $this->getCreatedOn()->getTimestamp() < $this->getUpdatedOn()->getTimestamp();
  310. } // isInfoUpdated
  311. /**
  312. * Set homepage URL
  313. *
  314. * This function is simple setter but it will check if protocol is specified for given URL. If it is not than
  315. * http will be used. Supported protocols are http and https for this type or URL
  316. *
  317. * @param string $value
  318. * @return null
  319. */
  320. function setHomepage($value) {
  321. if (trim($value) == '') {
  322. return parent::setHomepage('');
  323. } // if
  324. $check_value = strtolower($value);
  325. if (!str_starts_with($check_value, 'http://') && !str_starts_with($check_value, 'https://')) {
  326. return parent::setHomepage('http://' . $value);
  327. } else {
  328. return parent::setHomepage($value);
  329. } // if
  330. } // setHomepage
  331. // ---------------------------------------------------
  332. // Permissions
  333. // ---------------------------------------------------
  334. /**
  335. * Check if specific user can update this company
  336. *
  337. * @access public
  338. * @param User $user
  339. * @return boolean
  340. */
  341. function canEdit(User $user) {
  342. return $user->isAccountOwner() || $user->isAdministrator();
  343. } // canEdit
  344. /**
  345. * Check if specific user can delete this company
  346. *
  347. * @access public
  348. * @param User $user
  349. * @return boolean
  350. */
  351. function canDelete(User $user) {
  352. return $user->isAccountOwner() || $user->isAdministrator();
  353. } // canDelete
  354. /**
  355. * Returns true if specific user can add client company
  356. *
  357. * @access public
  358. * @param User $user
  359. * @return boolean
  360. */
  361. function canAddClient(User $user) {
  362. if (!$user->isMemberOf($this)) {
  363. return false;
  364. }
  365. return $user->isAccountOwner() || $user->isAdministrator($this);
  366. } // canAddClient
  367. /**
  368. * Check if this user can add new contact to this company
  369. *
  370. * @access public
  371. * @param User $user
  372. * @return boolean
  373. */
  374. function canAddContact(User $user) {
  375. return Contact::canAdd($user, $this);
  376. } // canAddContact
  377. /**
  378. * Check if this user can add new account to this company
  379. *
  380. * @access public
  381. * @param User $user
  382. * @return boolean
  383. */
  384. function canAddUser(User $user) {
  385. return User::canAdd($user, $this);
  386. } // canAddUser
  387. /**
  388. * Check if user can update permissions of this company
  389. *
  390. * @param User $user
  391. * @return boolean
  392. */
  393. function canUpdatePermissions(User $user) {
  394. if ($this->isOwner()) {
  395. return false; // owner company!
  396. } // if
  397. return $user->isAdministrator();
  398. } // canUpdatePermissions
  399. // ---------------------------------------------------
  400. // URLs
  401. // ---------------------------------------------------
  402. /**
  403. * Show company card page
  404. *
  405. * @access public
  406. * @param void
  407. * @return null
  408. */
  409. function getCardUrl() {
  410. return get_url('company', 'card', $this->getId());
  411. } // getCardUrl
  412. /**
  413. * Return view company URL
  414. *
  415. * @access public
  416. * @param void
  417. * @return string
  418. */
  419. function getViewUrl() {
  420. if ($this->getId() == owner_company()->getId()) {
  421. return get_url('administration', 'company');
  422. } else {
  423. return get_url('company', 'view_client', $this->getId());
  424. } // if
  425. } // getViewUrl
  426. /**
  427. * Edit owner company
  428. *
  429. * @access public
  430. * @param void
  431. * @return null
  432. */
  433. function getEditUrl() {
  434. return $this->isOwner() ? get_url('company', 'edit') : get_url('company', 'edit_client', $this->getId());
  435. } // getEditUrl
  436. /**
  437. * Return delete company URL
  438. *
  439. * @access public
  440. * @param void
  441. * @return string
  442. */
  443. function getDeleteClientUrl() {
  444. return get_url('company', 'delete_client', $this->getId());
  445. } // getDeleteClientUrl
  446. /**
  447. * Return update permissions URL
  448. *
  449. * @param void
  450. * @return string
  451. */
  452. function getUpdatePermissionsUrl() {
  453. return get_url('company', 'update_permissions', $this->getId());
  454. } // getUpdatePermissionsUrl
  455. /**
  456. * Return add contact URL
  457. *
  458. * @access public
  459. * @param void
  460. * @return string
  461. */
  462. function getAddContactUrl() {
  463. return get_url('contacts', 'add', array('company_id' => $this->getId()));
  464. } // getAddContactUrl
  465. /**
  466. * Return update avatar URL
  467. *
  468. * @access public
  469. * @param void
  470. * @return string
  471. */
  472. function getEditLogoUrl() {
  473. return get_url('company', 'edit_logo', $this->getId());
  474. } // getEditLogoUrl
  475. /**
  476. * Return delete logo URL
  477. *
  478. * @access public
  479. * @param void
  480. * @return string
  481. */
  482. function getDeleteLogoUrl() {
  483. return get_url('company', 'delete_logo', $this->getId());
  484. } // getDeleteLogoUrl
  485. /**
  486. * Return toggle favorite URL
  487. *
  488. * @param void
  489. * @return string
  490. */
  491. function getToggleFavoriteUrl($redirect_to = null) {
  492. $attributes = array('id' => $this->getId());
  493. if (trim($redirect_to) <> '') {
  494. $attributes['redirect_to'] = str_replace('&amp;', '&', trim($redirect_to));
  495. } // if
  496. return get_url('company', 'toggle_favorite', $attributes);
  497. } // getToggleFavoriteUrl
  498. /**
  499. * Show map page
  500. *
  501. * @access public
  502. * @param void
  503. * @return null
  504. */
  505. function getShowMapUrl() {
  506. $location = urlencode($this->getLocationDetails());
  507. $map_url = config_option('map url', 'http://maps.google.com?q=$location');
  508. $map_url = str_replace('$location', $location, $map_url);
  509. return $map_url;
  510. } // getShowMapUrl
  511. /**
  512. * Show route page
  513. *
  514. * @access public
  515. * @param void
  516. * @return null
  517. */
  518. function getShowRouteUrl($contact) {
  519. $to = '';
  520. if ($contact instanceof Contact) {
  521. $to = $contact->getLocationDetails();
  522. }
  523. $route_url = config_option('route url', 'http://maps.google.com?saddr=$from&daddr=$to');
  524. $route_url = str_replace('$to', $to, $route_url);
  525. $from = urlencode($this->getLocationDetails());
  526. $route_url = str_replace('$from', $from, $route_url);
  527. return $route_url;
  528. } // getShowRouteUrl
  529. // ---------------------------------------------------
  530. // Logo
  531. // ---------------------------------------------------
  532. /**
  533. * Set logo value
  534. *
  535. * @param string $source Source file
  536. * @param integer $max_width
  537. * @param integer $max_height
  538. * @param boolean $save Save object when done
  539. * @return null
  540. */
  541. function setLogo($source, $max_width = 50, $max_height = 50, $save = true) {
  542. if (!is_readable($source)) {
  543. return false;
  544. }
  545. do {
  546. $temp_file = ROOT . '/cache/' . sha1(uniqid(rand(), true));
  547. } while (is_file($temp_file));
  548. try {
  549. Env::useLibrary('simplegd');
  550. $image = new SimpleGdImage($source);
  551. $thumb = $image->scale($max_width, $max_height, SimpleGdImage::BOUNDARY_DECREASE_ONLY, false);
  552. $thumb->saveAs($temp_file, IMAGETYPE_PNG);
  553. $public_filename = PublicFiles::addFile($temp_file, 'png');
  554. if ($public_filename) {
  555. $this->setLogoFile($public_filename);
  556. if ($save) {
  557. $this->save();
  558. } // if
  559. } // if
  560. $result = true;
  561. } catch(Exception $e) {
  562. $result = false;
  563. } // try
  564. // Cleanup
  565. if (!$result && $public_filename) {
  566. PublicFiles::deleteFile($public_filename);
  567. } // if
  568. @unlink($temp_file);
  569. return $result;
  570. } // setLogo
  571. /**
  572. * Delete logo
  573. *
  574. * @param void
  575. * @return null
  576. */
  577. function deleteLogo() {
  578. if ($this->hasLogo()) {
  579. PublicFiles::deleteFile($this->getLogoFile());
  580. $this->setLogoFile('');
  581. } // if
  582. } // deleteLogo
  583. /**
  584. * Returns path of company logo. This function will not check if file really exists
  585. *
  586. * @access public
  587. * @param void
  588. * @return string
  589. */
  590. function getLogoPath() {
  591. return PublicFiles::getFilePath($this->getLogoFile());
  592. } // getLogoPath
  593. /**
  594. * description
  595. *
  596. * @access public
  597. * @param void
  598. * @return string
  599. */
  600. function getLogoUrl() {
  601. return $this->hasLogo() ? PublicFiles::getFileUrl($this->getLogoFile()) : get_image_url('logo.gif');
  602. } // getLogoUrl
  603. /**
  604. * Returns true if this company have logo file value and logo file exists
  605. *
  606. * @access public
  607. * @param void
  608. * @return boolean
  609. */
  610. function hasLogo() {
  611. return trim($this->getLogoFile()) && is_file($this->getLogoPath());
  612. } // hasLogo
  613. // ---------------------------------------------------
  614. // System functions
  615. // ---------------------------------------------------
  616. /**
  617. * Validate this object before save
  618. *
  619. * @param array $errors
  620. * @return boolean
  621. */
  622. function validate(&$errors) {
  623. if (!$this->validatePresenceOf('name')) {
  624. $errors[] = lang('company name required');
  625. } // if
  626. if ($this->validatePresenceOf('email')) {
  627. if (!is_valid_email($this->getEmail())) {
  628. $errors[] = lang('invalid email address');
  629. } // if
  630. } // if
  631. if ($this->validatePresenceOf('homepage')) {
  632. if (!is_valid_url($this->getHomepage())) {
  633. $errors[] = lang('company homepage invalid');
  634. } // if
  635. } // if
  636. } // validate
  637. /**
  638. * Delete this company and all related data
  639. *
  640. * @access public
  641. * @param void
  642. * @return boolean
  643. * @throws Error
  644. */
  645. function delete() {
  646. if ($this->isOwner()) {
  647. throw new Error(lang('error delete owner company'));
  648. } // if
  649. $contacts = $this->getContacts();
  650. if (is_array($contacts) && count($contacts)) {
  651. foreach ($contacts as $contact) {
  652. $contact->delete();
  653. }
  654. } // if
  655. ProjectCompanies::clearByCompany($this);
  656. $this->deleteLogo();
  657. return parent::delete();
  658. } // delete
  659. // ---------------------------------------------------
  660. // ApplicationDataObject implementation
  661. // ---------------------------------------------------
  662. /**
  663. * Return object URl
  664. *
  665. * @access public
  666. * @param void
  667. * @return string
  668. */
  669. function getObjectUrl() {
  670. return logged_user()->isAdministrator() ? $this->getViewUrl() : $this->getCardUrl();
  671. } // getObjectUrl
  672. /**
  673. * Return object type name
  674. *
  675. * @param void
  676. * @return string
  677. */
  678. function getObjectTypeName() {
  679. return lang('company');
  680. } // getObjectTypeName
  681. } // Company
  682. ?>