PageRenderTime 32ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 1ms

/application/controllers/ProjectController.class.php

https://github.com/fb83/Project-Pier
PHP | 1506 lines | 1087 code | 214 blank | 205 comment | 190 complexity | 0876b93b35635e7b00a41654915e17cc MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0, AGPL-3.0, LGPL-2.1, GPL-3.0
  1. <?php
  2. /**
  3. * Project controller
  4. *
  5. * @version 1.0
  6. * @http://www.projectpier.org/
  7. */
  8. class ProjectController extends ApplicationController {
  9. /**
  10. * Prepare this controller
  11. *
  12. * @param void
  13. * @return ProjectController
  14. */
  15. function __construct() {
  16. parent::__construct();
  17. prepare_company_website_controller($this, 'project_website');
  18. } // __construct
  19. /**
  20. * Call overview action
  21. *
  22. * @param void
  23. * @return null
  24. */
  25. function index() {
  26. $this->forward('overview');
  27. } // index
  28. /**
  29. * Show project overview
  30. *
  31. * @param void
  32. * @return null
  33. */
  34. function overview() {
  35. if (active_project() == null || !logged_user()->isProjectUser(active_project())) {
  36. flash_error(lang('no access permissions'));
  37. $this->redirectTo('dashboard', 'index');
  38. } // if
  39. $this->addHelper('textile');
  40. $project = active_project();
  41. $page_attachments = PageAttachments::getAttachmentsByPageNameAndProject('project_overview', active_project());
  42. $this->setLayout('project_website');
  43. tpl_assign('page_attachments', $page_attachments);
  44. tpl_assign('project_log_entries', $project->getProjectLog(
  45. config_option('project_logs_per_page', 20)
  46. ));
  47. tpl_assign('project', $project);
  48. tpl_assign('subprojects', $project->getSubprojects());
  49. tpl_assign('late_milestones', $project->getLateMilestones());
  50. tpl_assign('today_milestones', $project->getTodayMilestones());
  51. tpl_assign('upcoming_milestones', $project->getUpcomingMilestones());
  52. // Sidebar
  53. tpl_assign('visible_forms', $project->getVisibleForms(true));
  54. tpl_assign('project_companies', $project->getVisibleCompanies(logged_user()));
  55. tpl_assign('project_users', $project->getVisibleUsers(logged_user()));
  56. tpl_assign('important_messages', active_project()->getImportantMessages());
  57. tpl_assign('important_files', active_project()->getImportantFiles());
  58. $this->setSidebar(get_template_path('overview_sidebar', 'project'));
  59. } // overview
  60. /**
  61. * Execute search
  62. *
  63. * @param void
  64. * @return null
  65. */
  66. function search() {
  67. if (!logged_user()->isProjectUser(active_project())) {
  68. flash_error(lang('no access permissions'));
  69. $this->redirectTo('dashboard');
  70. } // if
  71. $search_for = array_var($_GET, 'search_for');
  72. $page = (integer) array_var($_GET, 'page', 1);
  73. if ($page < 1) {
  74. $page = 1;
  75. }
  76. if (trim($search_for) == '') {
  77. $search_results = null;
  78. $pagination = null;
  79. } else {
  80. list($search_results, $pagination) = SearchableObjects::searchPaginated($search_for, active_project(), logged_user()->isMemberOfOwnerCompany(), 10, $page);
  81. } // if
  82. tpl_assign('search_string', $search_for);
  83. tpl_assign('current_page', $page);
  84. tpl_assign('search_results', $search_results);
  85. tpl_assign('pagination', $pagination);
  86. $tag_names = plugin_active('tags') ? active_project()->getTagNames() : '';
  87. tpl_assign('tag_names', $tag_names);
  88. $this->setSidebar(get_template_path('search_sidebar', 'project'));
  89. } // search
  90. /**
  91. * Show tags page
  92. *
  93. * @param void
  94. * @return null
  95. */
  96. function tags() {
  97. if (!logged_user()->isProjectUser(active_project())) {
  98. flash_error(lang('no access permissions'));
  99. $this->redirectTo('dashboard', 'index');
  100. } // if
  101. tpl_assign('tag_names', active_project()->getTagNames());
  102. } // tags
  103. /**
  104. * List all companies and users involved in this project
  105. *
  106. * @param void
  107. * @return null
  108. */
  109. function people() {
  110. if (!logged_user()->isProjectUser(active_project())) {
  111. flash_error(lang('no access permissions'));
  112. $this->redirectTo('dashboard', 'index');
  113. } // if
  114. $this->addHelper('textile');
  115. $page_attachments = PageAttachments::getAttachmentsByTypeAndProject(array('Contacts', 'Companies'), active_project());
  116. tpl_assign('page_attachments', $page_attachments);
  117. tpl_assign('project', active_project());
  118. } // people
  119. /**
  120. * Show permission update form
  121. *
  122. * @param void
  123. * @return null
  124. */
  125. function permissions() {
  126. $project = active_project();
  127. if (!$project->canChangePermissions(logged_user())) {
  128. flash_error(lang('no access permissions'));
  129. $this->redirectToUrl($project->getOverviewUrl());
  130. } // if
  131. tpl_assign('project_users', $project->getUsers(false));
  132. tpl_assign('project_companies', $project->getCompanies());
  133. tpl_assign('user_projects', logged_user()->getProjects());
  134. $permissions = PermissionManager::getPermissionsText();
  135. tpl_assign('permissions', $permissions);
  136. $companies = array(owner_company());
  137. $clients = owner_company()->getClientCompanies();
  138. if (is_array($clients)) {
  139. $companies = array_merge($companies, $clients);
  140. } // if
  141. tpl_assign('companies', $companies);
  142. if (array_var($_POST, 'process') == 'process') {
  143. try {
  144. DB::beginWork();
  145. $project->clearCompanies();
  146. $project->clearUsers();
  147. $companies = array(owner_company());
  148. $client_companies = owner_company()->getClientCompanies();
  149. if (is_array($client_companies)) {
  150. $companies = array_merge($companies, $client_companies);
  151. } // if
  152. foreach ($companies as $company) {
  153. trace(__FILE__,"permissions(): processing company {$company->getId()}");
  154. // Company is selected!
  155. if (array_var($_POST, 'project_company_' . $company->getId()) == 'checked') {
  156. $is_owner_company = $company->isOwner();
  157. // Owner company is automatically included so it does not need to be in project_companies table
  158. if (!$is_owner_company) {
  159. $project_company = new ProjectCompany();
  160. $project_company->setProjectId(active_project()->getId());
  161. $project_company->setCompanyId($company->getId());
  162. $project_company->save();
  163. } // if
  164. $users = $company->getUsers();
  165. if (is_array($users)) {
  166. $counter = 0;
  167. foreach ($users as $user) {
  168. trace(__FILE__,"permissions(): processing user {$user->getId()} in company {$company->getId()}");
  169. $user_id = $user->getId();
  170. $counter++;
  171. if (array_var($_POST, "project_user_$user_id") == 'checked') {
  172. $project_user = new ProjectUser();
  173. $project_user->setProjectId($project->getId());
  174. $project_user->setUserId($user_id);
  175. $project_user->save();
  176. foreach ($permissions as $permission_name => $permission_text) {
  177. // Owner company members have all permissions
  178. $permission_value = $is_owner_company ? true : array_var($_POST, 'project_user_' . $user_id . '_' . $permission_name) == 'checked';
  179. //$user = Users::findById($project_user->getUserId());
  180. trace(__FILE__,"permissions(): processing permission $permission_name=$permission_value for user {$user->getId()} in company {$company->getId()}");
  181. $user->setProjectPermission($project,$permission_name,$permission_value);
  182. } // if
  183. } // if
  184. } // foreach
  185. } // if
  186. } // if
  187. } // foreach
  188. DB::commit();
  189. flash_success(lang('success update project permissions'));
  190. $this->redirectTo('project', 'overview');
  191. } catch(Exception $e) {
  192. DB::rollback();
  193. flash_error(lang('error update project permissions', $e->__toString() ));
  194. $this->redirectTo('project', 'permissions');
  195. } // try
  196. } // if
  197. } // permissions
  198. /**
  199. * Add project
  200. *
  201. * @param void
  202. * @return null
  203. */
  204. function add() {
  205. $this->setTemplate('add_project');
  206. $this->setLayout('administration');
  207. if (!logged_user()->canManageProjects()) {
  208. flash_error(lang('no access permissions'));
  209. $this->redirectToReferer(get_url('dashboard'));
  210. } // if
  211. $project = new Project();
  212. $project_data = array_var($_POST, 'project');
  213. $page_name = 'project_overview';
  214. $page_attachments = PageAttachments::getAttachmentsByPageNameAndProject($page_name, $project);
  215. $redirect_to = urldecode(array_var($_GET, 'redirect_to'));
  216. tpl_assign('project', $project);
  217. tpl_assign('project_data', $project_data);
  218. tpl_assign('page_attachments', $page_attachments);
  219. tpl_assign('redirect_to', $redirect_to);
  220. // Submitted...
  221. if (is_array($project_data)) {
  222. $project->setFromAttributes($project_data);
  223. $default_folders = array();
  224. if (plugin_active('files')) {
  225. $default_folders_config = str_replace(array("\r\n", "\r"), array("\n", "\n"), config_option('default_project_folders', ''));
  226. if (trim($default_folders_config) == '') {
  227. $default_folders = array();
  228. } else {
  229. $default_folders = explode("\n", $default_folders_config);
  230. } // if
  231. } // if
  232. $default_ticket_categories = array();
  233. if (plugin_active('tickets')) {
  234. $default_ticket_categories_config = str_replace(array("\r\n", "\r"), array("\n", "\n"), config_option('tickets_default_categories', ''));
  235. if (trim($default_ticket_categories_config) == '') {
  236. $default_ticket_categories = array();
  237. } else {
  238. $default_ticket_categories = explode("\n", $default_ticket_categories_config);
  239. } // if
  240. } // if
  241. try {
  242. DB::beginWork();
  243. $project->save();
  244. $permissions = array_keys(PermissionManager::getPermissionsText());
  245. $auto_assign_users = owner_company()->getAutoAssignUsers();
  246. // We are getting the list of auto assign users. If current user is not in the list
  247. // add it. He's creating the project after all...
  248. if (is_array($auto_assign_users)) {
  249. $auto_assign_logged_user = false;
  250. foreach ($auto_assign_users as $user) {
  251. if ($user->getId() == logged_user()->getId()) {
  252. $auto_assign_logged_user = true;
  253. }
  254. } // if
  255. if (!$auto_assign_logged_user) {
  256. $auto_assign_users[] = logged_user();
  257. }
  258. } else {
  259. $auto_assign_users[] = logged_user();
  260. } // if
  261. foreach ($auto_assign_users as $auto_assign_user) {
  262. $project_user = new ProjectUser();
  263. $project_user->setProjectId($project->getId());
  264. $project_user->setUserId($auto_assign_user->getId());
  265. $project_user->save();
  266. if (is_array($permissions)) {
  267. foreach ($permissions as $permission) {
  268. $auto_assign_user->setProjectPermission($project,$permission,true);
  269. }
  270. } // if
  271. } // foreach
  272. if (count($default_folders)) {
  273. $added_folders = array();
  274. foreach ($default_folders as $default_folder) {
  275. $folder_name = trim($default_folder);
  276. if ($folder_name == '') {
  277. continue;
  278. } // if
  279. if (in_array($folder_name, $added_folders)) {
  280. continue;
  281. } // if
  282. $folder = new ProjectFolder();
  283. $folder->setProjectId($project->getId());
  284. $folder->setName($folder_name);
  285. $folder->save();
  286. $added_folders[] = $folder_name;
  287. } // foreach
  288. } // if
  289. if (count($default_ticket_categories)) {
  290. $added_categories = array();
  291. foreach ($default_ticket_categories as $default_ticket_category) {
  292. $category_name = trim($default_ticket_category);
  293. if ($category_name == '') {
  294. continue;
  295. } // if
  296. if (in_array($category_name, $added_categories)) {
  297. continue;
  298. } // if
  299. $folder = new ProjectCategory();
  300. $folder->setProjectId($project->getId());
  301. $folder->setName($category_name);
  302. $folder->save();
  303. $added_categories[] = $category_name;
  304. } // foreach
  305. } // if
  306. $efqm_project = (isset($project_data['efqm_project'])) ? ($project_data['efqm_project']=='1') : false;
  307. if ($efqm_project) {
  308. // insert 9 milestones with task lists
  309. $efqm_template = array(
  310. 'efqm leadership' => array('a', 'b', 'c', 'd', 'e'),
  311. 'efqm strategy' => array('a', 'b', 'c', 'd'),
  312. 'efqm people' => array('a', 'b', 'c', 'd', 'e'),
  313. 'efqm partnership and resources' => array('a', 'b', 'c', 'd', 'e'),
  314. 'efqm processes products services' => array('a', 'b', 'c', 'd', 'e'),
  315. 'efqm customer results' => array('a', 'b'),
  316. 'efqm people results' => array('a', 'b'),
  317. 'efqm society results' => array('a', 'b'),
  318. 'efqm key results' => array('a', 'b'),
  319. );
  320. foreach($efqm_template as $criteria => $subcriteria) {
  321. $milestone = new ProjectMilestone();
  322. $milestone->setProjectId($project->getId());
  323. $milestone->setName(lang($criteria));
  324. $milestone->setGoal(config_option('initial goal', 80));
  325. $milestone->setDueDate(DateTimeValueLib::now());
  326. $offset_in_days = config_option('due date offset', 90);
  327. $milestone->getDueDate()->advance(60*60*24*$offset_in_days);
  328. $milestone->save();
  329. foreach($subcriteria as $subname) {
  330. $task_list = new ProjectTaskList();
  331. $task_list->setMilestoneId($milestone->getId());
  332. $task_list->setProjectId($project->getId());
  333. $task_list->setName(lang($criteria) . ' ' . $subname);
  334. $task_list->setDueDate($milestone->getDueDate());
  335. $task_list->setScore(config_option('initial score', 50));
  336. $task_list->save();
  337. }
  338. }
  339. }
  340. ApplicationLogs::createLog($project, null, ApplicationLogs::ACTION_ADD, false, true);
  341. DB::commit();
  342. flash_success(lang('success add project', $project->getName()));
  343. $this->redirectToUrl($project->getPermissionsUrl());
  344. } catch(Exception $e) {
  345. tpl_assign('error', $e);
  346. DB::rollback();
  347. } // try
  348. } // if
  349. } // add
  350. /**
  351. * Copy project
  352. *
  353. * @param void
  354. * @return null
  355. */
  356. function copy() {
  357. trace(__FILE__,"copy():begin");
  358. if (!Project::canAdd(logged_user())) {
  359. flash_error(lang('no access permissions'));
  360. $this->redirectToReferer(get_url('dashboard'));
  361. } // if
  362. $this->setTemplate('copy_project');
  363. $this->setLayout('administration');
  364. $project_data = array_var($_POST, 'project');
  365. tpl_assign('project_data', $project_data);
  366. // Submitted...
  367. if (is_array($project_data)) {
  368. $source = Projects::findById($project_data['source']);
  369. if (!($source instanceof Project)) {
  370. flash_error(lang('project dnx'));
  371. $this->redirectTo('administration', 'projects');
  372. } // if
  373. try {
  374. $shift_dates = (isset($project_data['shift_dates'])) ? ($project_data['shift_dates']=='checked') : false;
  375. $copy_details = (isset($project_data['copy_details'])) ? ($project_data['copy_details']=='checked') : false;
  376. $copy_tasks = (isset($project_data['copy_tasks'])) ? ($project_data['copy_tasks']=='checked') : false;
  377. $copy_milestones = (isset($project_data['copy_milestones'])) ? ($project_data['copy_milestones']=='checked') : false;
  378. $copy_messages = (isset($project_data['copy_messages'])) ? ($project_data['copy_messages']=='checked') : false;
  379. $copy_links = (isset($project_data['copy_links'])) ? ($project_data['copy_links']=='checked') : false;
  380. $copy_files = (isset($project_data['copy_files'])) ? ($project_data['copy_files']=='checked') : false;
  381. $copy_users = (isset($project_data['copy_users'])) ? ($project_data['copy_users']=='checked') : false;
  382. $copy_pages = (isset($project_data['copy_pages'])) ? ($project_data['copy_pages']=='checked') : false;
  383. DB::beginWork();
  384. $project = new Project();
  385. $new_name = lang( 'projects copy new name', $source->getName() );
  386. $new_name .= date(' z H:i:s');
  387. $project->setName($new_name);
  388. if ($copy_details) {
  389. $project->setDescription( $source->getDescription() );
  390. $project->setPriority( $source->getPriority() );
  391. $project->setShowDescriptionInOverview( $source->getShowDescriptionInOverview() );
  392. }
  393. $project->save();
  394. $project_id = $project->getId();
  395. $add_seconds = 0;
  396. if (isset($project_data['add_days'])) {
  397. $add_days = 0 + trim( $project_data['add_days'] );
  398. $add_seconds = $add_days * 24 * 60 * 60;
  399. }
  400. $source_created_on = $source->getCreatedOn();
  401. //var_dump($source_created_on);
  402. $milestone_map = array( 0 => 0 );
  403. // project milestones
  404. if ($copy_milestones) {
  405. $source_milestones = $source->getAllMilestones();
  406. if (is_array($source_milestones)) {
  407. foreach ($source_milestones as $source_milestone) {
  408. $milestone = new ProjectMilestone();
  409. //$milestone->copy($source_milestone);
  410. $milestone->setName($source_milestone->getName());
  411. $milestone->setDescription($source_milestone->getDescription());
  412. if ($shift_dates) {
  413. trace(__FILE__,"copy():shift dates");
  414. $milestone->setDueDate(DateTimeValueLib::now());
  415. $seconds = $source_milestone->getDueDate()->difference($source_created_on);
  416. $milestone->getDueDate()->advance($seconds);
  417. } else {
  418. $milestone->setDueDate($source_milestone->getDueDate());
  419. }
  420. $milestone->getDueDate()->advance($add_seconds);
  421. $milestone->setIsPrivate($source_milestone->getIsPrivate());
  422. $milestone->setAssignedToUserId($source_milestone->getAssignedToUserId());
  423. $milestone->setAssignedToCompanyId($source_milestone->getAssignedToCompanyId());
  424. $milestone->setProjectId($project_id);
  425. $milestone->save();
  426. $milestone_map[$source_milestone->getId()]=$milestone->getId();
  427. } // foreach
  428. } // if
  429. } // if
  430. // project tasks
  431. if ($copy_tasks) {
  432. $source_task_lists = $source->getAllTaskLists();
  433. if (is_array($source_task_lists)) {
  434. foreach ($source_task_lists as $source_task_list) {
  435. $task_list = new ProjectTaskList();
  436. //$task_list->copy($source_milestone);
  437. $task_list->setName($source_task_list->getName());
  438. $task_list->setPriority($source_task_list->getPriority());
  439. $task_list->setDescription($source_task_list->getDescription());
  440. if ($copy_milestones) {
  441. $task_list->setMilestoneId($milestone_map[$source_task_list->getMilestoneId()]);
  442. }
  443. $task_list->setDueDate($source_task_list->getDueDate());
  444. if ($task_list->getDueDate() instanceof DateTimeValue) {
  445. if ($shift_dates) {
  446. trace(__FILE__,"copy():task list shift dates");
  447. $task_list->setDueDate(DateTimeValueLib::now());
  448. $seconds = $source_task_list->getDueDate()->difference($source_created_on);
  449. $task_list->getDueDate()->advance($seconds);
  450. }
  451. $task_list->getDueDate()->advance($add_seconds);
  452. }
  453. $task_list->setIsPrivate($source_task_list->getIsPrivate());
  454. $task_list->setOrder($source_task_list->getOrder());
  455. $task_list->setProjectId($project_id);
  456. $task_list->save();
  457. $source_tasks = $source_task_list->getTasks();
  458. if (is_array($source_tasks)) {
  459. foreach($source_tasks as $source_task) {
  460. $task = new ProjectTask();
  461. $task->setOrder($source_task->getOrder());
  462. $task->setDueDate($source_task->getDueDate());
  463. if ($task->getDueDate() instanceof DateTimeValue) {
  464. if ($shift_dates) {
  465. trace(__FILE__,"copy():task shift dates");
  466. $task->setDueDate(DateTimeValueLib::now());
  467. $seconds = $source_task->getDueDate()->difference($source_created_on);
  468. $task->getDueDate()->advance($seconds);
  469. }
  470. $task->getDueDate()->advance($add_seconds);
  471. }
  472. $task->setText($source_task->getText());
  473. $task->getAssignedToUserId($source_task->getAssignedToUserId());
  474. $task->getAssignedToCompanyId($source_task->getAssignedToCompanyId());
  475. $task_list->attachTask($task);
  476. }
  477. }
  478. } // foreach
  479. } // if
  480. } // if
  481. // project messages
  482. if ($copy_messages) {
  483. $source_messages= $source->getAllMessages();
  484. if (is_array($source_messages)) {
  485. foreach ($source_messages as $source_message) {
  486. $message = new ProjectMessage();
  487. //$message->copy($source_message);
  488. $message->setTitle($source_message->getTitle());
  489. $message->setText($source_message->getText());
  490. $message->setAdditionalText($source_message->getAdditionalText());
  491. if ($copy_milestones) {
  492. $message->setMilestoneId($milestone_map[$source_message->getMilestoneId()]);
  493. }
  494. $message->setIsImportant($source_message->getIsImportant());
  495. $message->setIsPrivate($source_message->getIsPrivate());
  496. $message->setCommentsEnabled($source_message->getCommentsEnabled());
  497. $message->setAnonymousCommentsEnabled($source_message->getAnonymousCommentsEnabled());
  498. $message->setProjectId($project_id);
  499. $message->save();
  500. } // foreach
  501. } // if
  502. } // if
  503. // project links
  504. if ($copy_links) {
  505. $source_links = ProjectLinks::getAllProjectLinks($source);
  506. if (is_array($source_links)) {
  507. foreach ($source_links as $source_link) {
  508. $link = new ProjectLink();
  509. //$folder->copy($source_link);
  510. $link->setTitle($source_link->getTitle());
  511. $link->setUrl($source_link->getUrl());
  512. $link->setProjectId($project_id);
  513. $link->save();
  514. } // foreach
  515. } // if
  516. } // if
  517. // project folders & files
  518. if ($copy_files) {
  519. $folder_map = array( 0 => 0 );
  520. $source_folders = $source->getFolders();
  521. if (is_array($source_folders)) {
  522. foreach ($source_folders as $source_folder) {
  523. $folder = new ProjectFolder();
  524. //$folder->copy($source_folder);
  525. $folder->setName($source_folder->getName());
  526. $folder->setProjectId($project_id);
  527. $folder->save();
  528. $folder_map[$source_folder->getId()]=$folder->getId();
  529. } // foreach
  530. } // if
  531. $source_files = ProjectFiles::getAllFilesByProject($source);
  532. if (is_array($source_files)) {
  533. foreach ($source_files as $source_file) {
  534. $file = new ProjectFile();
  535. $file->setProjectId($project_id);
  536. $file->setFolderId($folder_map[$source_file->getFolderId()]);
  537. $file->setFileName($source_file->getFileName());
  538. $file->setDescription($source_file->getDescription());
  539. $file->setIsPrivate($source_file->getIsPrivate());
  540. $file->setIsImportant($source_file->getIsImportant());
  541. $file->setIsLocked($source_file->getIsLocked());
  542. $file->setIsVisible($source_file->getIsVisible());
  543. $file->setExpirationTime($source_file->getExpirationTime());
  544. $file->setCommentsEnabled($source_file->getCommentsEnabled());
  545. $file->setAnonymousCommentsEnabled($source_file->getAnonymousCommentsEnabled());
  546. $file->save();
  547. $source_revision = $source_file->getLastRevision();
  548. if ($source_revision instanceof ProjectFileRevision) {
  549. $revision = new ProjectFileRevision();
  550. $revision->setFileId($file->getId());
  551. $revision->setRevisionNumber($source_revision->getRevisionNumber());
  552. $revision->setRepositoryId($source_revision->getRepositoryId());
  553. $revision->setFilesize($source_revision->getFilesize());
  554. $revision->setFilename($source_revision->getFileName());
  555. $revision->setTypeString($source_revision->getTypeString());
  556. $revision->setThumbFilename($source_revision->getThumbFilename());
  557. $revision->setFileTypeId($source_revision->getFileTypeId());
  558. $revision->setComment($source_revision->getComment());
  559. $revision->save();
  560. }
  561. } // foreach
  562. } // if
  563. } // if
  564. if ($copy_pages) {
  565. $source_pages = Wiki::getAllProjectPages($source);
  566. if (is_array($source_pages)) {
  567. foreach ($source_pages as $source_page) {
  568. $page = new WikiPage();
  569. $page->setProjectId($project_id);
  570. $page->setProjectIndex($source_page->getProjectIndex());
  571. $page->setProjectSidebar($source_page->getProjectSidebar());
  572. if (plugin_active('tags')) {
  573. //$page->setTags($source_page->getTagNames());
  574. }
  575. //Make a new revision of this page
  576. $revision = $page->makeRevision();
  577. $source_revision = $source_page->getLatestRevision();
  578. //Set attributes
  579. $revision->setName($source_revision->getName());
  580. $revision->setContent($source_revision->getContent());
  581. $revision->setLogMessage($source_revision->getLogMessage());
  582. //Save the page
  583. $page->save();
  584. } // foreach
  585. } // if
  586. } // if
  587. if ($copy_users) {
  588. $source_companies = ProjectCompanies ::instance()->getCompaniesByProject($source);
  589. if (is_array($source_companies)) {
  590. foreach ($source_companies as $source_company) {
  591. $project_company = new ProjectCompany();
  592. $project_company->setCompanyId($source_company->getId());
  593. $project_company->setProjectId($project_id);
  594. $project_company->save();
  595. } // foreach
  596. }
  597. $source_users = ProjectUsers::instance()->getUsersByProject($source);
  598. if (is_array($source_users)) {
  599. foreach ($source_users as $source_user) {
  600. $project_user = new ProjectUser();
  601. $project_user->setUserId($source_user->getId());
  602. $project_user->setProjectId($project_id);
  603. $project_user->save();
  604. } // foreach
  605. }
  606. }
  607. /*
  608. $permissions = array_keys(PermissionManager::getPermissionsText());
  609. $auto_assign_users = owner_company()->getAutoAssignUsers();
  610. // We are getting the list of auto assign users. If current user is not in the list
  611. // add it. He's creating the project after all...
  612. if (is_array($auto_assign_users)) {
  613. $auto_assign_logged_user = false;
  614. foreach ($auto_assign_users as $user) {
  615. if ($user->getId() == logged_user()->getId()) {
  616. $auto_assign_logged_user = true;
  617. }
  618. } // if
  619. if (!$auto_assign_logged_user) {
  620. $auto_assign_users[] = logged_user();
  621. }
  622. } else {
  623. $auto_assign_users[] = logged_user();
  624. } // if
  625. foreach ($auto_assign_users as $user) {
  626. $project_user = new ProjectUser();
  627. $project_user->setProjectId($project->getId());
  628. $project_user->setUserId($user->getId());
  629. if (is_array($permissions)) {
  630. foreach ($permissions as $permission) {
  631. $user = Users::findById($project_user->getUserId());
  632. $user->setProjectPermission($project,$permission,true);
  633. }
  634. } // if
  635. $project_user->save();
  636. } // foreach
  637. */
  638. ApplicationLogs::createLog($project, null, ApplicationLogs::ACTION_ADD, false, true);
  639. DB::commit();
  640. flash_success(lang('success copy project', $source->getName(), $project->getName()));
  641. $this->redirectToUrl($project->getPermissionsUrl());
  642. } catch(Exception $e) {
  643. echo $e->getMessage();
  644. tpl_assign('error', $e);
  645. DB::rollback();
  646. } // try
  647. } // if (submitted)
  648. } // copy
  649. /**
  650. * Download project task lists
  651. *
  652. * @param void
  653. * @return null
  654. */
  655. function download_task_lists() {
  656. if (!logged_user()->isProjectUser(active_project())) {
  657. flash_error(lang('no access permissions'));
  658. $this->redirectTo('dashboard', 'index');
  659. } // if
  660. $project = active_project();
  661. $task_lists = $project->getTaskLists();
  662. $project_name = $project->getName();
  663. if (is_array($task_lists)) {
  664. $name = $project_name.'_tasks.txt';
  665. $content = '';
  666. $header = true;
  667. $count = 0;
  668. foreach ($task_lists as $task_list) {
  669. $content .= $task_list->getDownloadText($count, "\t", $header);
  670. $header = false;
  671. }
  672. //flash_success(lang('%s items downloaded', $count));
  673. download_contents($content, 'text/csv', $name, strlen($content));
  674. die();
  675. } else {
  676. flash_error(lang('nothing to download', $project_name));
  677. }
  678. $this->redirectTo('project', 'index');
  679. }
  680. /**
  681. * Edit project
  682. *
  683. * @param void
  684. * @return null
  685. */
  686. function edit() {
  687. $project = Projects::findById(get_id());
  688. if (!($project instanceof Project)) {
  689. flash_error(lang('project dnx'));
  690. $this->redirectTo('dashboard', 'index');
  691. } // if
  692. if (!$project->canEdit(logged_user())) {
  693. flash_error(lang('no access permissions'));
  694. $this->redirectToReferer(get_url('dashboard', 'index'));
  695. } // if
  696. if (plugin_active('files')) {
  697. $this->addHelper('files', 'files');
  698. }
  699. if (plugin_active('tickets')) {
  700. $this->addHelper('tickets', 'tickets');
  701. }
  702. // TODO find a more elegant solution for this parameter
  703. $page_name = 'project_overview';
  704. $this->setTemplate('add_project');
  705. $this->setLayout('project_website');
  706. $this->setSidebar(get_template_path('textile_help_sidebar'));
  707. $project_data = array_var($_POST, 'project');
  708. if (!is_array($project_data)) {
  709. $project_data = array(
  710. 'name' => $project->getName(),
  711. 'parent_id' => $project->getParentId(),
  712. 'priority' => $project->getPriority(),
  713. 'description' => $project->getDescription(),
  714. 'show_description_in_overview' => $project->getShowDescriptionInOverview()
  715. ); // array
  716. } // if
  717. $page_attachments = PageAttachments::getAttachmentsByPageNameAndProject($page_name, $project);
  718. $redirect_to = urldecode(array_var($_GET, 'redirect_to'));
  719. tpl_assign('project', $project);
  720. tpl_assign('project_data', $project_data);
  721. tpl_assign('page_attachments', $page_attachments);
  722. tpl_assign('redirect_to', $redirect_to);
  723. if (is_array(array_var($_POST, 'project'))) {
  724. $project->setFromAttributes($project_data);
  725. try {
  726. DB::beginWork();
  727. $project->save();
  728. ApplicationLogs::createLog($project, null, ApplicationLogs::ACTION_EDIT, false, true);
  729. $page_attachments = array_var($project_data, 'page_attachments');
  730. if (is_array($page_attachments)) {
  731. foreach ($page_attachments as $id => $page_attachment_data) {
  732. $page_attachment = PageAttachments::findById($id);
  733. if (array_var($page_attachment_data, 'delete') == "checked") {
  734. $page_attachment->delete();
  735. } else {
  736. $page_attachment->setFromAttributes($page_attachment_data);
  737. $page_attachment->save();
  738. } // if
  739. } // foreach
  740. PageAttachments::reorder($page_name, $project);
  741. } // if
  742. DB::commit();
  743. flash_success(lang('success edit project', $project->getName()));
  744. if ((trim($redirect_to)) == '' || !is_valid_url($redirect_to)) {
  745. $redirect_to = $project->getSettingsUrl();
  746. } // if
  747. $this->redirectToUrl($redirect_to);
  748. } catch(Exception $e) {
  749. DB::rollback();
  750. tpl_assign('error', $e);
  751. } // try
  752. } // if
  753. } // edit
  754. /**
  755. * Show and process edit project logo form
  756. *
  757. * @param void
  758. * @return null
  759. */
  760. function edit_logo() {
  761. $project = Projects::findById(get_id());
  762. if (!($project instanceof Project)) {
  763. flash_error(lang('project dnx'));
  764. $this->redirectToReferer(get_url('dashboard'));
  765. } // if
  766. if (!$project->canEdit(logged_user())) {
  767. flash_error(lang('no access permissions'));
  768. $this->redirectToReferer(get_url('dashboard'));
  769. } // if
  770. if (!function_exists('imagecreatefromjpeg')) {
  771. flash_error(lang('no image functions'));
  772. $this->redirectTo('dashboard');
  773. } // if
  774. $this->setTemplate('edit_logo');
  775. $this->setLayout('administration');
  776. tpl_assign('project', $project);
  777. $logo = array_var($_FILES, 'new_logo');
  778. if (is_array($logo)) {
  779. try {
  780. move_uploaded_file($logo["tmp_name"], ROOT . "/tmp/" . $logo["name"]);
  781. $logo["tmp_name"] = ROOT . "/tmp/" . $logo["name"];
  782. if (!isset($logo['name']) || !isset($logo['type']) || !isset($logo['size']) || !isset($logo['tmp_name']) || !is_readable($logo['tmp_name'])) {
  783. throw new InvalidUploadError($logo, lang('error upload file'));
  784. } // if
  785. $valid_types = array('image/jpg', 'image/jpeg', 'image/pjpeg', 'image/gif', 'image/png');
  786. $max_width = config_option('max_logo_width', 50);
  787. $max_height = config_option('max_logo_height', 50);
  788. if (!in_array($logo['type'], $valid_types) || !($image = getimagesize($logo['tmp_name']))) {
  789. throw new InvalidUploadError($logo, lang('invalid upload type', 'JPG, GIF, PNG'));
  790. } // if
  791. $old_file = $project->getLogoPath();
  792. DB::beginWork();
  793. if (!$project->setLogo($logo['tmp_name'], $max_width, $max_height, true)) {
  794. DB::rollback();
  795. flash_error(lang('error edit project logo', $e));
  796. $this->redirectToUrl($project->getEditLogoUrl());
  797. } // if
  798. ApplicationLogs::createLog($project, null, ApplicationLogs::ACTION_EDIT);
  799. flash_success(lang('success edit project logo'));
  800. DB::commit();
  801. if (is_file($old_file)) {
  802. @unlink($old_file);
  803. } // uf
  804. } catch(Exception $e) {
  805. flash_error(lang('error edit project logo', $e));
  806. DB::rollback();
  807. } // try
  808. $this->redirectToUrl($project->getEditLogoUrl());
  809. } // if
  810. } // edit_logo
  811. /**
  812. * Delete company logo
  813. *
  814. * @param void
  815. * @return null
  816. */
  817. function delete_logo() {
  818. if (!logged_user()->isAdministrator(owner_company())) {
  819. flash_error(lang('no access permissions'));
  820. $this->redirectTo('dashboard');
  821. } // if
  822. $project = Projects::findById(get_id());
  823. if (!($project instanceof Project)) {
  824. flash_error(lang('project dnx'));
  825. $this->redirectToReferer(get_url('dashboard', 'projects'));
  826. } // if
  827. try {
  828. DB::beginWork();
  829. $project->deleteLogo();
  830. $project->save();
  831. ApplicationLogs::createLog($project, null, ApplicationLogs::ACTION_EDIT);
  832. DB::commit();
  833. flash_success(lang('success delete project logo'));
  834. } catch(Exception $e) {
  835. DB::rollback();
  836. flash_error(lang('error delete project logo'));
  837. } // try
  838. $this->redirectToUrl($project->getEditLogoUrl());
  839. } // delete_logo
  840. /**
  841. * Delete project
  842. *
  843. * @param void
  844. * @return null
  845. */
  846. function delete() {
  847. $this->setTemplate('del_project');
  848. $this->setLayout('administration');
  849. $project = Projects::findById(get_id());
  850. if (!($project instanceof Project)) {
  851. flash_error(lang('project dnx'));
  852. $this->redirectTo('administration', 'projects');
  853. } // if
  854. if (!$project->canDelete(logged_user())) {
  855. flash_error(lang('no access permissions'));
  856. $this->redirectToReferer(get_url('administration', 'projects'));
  857. } // if
  858. $delete_data = array_var($_POST, 'deleteProject');
  859. tpl_assign('project', $project);
  860. tpl_assign('delete_data', $delete_data);
  861. if (!is_array($delete_data)) {
  862. $delete_data = array(
  863. 'really' => 0,
  864. 'password' => '',
  865. ); // array
  866. tpl_assign('delete_data', $delete_data);
  867. } else if ($delete_data['really'] == 1) {
  868. $password = $delete_data['password'];
  869. if (trim($password) == '') {
  870. tpl_assign('error', new Error(lang('password value missing')));
  871. $this->render();
  872. }
  873. if (!logged_user()->isValidPassword($password)) {
  874. tpl_assign('error', new Error(lang('invalid login data')));
  875. $this->render();
  876. } // if
  877. try {
  878. DB::beginWork();
  879. $project->delete();
  880. CompanyWebsite::instance()->setProject(null);
  881. ApplicationLogs::createLog($project, null, ApplicationLogs::ACTION_DELETE);
  882. DB::commit();
  883. flash_success(lang('success delete project', $project->getName()));
  884. } catch(Exception $e) {
  885. DB::rollback();
  886. flash_error(lang('error delete project'));
  887. } // try
  888. $this->redirectTo('administration', 'projects');
  889. } else {
  890. flash_error(lang('error delete project'));
  891. $this->redirectTo('administration', 'projects');
  892. }
  893. } // delete
  894. /**
  895. * Complete this project
  896. *
  897. * @param void
  898. * @return null
  899. */
  900. function complete() {
  901. $project = Projects::findById(get_id());
  902. if (!($project instanceof Project)) {
  903. flash_error(lang('project dnx'));
  904. $this->redirectTo('administration', 'projects');
  905. } // if
  906. if (!$project->canChangeStatus(logged_user())) {
  907. flash_error(lang('no access permissions'));
  908. $this->redirectToReferer(get_url('administration', 'projects'));
  909. } // if
  910. try {
  911. $project->setCompletedOn(DateTimeValueLib::now());
  912. $project->setCompletedById(logged_user()->getId());
  913. DB::beginWork();
  914. $project->save();
  915. ApplicationLogs::createLog($project, null, ApplicationLogs::ACTION_CLOSE);
  916. DB::commit();
  917. flash_success(lang('success complete project', $project->getName()));
  918. } catch(Exception $e) {
  919. DB::rollback();
  920. flash_error(lang('error complete project'));
  921. } // try
  922. $this->redirectToReferer(get_url('administration', 'projects'));
  923. } // complete
  924. /**
  925. * Reopen project
  926. *
  927. * @param void
  928. * @return null
  929. */
  930. function open() {
  931. $project = Projects::findById(get_id());
  932. if (!($project instanceof Project)) {
  933. flash_error(lang('project dnx'));
  934. $this->redirectTo('administration', 'projects');
  935. } // if
  936. if (!$project->canChangeStatus(logged_user())) {
  937. flash_error(lang('no access permissions'));
  938. $this->redirectToReferer(get_url('administration', 'projects'));
  939. } // if
  940. try {
  941. $project->setCompletedOn(null);
  942. $project->setCompletedById(0);
  943. DB::beginWork();
  944. $project->save();
  945. ApplicationLogs::createLog($project, null, ApplicationLogs::ACTION_OPEN);
  946. DB::commit();
  947. flash_success(lang('success open project', $project->getName()));
  948. } catch(Exception $e) {
  949. DB::rollback();
  950. flash_error(lang('error open project'));
  951. } // try
  952. $this->redirectToReferer(get_url('administration', 'projects'));
  953. } // open
  954. /**
  955. * Adds contact to project (as a PageAttachment)
  956. *
  957. * @param void
  958. * @return null
  959. */
  960. function add_contact() {
  961. if (!active_project()->canChangePermissions(logged_user())) {
  962. flash_error(lang('no access permissions'));
  963. $this->redirectToReferer(active_project()->getOverviewUrl());
  964. } // if
  965. $already_attached_contacts = PageAttachments::getAttachmentsByTypeAndProject(array('Contacts'), active_project());
  966. $already_attached_contacts_ids = null;
  967. if (is_array($already_attached_contacts)) {
  968. $already_attached_contacts_ids = array();
  969. foreach ($already_attached_contacts as $already_attached_contact) {
  970. $already_attached_contacts_ids[] = $already_attached_contact->getRelObjectId();
  971. } // foreach
  972. } // if
  973. $this->setTemplate('add_contact');
  974. $contact = new Contact();
  975. $im_types = ImTypes::findAll(array('order' => '`id`'));
  976. $contact_data = array_var($_POST, 'contact');
  977. if (!is_array($contact_data)) {
  978. $contact_data = array(); // array
  979. } // if
  980. $existing_contact_data = array_var($contact_data, 'existing');
  981. if (!is_array($existing_contact_data)) {
  982. $existing_contact_data = array(); // array
  983. } // if
  984. $new_contact_data = array_var($contact_data, 'new');
  985. if (!is_array($new_contact_data)) {
  986. $new_contact_data = array(); // array
  987. } // if
  988. $company_data = array_var($new_contact_data, 'company');
  989. if (!is_array($company_data)) {
  990. $company_data = array(); // array
  991. } // if
  992. $user_data = array_var($new_contact_data, 'user');
  993. if (!is_array($user_data)) {
  994. $user_data = array(); // array
  995. } // if
  996. $project_init = array_var($_GET, 'project_init');
  997. tpl_assign('already_attached_contacts_ids', $already_attached_contacts_ids);
  998. tpl_assign('contact', $contact);
  999. tpl_assign('contact_data', $contact_data);
  1000. tpl_assign('existing_contact_data', $existing_contact_data);
  1001. tpl_assign('new_contact_data', $new_contact_data);
  1002. tpl_assign('company_data', $company_data);
  1003. tpl_assign('user_data', $user_data);
  1004. tpl_assign('project_init', $project_init);
  1005. tpl_assign('im_types', $im_types);
  1006. tpl_assign('project', active_project());
  1007. if (is_array(array_var($_POST, 'contact'))) {
  1008. if (array_var($contact_data, 'what') == 'existing') {
  1009. if (!(Contacts::findById(array_var($existing_contact_data, 'rel_object_id')) instanceof Contact)) {
  1010. tpl_assign('error', new FormSubmissionErrors(array(lang('existing contact required'))));
  1011. } else {
  1012. $page_attachment = new PageAttachment();
  1013. $page_attachment->setFromAttributes($existing_contact_data);
  1014. $page_attachment->setRelObjectManager('Contacts');
  1015. $page_attachment->setProjectId(active_project()->getId());
  1016. $page_attachment->setPageName('people');
  1017. $page_attachment->save();
  1018. PageAttachments::reorder('people', active_project());
  1019. flash_success(lang('success add contact', $page_attachment->getObject()->getDisplayName()));
  1020. if ($project_init) {
  1021. $this->redirectToUrl(active_project()->getAddContactUrl(array('project_init' => '1')));
  1022. } else {
  1023. $this->redirectToUrl(get_url('project', 'people'));
  1024. } // if
  1025. } // if
  1026. } else {
  1027. // New contact
  1028. // Save avatar
  1029. $avatar = array_var($_FILES, 'new_avatar');
  1030. if (is_array($avatar) && isset($avatar['size']) && $avatar['size'] != 0) {
  1031. try {
  1032. if (!isset($avatar['name']) || !isset($avatar['type']) || !isset($avatar['size']) || !isset($avatar['tmp_name']) || !is_readable($avatar['tmp_name'])) {
  1033. throw new InvalidUploadError($avatar, lang('error upload file'));
  1034. } // if
  1035. $valid_types = array('image/jpg', 'image/jpeg', 'image/pjpeg', 'image/gif', 'image/png');
  1036. $max_width = config_option('max_avatar_width', 50);
  1037. $max_height = config_option('max_avatar_height', 50);
  1038. if ($avatar['size']) {
  1039. if (!in_array($avatar['type'], $valid_types) || !($image = getimagesize($avatar['tmp_name']))) {
  1040. throw new InvalidUploadError($avatar, lang('invalid upload type', 'JPG, GIF, PNG'));
  1041. } elseif (!$contact->setAvatar($avatar['tmp_name'], $max_width, $max_height, false)) {
  1042. throw new Error($avatar, lang('error edit avatar'));
  1043. $contact->setAvatarFile('');
  1044. } // if
  1045. } // if
  1046. } catch (Exception $e) {
  1047. flash_error($e->getMessage());
  1048. }
  1049. } else {
  1050. $contact->setAvatarFile('');
  1051. } // if
  1052. try {
  1053. DB::beginWork();
  1054. $contact->setFromAttributes($new_contact_data);
  1055. if (array_var($company_data, 'what') == 'existing') {
  1056. $company_id = $new_contact_data['company_id'];
  1057. } else {
  1058. $company = new Company();
  1059. $company->setName(array_var($company_data, 'name'));
  1060. $company->setTimezone(array_var($company_data, 'timezone'));
  1061. $company->setClientOfId(owner_company()->getId());
  1062. $company->save();
  1063. $company_id = $company->getId();
  1064. } // if
  1065. $contact->setCompanyId($company_id);
  1066. // User account info
  1067. if (array_var($user_data, 'add_account') == "yes") {
  1068. $user = new User();
  1069. $user->setFromAttributes($user_data);
  1070. if (array_var($user_data, 'password_generator') == 'random') {
  1071. // Generate random password
  1072. $password = substr(sha1(uniqid(rand(), true)), rand(0, 25), 13);
  1073. } else {
  1074. // Validate user input
  1075. $password = array_var($user_data, 'password');
  1076. if (trim($password) == '') {
  1077. throw new Error(lang('password value required'));
  1078. } // if
  1079. if ($password <> array_var($user_data, 'password_a')) {
  1080. throw new Error(lang('passwords dont match'));
  1081. } // if
  1082. } // if
  1083. $user->setPassword($password);
  1084. $user->save();
  1085. $contact->setUserId($user->getId());
  1086. } else {
  1087. $contact->setUserId(0);
  1088. } // if
  1089. $contact->save();
  1090. if (plugin_active('tags')) {
  1091. $contact->setTagsFromCSV(array_var($new_contact_data, 'tags'));
  1092. }
  1093. $contact->clearImValues();
  1094. foreach ($im_types as $im_type) {
  1095. $value = trim(array_var($new_contact_data, 'im_' . $im_type->getId()));
  1096. if ($value <> '') {
  1097. $contact_im_value = new ContactImValue();
  1098. $contact_im_value->setContactId($contact->getId());
  1099. $contact_im_value->setImTypeId($im_type->getId());
  1100. $contact_im_value->setValue($value);
  1101. $contact_im_value->setIsDefault(array_var($new_contact_data, 'default_im') == $im_type->getId());
  1102. $contact_im_value->save();
  1103. } // if
  1104. } // foreach
  1105. ApplicationLogs::createLog($contact, null, ApplicationLogs::ACTION_ADD);
  1106. $page_attachment = new PageAttachment();
  1107. $page_attachment->setFromAttributes($new_contact_data);
  1108. $page_attachment->setRelObjectId($contact->getId());
  1109. $page_attachment->setRelObjectManager('Contacts');
  1110. $page_attachment->setProjectId(active_project()->getId());
  1111. $page_attachment->setPageName('people');
  1112. $page_attachment->save();
  1113. PageAttachments::reorder('people', active_project());
  1114. DB::commit();
  1115. // Send notification...
  1116. try {
  1117. if (array_var($user_data, 'add_account') == "yes" && array_var($user_data, 'send_email_notification')) {
  1118. Notifier::newUserAccount($user, $password);
  1119. } // if
  1120. } catch(Exception $e) {
  1121. } // try
  1122. flash_success(lang('success add contact', $contact->getDisplayName()));
  1123. if ($project_init) {
  1124. $this->redirectToUrl(active_project()->getAddContactUrl(array('project_init' => '1')));
  1125. } else {
  1126. $this->redirectToUrl(get_url('project', 'people'));
  1127. } // if
  1128. } catch (Exception $e) {
  1129. DB::rollback();
  1130. tpl_assign('error', $e);
  1131. } // try
  1132. } // if
  1133. } // if
  1134. } // add_contact
  1135. /**
  1136. * Remove contact from project
  1137. *
  1138. * @param void
  1139. * @return null
  1140. */
  1141. function remove_contact() {
  1142. if (!active_project()->canChangePermissions(logged_user())) {
  1143. flash_error(lang('no access permissions'));
  1144. $this->redirectToReferer(active_project()->getOverviewUrl());
  1145. } // if
  1146. $rel_object_manager = array_var($_GET, 'rel_object_manager', 'Contacts');
  1147. $rel_object_id = array_var($_GET, 'rel_object_id');
  1148. $contact = Contacts::findById($rel_object_id);
  1149. if (!($contact instanceof Contact)) {
  1150. flash_error(lang('contact dnx'));
  1151. $this->redirectTo('project', 'people');
  1152. } // if
  1153. $project_id = array_var($_GET, 'project_id', active_project());
  1154. $project = Projects::findById(get_id('project_id'));
  1155. if (!($project instanceof Project)) {
  1156. flash_error(lang('project dnx'));
  1157. $this->redirectTo('project', 'people');
  1158. } // if
  1159. $page_attachments = PageAttachments::getAttachmentsByManagerIdAndProject($rel_object_manager, $rel_object_id, $project_id);
  1160. foreach ($page_attachments as $page_attachment) {
  1161. try {
  1162. $page_attachment->delete();
  1163. flash_success(lang('success remove contact from project'));
  1164. } catch (Exception $e) {
  1165. flash_error(lang('error remove contact from project'));
  1166. } // try
  1167. } // foreach
  1168. $this->redirectTo('project', 'people');
  1169. } // remove_contact
  1170. /**
  1171. * Remove user from project
  1172. *
  1173. * @param void
  1174. * @return null
  1175. */
  1176. function remove_user() {
  1177. if (!active_project()->canChangePermissions(logged_user())) {
  1178. flash_error(lang('no access permissions'));
  1179. $this->redirectToReferer(active_project()->getOverviewUrl());
  1180. } // if
  1181. $user = Users::findById(get_id('user_id'));
  1182. if (!($user instanceof User)) {
  1183. flash_error(lang('user dnx'));
  1184. $this->redirectTo('project', 'permissions');
  1185. } // if
  1186. if ($user->isAccountOwner()) {
  1187. flash_error(lang('user cant be removed from project'));
  1188. $this->redirectTo('project', 'permissions');
  1189. } // if
  1190. $project = Projects::findById(get_id('project_id'));
  1191. if (!($project instanceof Project)) {
  1192. flash_error(lang('project dnx'));
  1193. $this->redirectTo('project', 'permissions');
  1194. } // if
  1195. $project_user = ProjectUsers::findById(array('project_id' => $project->getId(), 'user_id' => $user->getId()));
  1196. if (!($project_user instanceof ProjectUser)) {
  1197. flash_error(lang('user not on project'));
  1198. $this->redirectTo('project', 'permissions');
  1199. } // if
  1200. try {
  1201. $project_user->delete();
  1202. flash_success(lang('success remove user from project'));
  1203. } catch(Exception $e) {
  1204. flash_error(lang('error remove user from project'));
  1205. } // try
  1206. $this->redirectTo('project', 'people');
  1207. } // remove_user
  1208. /**
  1209. * Remove company from project
  1210. *
  1211. * @param void
  1212. * @return null
  1213. */
  1214. function remove_company() {
  1215. if (!active_project()->canChangePermissions(logged_user())) {
  1216. flash_error(lang('no access permissions'));
  1217. $this->redirectToReferer(active_project()->getOverviewUrl());
  1218. } // if
  1219. $project = Projects::findById(get_id('project_id'));
  1220. if (!($project instanceof Project)) {
  1221. flash_error(lang('project dnx'));
  1222. $this->redirectTo('project', 'people');
  1223. } // if
  1224. $company = Companies::findById(get_id('company_id'));
  1225. if (!($company instanceof Company)) {
  1226. flash_error(lang('company dnx'));
  1227. $this->redirectTo('project', 'people');
  1228. } // if
  1229. $project_company = ProjectCompanies::findById(array('project_id' => $project->getId(), 'company_id' => $company->getId()));
  1230. if (!($project_company instanceof ProjectCompany)) {
  1231. flash_error(lang('company not on project'));
  1232. $this->redirectTo('project', 'people');
  1233. } // if
  1234. try {
  1235. DB::beginWork();
  1236. $project_company->delete();
  1237. $users = ProjectUsers::getCompanyUsersByProject($company, $project);
  1238. if (is_array($users)) {
  1239. foreach ($users as $user) {
  1240. $project_user = ProjectUsers::findById(array('project_id' => $project->getId(), 'user_id' => $user->getId()));
  1241. if ($project_user instanceof ProjectUser) {
  1242. $project_user->delete();
  1243. }
  1244. } // foreach
  1245. } // if
  1246. DB::commit();
  1247. flash_success(lang('success remove company from project'));
  1248. } catch(Exception $e) {
  1249. DB::rollback();
  1250. flash_error(lang('error remove company from project'));
  1251. } // try
  1252. $this->redirectTo('project', 'people');
  1253. } // remove_company
  1254. /**
  1255. * Show score card form
  1256. *
  1257. * @param void
  1258. * @return null
  1259. */
  1260. function score_card() {
  1261. $project = Projects::findById(get_id());
  1262. if (!($project instanceof Project)) {
  1263. flash_error(lang('project dnx'));
  1264. $this->redirectToReferer(get_url('dashboard'));
  1265. } // if
  1266. if (!$project->canEdit(logged_user())) {
  1267. flash_error(lang('no access permissions'));
  1268. $this->redirectToReferer(get_url('dashboard'));
  1269. } // if
  1270. $this->setTemplate('score_card');
  1271. $this->setLayout('project_website');
  1272. tpl_assign('project', $project);
  1273. } // edit_logo
  1274. /**
  1275. * Show project time
  1276. *
  1277. * @access public
  1278. * @param void
  1279. * @return null
  1280. */
  1281. function time() {
  1282. if (!logged_user()->isAdministrator(owner_company())) {
  1283. flash_error(lang('no access permissions'));
  1284. $this->redirectTo('dashboard');
  1285. } // if
  1286. $this->setLayout('dashboard');
  1287. tpl_assign('projects', owner_company()->getProjects());
  1288. } // time
  1289. } // ProjectController
  1290. ?>