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