File indexing completed on 2024-12-22 05:33:35
0001 <?php 0002 0003 /** 0004 * ocs-webserver 0005 * 0006 * Copyright 2016 by pling GmbH. 0007 * 0008 * This file is part of ocs-webserver. 0009 * 0010 * This program is free software: you can redistribute it and/or modify 0011 * it under the terms of the GNU Affero General Public License as 0012 * published by the Free Software Foundation, either version 3 of the 0013 * License, or (at your option) any later version. 0014 * 0015 * This program is distributed in the hope that it will be useful, 0016 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0018 * GNU Affero General Public License for more details. 0019 * 0020 * You should have received a copy of the GNU Affero General Public License 0021 * along with this program. If not, see <http://www.gnu.org/licenses/>. 0022 **/ 0023 class Default_Form_Collection extends Zend_Form 0024 { 0025 0026 protected $onlineGalleryImageSources = array(); 0027 private $member_id = null; 0028 0029 public function __construct($options = null) 0030 { 0031 if (isset($options['pictures'])) { 0032 $this->onlineGalleryImageSources = $options['pictures']; 0033 } 0034 if (isset($options['member_id'])) { 0035 $this->member_id = $options['member_id']; 0036 } 0037 0038 parent::__construct($options); 0039 } 0040 0041 /** 0042 * @param $sources array 0043 */ 0044 public function setOnlineGalleryImageSources($sources) 0045 { 0046 $this->onlineGalleryImageSources = $sources; 0047 $this->init(); 0048 } 0049 0050 public function init() 0051 { 0052 $this->setAction(''); 0053 $this->setAttrib('enctype', 'multipart/form-data'); 0054 $this->addPrefixPath('Default_Form_Element', APPLICATION_PATH . '/modules/default/forms/elements/', Zend_Form::ELEMENT); 0055 0056 $this->addElement($this->getTitleElement()) 0057 ->addElement($this->getCategoryIdElement()) 0058 ->addElement($this->getDescriptionElement()) 0059 // ->addElement($this->getUserCategoryElement()) 0060 // ->addElement($this->getVersionElement()) 0061 ->addElement($this->getSmallImageElement()) 0062 ->addElement($this->getImageUploadElement()) 0063 // ->addElement($this->getBigImageElement()) 0064 // ->addElement($this->getBigImageUploadElement()) 0065 // ->addSubForm($this->getGalleryElement(), 'gallery') 0066 // ->addElement($this->getAmountElement()) 0067 // ->addElement($this->getAmountPeriodElement()) 0068 // ->addElement($this->getEmbedCodeElement()) 0069 // ->addElement($this->getProjectHomepageElement()) 0070 // ->addElement($this->getSourceElement()) 0071 // ->addElement($this->getFacebookElement()) 0072 // ->addElement($this->getTwitterElement()) 0073 // ->addElement($this->getGoogleElement()) 0074 //->addElement($this->getTagElement()) 0075 ->addElement($this->getTagUserElement()) 0076 ->addElement($this->getHiddenProjectId()) 0077 ->addElement($this->getSubmitElement()) 0078 ->addElement($this->getCancelElement()) 0079 // ->addElement($this->getLicenseIdElement()) 0080 // ->addElement($this->getIsOriginal()) 0081 // ->addElement($this->getIsGitlab()) 0082 // ->addElement($this->getGitlabProjectId()) 0083 // ->addElement($this->getShowGitlabProjectIssues()) 0084 // ->addElement($this->getUseGitlabProjectReadme()) 0085 0086 //->addElement($this->getCCAttribution()) 0087 //->addElement($this->getCCComercial()) 0088 //->addElement($this->getCCDerivateWorks()) 0089 //->addElement($this->getCCShareAlike()) 0090 //->addElement($this->getCCLicense()) 0091 ; 0092 } 0093 0094 private function getTitleElement() 0095 { 0096 // $validatorRegEx = new Zend_Validate_Regex(array('pattern' => "/^[ \/\[\]\.\-_A-Za-z0-9'\pL]{1,}$/iu")); // with unicode character class 0097 $validatorRegEx = new Zend_Validate_Regex(array('pattern' => "/^[ \/\[\]\.\-_A-Za-z0-9']{1,}$/iu")); 0098 $validatorRegEx->setMessages(array(Zend_Validate_Regex::NOT_MATCH => "'%value%' is not valid. Please use only alphanumeric characters or /, [, ], -, _, ' ")); 0099 0100 return $this->createElement('text', 'title') 0101 ->setRequired(true) 0102 ->addValidators(array( 0103 array('StringLength', true, array(4, 60)), 0104 $validatorRegEx 0105 )) 0106 ->setFilters(array('StringTrim')) 0107 ->setDecorators(array( 0108 array( 0109 'ViewScript', 0110 array( 0111 'viewScript' => 'collection/viewscripts/input_title.phtml', 0112 'placement' => false 0113 ) 0114 ) 0115 )) 0116 ; 0117 } 0118 0119 private function getCategoryIdElement() 0120 { 0121 0122 $validatorCategory = new Default_Form_Validator_Category(); 0123 0124 return $this->createElement('number', 'project_category_id', array()) 0125 ->setRequired(true) 0126 ->addValidator('Digits') 0127 ->addValidator($validatorCategory) 0128 ->addFilter('Digits') 0129 ->setDecorators(array( 0130 array( 0131 'ViewScript', 0132 array( 0133 'viewScript' => 'collection/viewscripts/input_cat_id.phtml', 0134 'placement' => false 0135 ) 0136 ) 0137 )) 0138 ; 0139 } 0140 0141 private function getDescriptionElement() 0142 { 0143 return $this->createElement('textarea', 'description', array('cols' => 30, 'rows' => 9)) 0144 ->setRequired(true) 0145 ->setFilters(array('StringTrim')) 0146 ->setDecorators(array( 0147 array( 0148 'ViewScript', 0149 array( 0150 'viewScript' => 'collection/viewscripts/input_description.phtml', 0151 'placement' => false 0152 ) 0153 ) 0154 )) 0155 ; 0156 } 0157 0158 private function getUserCategoryElement() 0159 { 0160 return $this->createElement('textarea', 'user_category', array('cols' => 30, 'rows' => 9)) 0161 ->setRequired(false) 0162 ->setFilters(array('StringTrim')) 0163 ->setDecorators(array( 0164 array( 0165 'ViewScript', 0166 array( 0167 'viewScript' => 'collection/viewscripts/input_user_category.phtml', 0168 'placement' => false 0169 ) 0170 ) 0171 )) 0172 ; 0173 } 0174 0175 private function getVersionElement() 0176 { 0177 return $this->createElement('text', 'version') 0178 ->setRequired(false) 0179 ->addValidators(array( 0180 array('StringLength', false, array(0, 50)), 0181 )) 0182 ->setFilters(array('StringTrim')) 0183 ->setDecorators(array( 0184 array( 0185 'ViewScript', 0186 array( 0187 'viewScript' => 'collection/viewscripts/input_version.phtml', 0188 'placement' => false 0189 ) 0190 ) 0191 )) 0192 ; 0193 } 0194 0195 private function getSmallImageElement() 0196 { 0197 return $this->createElement('hidden', 'image_small') 0198 ->setFilters(array('StringTrim')) 0199 ->addValidators(array( 0200 array( 0201 'Regex', 0202 false, 0203 array('/^[A-Za-z0-9.\/_-]{1,}$/i') 0204 ) 0205 )) 0206 ->setDecorators(array( 0207 array( 0208 'ViewScript', 0209 array( 0210 'viewScript' => 'collection/viewscripts/input_image_small.phtml', 0211 'placement' => false 0212 ) 0213 ) 0214 )) 0215 ; 0216 } 0217 0218 private function getImageUploadElement() 0219 { 0220 $modelImage = new Default_Model_DbTable_Image(); 0221 0222 return $this->createElement('file', 'image_small_upload') 0223 ->setDisableLoadDefaultDecorators(true) 0224 ->setValueDisabled(true) 0225 ->setTransferAdapter(new Local_File_Transfer_Adapter_Http()) 0226 ->setRequired(false) 0227 ->setMaxFileSize(2097152) 0228 ->addValidator('Count', false, 1) 0229 ->addValidator('Size', false, array('min' => '500B', 'max' => '2MB')) 0230 ->addValidator('Extension', false, $modelImage->getAllowedFileExtension()) 0231 ->addValidator('ImageSize', false, array( 0232 'minwidth' => 20, 0233 'maxwidth' => 2000, 0234 'minheight' => 20, 0235 'maxheight' => 2000 0236 )) 0237 ->addValidator('MimeType', false, $modelImage->getAllowedMimeTypes()) 0238 ->setDecorators(array( 0239 array('File' => new Local_Form_Decorator_File()), 0240 array( 0241 'ViewScript', 0242 array( 0243 'viewScript' => 'collection/viewscripts/input_image_small_upload.phtml', 0244 'placement' => false 0245 ) 0246 ) 0247 0248 )) 0249 ; 0250 } 0251 0252 private function getGalleryElement() 0253 { 0254 $gallerySubform = new Default_Form_GallerySubForm(array('pictures' => $this->onlineGalleryImageSources)); 0255 $gallerySubform->setMaxGalleryPics(5); 0256 0257 return $gallerySubform; 0258 } 0259 0260 private function getEmbedCodeElement() 0261 { 0262 return $this->createElement('textarea', 'embed_code', array('cols' => 30, 'rows' => 3)) 0263 ->setRequired(false) 0264 ->setFilters(array('StringTrim')) 0265 ->setDecorators(array( 0266 array( 0267 'ViewScript', 0268 array( 0269 'viewScript' => 'collection/viewscripts/input_embedcode.phtml', 0270 'placement' => false 0271 ) 0272 ) 0273 )) 0274 ; 0275 } 0276 0277 private function getProjectHomepageElement() 0278 { 0279 return $this->createElement('text', 'link_1', array()) 0280 ->setRequired(false) 0281 ->setFilters(array('StringTrim')) 0282 ->addPrefixPath('Local_Validate', 'Local/Validate', Zend_Form_Element::VALIDATE) 0283 ->addValidator('PartialUrl') 0284 ->setDecorators(array( 0285 array( 0286 'ViewScript', 0287 array( 0288 'viewScript' => 'collection/viewscripts/input_link.phtml', 0289 'placement' => false 0290 ) 0291 ) 0292 )) 0293 ; 0294 } 0295 0296 private function getSourceElement() 0297 { 0298 return $this->createElement('text', 'source_url', array()) 0299 ->setRequired(false) 0300 ->setFilters(array('StringTrim')) 0301 ->addPrefixPath('Local_Validate', 'Local/Validate', Zend_Form_Element::VALIDATE) 0302 ->addValidator('PartialUrl') 0303 ->setDecorators(array( 0304 array( 0305 'ViewScript', 0306 array( 0307 'viewScript' => 'collection/viewscripts/input_source_url.phtml', 0308 'placement' => false 0309 ) 0310 ) 0311 )) 0312 ; 0313 } 0314 0315 private function getFacebookElement() 0316 { 0317 return $this->createElement('text', 'facebook_code', array()) 0318 ->setRequired(false) 0319 ->setFilters(array('StringTrim')) 0320 ->addPrefixPath('Local_Validate', 'Local/Validate', Zend_Form_Element::VALIDATE) 0321 ->addValidator('PartialUrl') 0322 ->setDecorators(array( 0323 array( 0324 'ViewScript', 0325 array( 0326 'viewScript' => 'collection/viewscripts/input_facebook.phtml', 0327 'placement' => false 0328 ) 0329 ) 0330 )) 0331 ; 0332 } 0333 0334 private function getTwitterElement() 0335 { 0336 return $this->createElement('text', 'twitter_code', array()) 0337 ->setRequired(false) 0338 ->setFilters(array('StringTrim')) 0339 ->addPrefixPath('Local_Validate', 'Local/Validate', Zend_Form_Element::VALIDATE) 0340 ->addValidator('PartialUrl') 0341 ->setDecorators(array( 0342 array( 0343 'ViewScript', 0344 array( 0345 'viewScript' => 'collection/viewscripts/input_twitter.phtml', 0346 'placement' => false 0347 ) 0348 ) 0349 )) 0350 ; 0351 } 0352 0353 private function getGoogleElement() 0354 { 0355 return $this->createElement('text', 'google_code', array()) 0356 ->setRequired(false) 0357 ->setFilters(array('StringTrim')) 0358 ->addPrefixPath('Local_Validate', 'Local/Validate', Zend_Form_Element::VALIDATE) 0359 ->addValidator('PartialUrl') 0360 ->setDecorators(array( 0361 array( 0362 'ViewScript', 0363 array( 0364 'viewScript' => 'collection/viewscripts/input_google.phtml', 0365 'placement' => false 0366 ) 0367 ) 0368 )) 0369 ; 0370 } 0371 0372 private function getTagElement() 0373 { 0374 $element = new Zend_Form_Element_Multiselect('tags', array('registerInArrayValidator' => false)); 0375 0376 return $element->setFilters(array('StringTrim'))->setDecorators(array( 0377 array( 0378 'ViewScript', 0379 array( 0380 'viewScript' => 'collection/viewscripts/input_tags_multiselect.phtml', 0381 'placement' => false 0382 ) 0383 ) 0384 )) 0385 ; 0386 } 0387 0388 private function getTagUserElement() 0389 { 0390 $element = new Zend_Form_Element_Multiselect('tagsuser', array('registerInArrayValidator' => false)); 0391 0392 return $element->setFilters(array('StringTrim'))->setDecorators(array( 0393 array( 0394 'ViewScript', 0395 array( 0396 'viewScript' => 'collection/viewscripts/input_tags_user.phtml', 0397 'placement' => false 0398 ) 0399 ) 0400 )) 0401 ; 0402 } 0403 0404 private function getHiddenProjectId() 0405 { 0406 return $this->createElement('hidden', 'project_id')->setFilters(array('StringTrim'))->addValidators(array('Digits')) 0407 ->setDecorators(array( 0408 array( 0409 'ViewScript', 0410 array( 0411 'viewScript' => 'collection/viewscripts/input_hidden.phtml', 0412 'placement' => false 0413 ) 0414 ) 0415 )) 0416 ; 0417 } 0418 0419 private function getSubmitElement() 0420 { 0421 $submit = $this->createElement('button', 'preview')->setDecorators(array( 0422 'ViewHelper' 0423 )) 0424 ; 0425 $submit->setLabel('Preview'); 0426 $submit->setAttrib('type', 'submit'); 0427 0428 return $submit; 0429 } 0430 0431 private function getCancelElement() 0432 { 0433 $cancel = $this->createElement('button', 'cancel')->setDecorators(array( 0434 'ViewHelper' 0435 )) 0436 ; 0437 $cancel->setLabel('Cancel'); 0438 $cancel->setAttrib('type', 'submit'); 0439 0440 return $cancel; 0441 } 0442 0443 private function getLicenseIdElement() 0444 { 0445 0446 //$element = new Zend_Form_Element_Multiselect('project_license_id', array('registerInArrayValidator' => false)); 0447 $element = new Zend_Form_Element_Select('license_tag_id', array('multiple' => false)); 0448 $element->setIsArray(true); 0449 0450 $tagTable = new Default_Model_DbTable_Tags(); 0451 $options = $tagTable->fetchLicenseTagsForSelect(); 0452 0453 return $element->setFilters(array('StringTrim'))->setMultiOptions($options)->setDecorators(array( 0454 array( 0455 'ViewScript', 0456 array( 0457 'viewScript' => 'collection/viewscripts/input_select_license.phtml', 0458 'placement' => false 0459 ) 0460 ) 0461 )) 0462 ; 0463 } 0464 0465 private function getIsOriginal() 0466 { 0467 $element = new Zend_Form_Element_Checkbox('is_original'); 0468 0469 return $element->setOptions(array( 0470 'label' => ' Product original ', 0471 'use_hidden_element' => false, 0472 'checked_value' => 1, 0473 'unchecked_value' => 0 0474 )); 0475 } 0476 0477 private function getIsGitlab() 0478 { 0479 $element = new Zend_Form_Element_Checkbox('is_gitlab_project'); 0480 0481 return $element->setOptions(array( 0482 'label' => ' Git-Project ', 0483 'use_hidden_element' => false, 0484 'checked_value' => 1, 0485 'unchecked_value' => 0 0486 )); 0487 } 0488 0489 private function getGitlabProjectId() 0490 { 0491 $element = new Zend_Form_Element_Select('gitlab_project_id', array('multiple' => false)); 0492 $element->setIsArray(true); 0493 0494 $gitlab = new Default_Model_Ocs_Gitlab(); 0495 0496 $optionArray = array(); 0497 0498 if ($this->member_id) { 0499 0500 $memberTable = new Default_Model_Member(); 0501 $member = $memberTable->fetchMemberData($this->member_id); 0502 $gitlab_user_id = null; 0503 if(!empty($member->gitlab_user_id)) { 0504 //get gitlab user id from db 0505 $gitlab_user_id = $member->gitlab_user_id; 0506 } else { 0507 //get gitlab user id from gitlab API and save in DB 0508 $gitUser = $gitlab->getUserWithName($member->username); 0509 0510 if ($gitUser && null != $gitUser) { 0511 $gitlab_user_id = $gitUser['id']; 0512 $memberTableExternal = new Default_Model_DbTable_MemberExternalId(); 0513 $memberTableExternal->updateGitlabUserId($this->member_id, $gitlab_user_id); 0514 } 0515 } 0516 0517 if ($gitlab_user_id && null != $gitlab_user_id) { 0518 try { 0519 //now get his projects 0520 $gitProjects = $gitlab->getUserProjects($gitlab_user_id); 0521 0522 foreach ($gitProjects as $proj) { 0523 $optionArray[$proj['id']] = $proj['name']; 0524 } 0525 } catch (Exception $exc) { 0526 //Error getting USerProjects, 0527 } 0528 0529 } 0530 } 0531 0532 return $element->setFilters(array('StringTrim'))->setMultiOptions($optionArray)->setDecorators(array( 0533 array( 0534 'ViewScript', 0535 array( 0536 'viewScript' => 'collection/viewscripts/input_gitlab_project_id.phtml', 0537 'placement' => false 0538 ) 0539 ) 0540 )) 0541 ; 0542 } 0543 0544 private function getShowGitlabProjectIssues() 0545 { 0546 $element = new Zend_Form_Element_Checkbox('show_gitlab_project_issues'); 0547 0548 return $element->setOptions(array( 0549 'label' => ' Git-Issues ', 0550 'use_hidden_element' => false, 0551 'checked_value' => 1, 0552 'unchecked_value' => 0 0553 )); 0554 } 0555 0556 private function getUseGitlabProjectReadme() 0557 { 0558 $element = new Zend_Form_Element_Checkbox('use_gitlab_project_readme'); 0559 0560 return $element->setOptions(array( 0561 'label' => 'README.md', 0562 'use_hidden_element' => false, 0563 'checked_value' => 1, 0564 'unchecked_value' => 0 0565 )); 0566 } 0567 0568 public function initSubCatElement($projectCatId) 0569 { 0570 if (false === isset($projectCatId)) { 0571 return; 0572 } 0573 $tableSubCategory = new Default_Model_SubCategory(); 0574 $subcategories = $tableSubCategory->fetchAllSubCategories($projectCatId); 0575 $this->getElement('project_subcategory_id')->setMultiOptions($subcategories); 0576 } 0577 0578 public function initSubSubCatElement($projectSubCatId) 0579 { 0580 if (false === isset($projectCatId)) { 0581 return; 0582 } 0583 $modelSubSubCategories = new Default_Model_SubSubCategory(); 0584 $subSubCategories = $modelSubSubCategories->fetchAllSubCategories($projectSubCatId); 0585 $this->getElement('project_subcategory_id')->setMultiOptions($subSubCategories); 0586 } 0587 0588 /** 0589 * Validate the form 0590 * 0591 * @param array $data 0592 * @param null $project_id 0593 * 0594 * @return bool 0595 * @throws Zend_Exception 0596 * @throws Zend_Form_Exception 0597 */ 0598 public function isValid($data, $project_id = null) 0599 { 0600 $sqlExclude = 'status > ' . Default_Model_DbTable_Project::PROJECT_DELETED; 0601 if (isset($project_id)) { 0602 $db = Zend_Registry::get('db'); 0603 $sqlExclude .= $db->quoteInto(' AND project_id <> ?', $project_id, 'INTEGER'); 0604 } 0605 0606 /* 0607 $checkTitleExist = new Zend_Validate_Db_NoRecordExists(array( 0608 'table' => 'project', 0609 'field' => 'title', 0610 'exclude' => $sqlExclude 0611 )); 0612 $checkTitleExist->setMessage('This title already exists.', Zend_Validate_Db_NoRecordExists::ERROR_RECORD_FOUND); 0613 0614 $this->getElement('title') 0615 ->addValidator($checkTitleExist); 0616 */ 0617 0618 return parent::isValid($data); 0619 } 0620 0621 /** 0622 * Validate a partial form 0623 * 0624 * Does not check for required flags. 0625 * 0626 * @param array $data 0627 * 0628 * @return boolean 0629 */ 0630 public function isValidPartial(array $data) 0631 { 0632 return parent::isValidPartial($data); // TODO: Change the autogenerated stub 0633 } 0634 0635 private function getAmountElement() 0636 { 0637 return $this->createElement('number', 'amount', array()) 0638 ->setRequired(false) 0639 ->addValidator('Digits') 0640 ->addFilter('Digits') 0641 ->setDecorators(array( 0642 array( 0643 'ViewScript', 0644 array( 0645 'viewScript' => 'collection/viewscripts/input_amount.phtml', 0646 'placement' => false 0647 ) 0648 ) 0649 )) 0650 ; 0651 } 0652 0653 private function getAmountPeriodElement() 0654 { 0655 return $this->createElement('radio', 'amount_period', array()) 0656 ->setRequired(false) 0657 ->addMultiOptions(array('yearly' => 'yearly (will run continuously each year)', 'one-time' => 'one-time')) 0658 ->setValue('yearly') 0659 ->setSeparator(' ') 0660 ->setDecorators(array( 0661 array( 0662 'ViewScript', 0663 array( 0664 'viewScript' => 'collection/viewscripts/input_amount_period.phtml', 0665 'placement' => false 0666 ) 0667 ) 0668 )) 0669 ; 0670 } 0671 0672 private function getBigImageElement() 0673 { 0674 return $this->createElement('hidden', 'image_big') 0675 ->setFilters(array('StringTrim')) 0676 ->addValidators(array( 0677 array( 0678 'Regex', 0679 false, 0680 array('/^[A-Za-z0-9.\/_-]{1,}$/i') 0681 ) 0682 )) 0683 ->setDecorators(array( 0684 array( 0685 'ViewScript', 0686 array( 0687 'viewScript' => 'collection/viewscripts/input_image_big.phtml', 0688 'placement' => false 0689 ) 0690 ) 0691 )) 0692 ; 0693 } 0694 0695 private function getBigImageUploadElement() 0696 { 0697 $modelImage = new Default_Model_DbTable_Image(); 0698 0699 return $this->createElement('file', 'image_big_upload')->setDisableLoadDefaultDecorators(true) 0700 ->setTransferAdapter(new Local_File_Transfer_Adapter_Http())->setRequired(false)->setMaxFileSize(2097152) 0701 ->addValidator('Count', false, 1)->addValidator('Size', false, 2097152)->addValidator('FilesSize', false, 2000000) 0702 ->addValidator('Extension', false, $modelImage->getAllowedFileExtension())->addValidator('ImageSize', false, array( 0703 'minwidth' => 100, 0704 'maxwidth' => 2000, 0705 'minheight' => 100, 0706 'maxheight' => 1200 0707 ))->addValidator('MimeType', false, $modelImage->getAllowedMimeTypes())->setDecorators(array( 0708 array('File' => new Local_Form_Decorator_File()), 0709 array( 0710 'ViewScript', 0711 array( 0712 'viewScript' => 'collection/viewscripts/input_image_big_upload.phtml', 0713 'placement' => false 0714 ) 0715 ) 0716 0717 )) 0718 ; 0719 } 0720 0721 private function getCCAttribution() 0722 { 0723 return $this->createElement('checkbox', 'by'); 0724 } 0725 0726 private function getCCComercial() 0727 { 0728 return $this->createElement('checkbox', 'nc'); 0729 } 0730 0731 private function getCCDerivateWorks() 0732 { 0733 return $this->createElement('checkbox', 'nd'); 0734 } 0735 0736 private function getCCShareAlike() 0737 { 0738 return $this->createElement('checkbox', 'sa'); 0739 } 0740 0741 private function getCCLicense() 0742 { 0743 return $this->createElement('checkbox', 'cc_license'); 0744 } 0745 0746 }