Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like documentController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use documentController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | class documentController extends document |
||
12 | { |
||
13 | /** |
||
14 | * Initialization |
||
15 | * @return void |
||
16 | */ |
||
17 | function init() |
||
20 | |||
21 | /** |
||
22 | * Action to handle vote-up of the post (Up) |
||
23 | * @return Object |
||
24 | */ |
||
25 | View Code Duplication | function procDocumentVoteUp() |
|
|
|||
26 | { |
||
27 | if(!Context::get('is_logged')) return new Object(-1, 'msg_invalid_request'); |
||
28 | |||
29 | $document_srl = Context::get('target_srl'); |
||
30 | if(!$document_srl) return new Object(-1, 'msg_invalid_request'); |
||
31 | |||
32 | $oDocumentModel = getModel('document'); |
||
33 | $oDocument = $oDocumentModel->getDocument($document_srl, false, false); |
||
34 | $module_srl = $oDocument->get('module_srl'); |
||
35 | if(!$module_srl) return new Object(-1, 'msg_invalid_request'); |
||
36 | |||
37 | $oModuleModel = getModel('module'); |
||
38 | $document_config = $oModuleModel->getModulePartConfig('document',$module_srl); |
||
39 | if($document_config->use_vote_up=='N') return new Object(-1, 'msg_invalid_request'); |
||
40 | |||
41 | $point = 1; |
||
42 | $output = $this->updateVotedCount($document_srl, $point); |
||
43 | $this->add('voted_count', $output->get('voted_count')); |
||
44 | return $output; |
||
45 | } |
||
46 | |||
47 | /** |
||
48 | * insert alias |
||
49 | * @param int $module_srl |
||
50 | * @param int $document_srl |
||
51 | * @param string $alias_title |
||
52 | * @return object |
||
53 | */ |
||
54 | function insertAlias($module_srl, $document_srl, $alias_title) |
||
55 | { |
||
56 | $args = new stdClass; |
||
57 | $args->alias_srl = getNextSequence(); |
||
58 | $args->module_srl = $module_srl; |
||
59 | $args->document_srl = $document_srl; |
||
60 | $args->alias_title = urldecode($alias_title); |
||
61 | $query = "document.insertAlias"; |
||
62 | $output = executeQuery($query, $args); |
||
63 | return $output; |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * Action to handle vote-up of the post (Down) |
||
68 | * @return Object |
||
69 | */ |
||
70 | View Code Duplication | function procDocumentVoteDown() |
|
71 | { |
||
72 | if(!Context::get('is_logged')) return new Object(-1, 'msg_invalid_request'); |
||
73 | |||
74 | $document_srl = Context::get('target_srl'); |
||
75 | if(!$document_srl) return new Object(-1, 'msg_invalid_request'); |
||
76 | |||
77 | $oDocumentModel = getModel('document'); |
||
78 | $oDocument = $oDocumentModel->getDocument($document_srl, false, false); |
||
79 | $module_srl = $oDocument->get('module_srl'); |
||
80 | if(!$module_srl) return new Object(-1, 'msg_invalid_request'); |
||
81 | |||
82 | $oModuleModel = getModel('module'); |
||
83 | $document_config = $oModuleModel->getModulePartConfig('document',$module_srl); |
||
84 | if($document_config->use_vote_down=='N') return new Object(-1, 'msg_invalid_request'); |
||
85 | |||
86 | $point = -1; |
||
87 | $output = $this->updateVotedCount($document_srl, $point); |
||
88 | $this->add('blamed_count', $output->get('blamed_count')); |
||
89 | return $output; |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * Action called when the post is reported by other member |
||
94 | * @return void|Object |
||
95 | */ |
||
96 | View Code Duplication | function procDocumentDeclare() |
|
97 | { |
||
98 | if(!Context::get('is_logged')) return new Object(-1, 'msg_invalid_request'); |
||
99 | |||
100 | $document_srl = Context::get('target_srl'); |
||
101 | if(!$document_srl) return new Object(-1, 'msg_invalid_request'); |
||
102 | |||
103 | return $this->declaredDocument($document_srl); |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * Delete alias when module deleted |
||
108 | * @param int $module_srl |
||
109 | * @return void |
||
110 | */ |
||
111 | function deleteDocumentAliasByModule($module_srl) |
||
112 | { |
||
113 | $args = new stdClass(); |
||
114 | $args->module_srl = $module_srl; |
||
115 | executeQuery("document.deleteAlias", $args); |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * Delete alias when document deleted |
||
120 | * @param int $document_srl |
||
121 | * @return void |
||
122 | */ |
||
123 | function deleteDocumentAliasByDocument($document_srl) |
||
124 | { |
||
125 | $args = new stdClass(); |
||
126 | $args->document_srl = $document_srl; |
||
127 | executeQuery("document.deleteAlias", $args); |
||
128 | } |
||
129 | |||
130 | /** |
||
131 | * Delete document history |
||
132 | * @param int $history_srl |
||
133 | * @param int $document_srl |
||
134 | * @param int $module_srl |
||
135 | * @return void |
||
136 | */ |
||
137 | function deleteDocumentHistory($history_srl, $document_srl, $module_srl) |
||
146 | |||
147 | /** |
||
148 | * A trigger to delete all posts together when the module is deleted |
||
149 | * @param object $obj |
||
150 | * @return Object |
||
151 | */ |
||
152 | function triggerDeleteModuleDocuments(&$obj) |
||
153 | { |
||
154 | $module_srl = $obj->module_srl; |
||
155 | if(!$module_srl) return new Object(); |
||
156 | // Delete the document |
||
157 | $oDocumentAdminController = getAdminController('document'); |
||
158 | $output = $oDocumentAdminController->deleteModuleDocument($module_srl); |
||
159 | if(!$output->toBool()) return $output; |
||
160 | // Delete the category |
||
161 | $oDocumentController = getController('document'); |
||
162 | $output = $oDocumentController->deleteModuleCategory($module_srl); |
||
163 | if(!$output->toBool()) return $output; |
||
164 | // Delete extra key and variable, because module deleted |
||
165 | $this->deleteDocumentExtraKeys($module_srl); |
||
166 | |||
167 | // remove aliases |
||
168 | $this->deleteDocumentAliasByModule($module_srl); |
||
169 | |||
170 | // remove histories |
||
171 | $this->deleteDocumentHistory(null, null, $module_srl); |
||
172 | |||
173 | return new Object(); |
||
174 | } |
||
175 | |||
176 | /** |
||
177 | * Grant a permisstion of the document |
||
178 | * Available in the current connection with session value |
||
179 | * @param int $document_srl |
||
180 | * @return void |
||
181 | */ |
||
182 | function addGrant($document_srl) |
||
186 | |||
187 | /** |
||
188 | * Insert the document |
||
189 | * @param object $obj |
||
190 | * @param bool $manual_inserted |
||
191 | * @param bool $isRestore |
||
192 | * @return object |
||
193 | */ |
||
194 | function insertDocument($obj, $manual_inserted = false, $isRestore = false, $isLatest = true) |
||
353 | |||
354 | /** |
||
355 | * Update the document |
||
356 | * @param object $source_obj |
||
357 | * @param object $obj |
||
358 | * @param bool $manual_updated |
||
359 | * @return object |
||
360 | */ |
||
361 | function updateDocument($source_obj, $obj, $manual_updated = FALSE) |
||
590 | |||
591 | /** |
||
592 | * Deleting Documents |
||
593 | * @param int $document_srl |
||
594 | * @param bool $is_admin |
||
595 | * @param bool $isEmptyTrash |
||
596 | * @param documentItem $oDocument |
||
597 | * @return object |
||
598 | */ |
||
599 | function deleteDocument($document_srl, $is_admin = false, $isEmptyTrash = false, $oDocument = null) |
||
600 | { |
||
601 | // Call a trigger (before) |
||
602 | $trigger_obj = new stdClass(); |
||
603 | $trigger_obj->document_srl = $document_srl; |
||
604 | $output = ModuleHandler::triggerCall('document.deleteDocument', 'before', $trigger_obj); |
||
605 | if(!$output->toBool()) return $output; |
||
606 | |||
607 | // begin transaction |
||
608 | $oDB = &DB::getInstance(); |
||
609 | $oDB->begin(); |
||
610 | |||
611 | if(!$isEmptyTrash) |
||
612 | { |
||
613 | // get model object of the document |
||
614 | $oDocumentModel = getModel('document'); |
||
615 | // Check if the documnet exists |
||
616 | $oDocument = $oDocumentModel->getDocument($document_srl, $is_admin); |
||
617 | } |
||
618 | else if($isEmptyTrash && $oDocument == null) return new Object(-1, 'document is not exists'); |
||
619 | |||
620 | if(!$oDocument->isExists() || $oDocument->document_srl != $document_srl) return new Object(-1, 'msg_invalid_document'); |
||
621 | // Check if a permossion is granted |
||
622 | if(!$oDocument->isGranted()) return new Object(-1, 'msg_not_permitted'); |
||
623 | |||
624 | //if empty trash, document already deleted, therefore document not delete |
||
625 | $args = new stdClass(); |
||
626 | $args->document_srl = $document_srl; |
||
627 | if(!$isEmptyTrash) |
||
628 | { |
||
629 | // Delete the document |
||
630 | $output = executeQuery('document.deleteDocument', $args); |
||
631 | if(!$output->toBool()) |
||
632 | { |
||
633 | $oDB->rollback(); |
||
634 | return $output; |
||
635 | } |
||
636 | } |
||
637 | |||
638 | $this->deleteDocumentAliasByDocument($document_srl); |
||
639 | |||
640 | $this->deleteDocumentHistory(null, $document_srl, null); |
||
641 | // Update category information if the category_srl exists. |
||
642 | if($oDocument->get('category_srl')) $this->updateCategoryCount($oDocument->get('module_srl'),$oDocument->get('category_srl')); |
||
643 | // Delete a declared list |
||
644 | executeQuery('document.deleteDeclared', $args); |
||
645 | // Delete extra variable |
||
646 | $this->deleteDocumentExtraVars($oDocument->get('module_srl'), $oDocument->document_srl); |
||
647 | |||
648 | //this |
||
649 | // Call a trigger (after) |
||
650 | View Code Duplication | if($output->toBool()) |
|
651 | { |
||
652 | $trigger_obj = $oDocument->getObjectVars(); |
||
653 | $trigger_output = ModuleHandler::triggerCall('document.deleteDocument', 'after', $trigger_obj); |
||
654 | if(!$trigger_output->toBool()) |
||
655 | { |
||
656 | $oDB->rollback(); |
||
657 | return $trigger_output; |
||
658 | } |
||
659 | } |
||
660 | // declared document, log delete |
||
661 | $this->_deleteDeclaredDocuments($args); |
||
662 | $this->_deleteDocumentReadedLog($args); |
||
663 | $this->_deleteDocumentVotedLog($args); |
||
664 | |||
665 | // Remove the thumbnail file |
||
666 | FileHandler::removeDir(sprintf('files/thumbnails/%s',getNumberingPath($document_srl, 3))); |
||
667 | |||
668 | // commit |
||
669 | $oDB->commit(); |
||
670 | |||
671 | //remove from cache |
||
672 | $oCacheHandler = CacheHandler::getInstance('object'); |
||
673 | if($oCacheHandler->isSupport()) |
||
674 | { |
||
675 | $cache_key = 'document_item:'. getNumberingPath($document_srl) . $document_srl; |
||
676 | $oCacheHandler->delete($cache_key); |
||
677 | } |
||
678 | |||
679 | return $output; |
||
680 | } |
||
681 | |||
682 | /** |
||
683 | * Delete declared document, log |
||
684 | * @param string $documentSrls (ex: 1, 2,56, 88) |
||
685 | * @return void |
||
686 | */ |
||
687 | function _deleteDeclaredDocuments($documentSrls) |
||
688 | { |
||
689 | executeQuery('document.deleteDeclaredDocuments', $documentSrls); |
||
690 | executeQuery('document.deleteDocumentDeclaredLog', $documentSrls); |
||
691 | } |
||
692 | |||
693 | /** |
||
694 | * Delete readed log |
||
695 | * @param string $documentSrls (ex: 1, 2,56, 88) |
||
696 | * @return void |
||
697 | */ |
||
698 | function _deleteDocumentReadedLog($documentSrls) |
||
702 | |||
703 | /** |
||
704 | * Delete voted log |
||
705 | * @param string $documentSrls (ex: 1, 2,56, 88) |
||
706 | * @return void |
||
707 | */ |
||
708 | function _deleteDocumentVotedLog($documentSrls) |
||
712 | |||
713 | /** |
||
714 | * Move the doc into the trash |
||
715 | * @param object $obj |
||
716 | * @return object |
||
717 | */ |
||
718 | function moveDocumentToTrash($obj) |
||
719 | { |
||
720 | $trash_args = new stdClass(); |
||
721 | // Get trash_srl if a given trash_srl doesn't exist |
||
722 | if(!$obj->trash_srl) $trash_args->trash_srl = getNextSequence(); |
||
723 | else $trash_args->trash_srl = $obj->trash_srl; |
||
724 | // Get its module_srl which the document belongs to |
||
725 | $oDocumentModel = getModel('document'); |
||
726 | $oDocument = $oDocumentModel->getDocument($obj->document_srl); |
||
727 | |||
728 | $trash_args->module_srl = $oDocument->get('module_srl'); |
||
729 | $obj->module_srl = $oDocument->get('module_srl'); |
||
730 | // Cannot throw data from the trash to the trash |
||
731 | if($trash_args->module_srl == 0) return false; |
||
732 | // Data setting |
||
733 | $trash_args->document_srl = $obj->document_srl; |
||
734 | $trash_args->description = $obj->description; |
||
735 | // Insert member's information only if the member is logged-in and not manually registered. |
||
736 | if(Context::get('is_logged')&&!$manual_inserted) |
||
737 | { |
||
738 | $logged_info = Context::get('logged_info'); |
||
739 | $trash_args->member_srl = $logged_info->member_srl; |
||
740 | |||
741 | // user_id, user_name and nick_name already encoded |
||
742 | $trash_args->user_id = htmlspecialchars_decode($logged_info->user_id); |
||
743 | $trash_args->user_name = htmlspecialchars_decode($logged_info->user_name); |
||
744 | $trash_args->nick_name = htmlspecialchars_decode($logged_info->nick_name); |
||
745 | } |
||
746 | // Date setting for updating documents |
||
747 | $document_args = new stdClass; |
||
748 | $document_args->module_srl = 0; |
||
749 | $document_args->document_srl = $obj->document_srl; |
||
750 | |||
751 | // begin transaction |
||
752 | $oDB = &DB::getInstance(); |
||
753 | $oDB->begin(); |
||
754 | |||
755 | /*$output = executeQuery('document.insertTrash', $trash_args); |
||
756 | if (!$output->toBool()) { |
||
757 | $oDB->rollback(); |
||
758 | return $output; |
||
759 | }*/ |
||
760 | |||
761 | // new trash module |
||
762 | require_once(_XE_PATH_.'modules/trash/model/TrashVO.php'); |
||
763 | $oTrashVO = new TrashVO(); |
||
764 | $oTrashVO->setTrashSrl(getNextSequence()); |
||
765 | $oTrashVO->setTitle($oDocument->variables['title']); |
||
766 | $oTrashVO->setOriginModule('document'); |
||
767 | $oTrashVO->setSerializedObject(serialize($oDocument->variables)); |
||
768 | $oTrashVO->setDescription($obj->description); |
||
769 | |||
770 | $oTrashAdminController = getAdminController('trash'); |
||
771 | $output = $oTrashAdminController->insertTrash($oTrashVO); |
||
772 | if(!$output->toBool()) |
||
773 | { |
||
774 | $oDB->rollback(); |
||
775 | return $output; |
||
776 | } |
||
777 | |||
778 | $output = executeQuery('document.deleteDocument', $trash_args); |
||
779 | if(!$output->toBool()) |
||
780 | { |
||
781 | $oDB->rollback(); |
||
782 | return $output; |
||
783 | } |
||
784 | |||
785 | /*$output = executeQuery('document.updateDocument', $document_args); |
||
786 | if (!$output->toBool()) { |
||
787 | $oDB->rollback(); |
||
788 | return $output; |
||
789 | }*/ |
||
790 | |||
791 | // update category |
||
792 | if($oDocument->get('category_srl')) $this->updateCategoryCount($oDocument->get('module_srl'),$oDocument->get('category_srl')); |
||
793 | |||
794 | // remove thumbnails |
||
795 | FileHandler::removeDir(sprintf('files/thumbnails/%s',getNumberingPath($obj->document_srl, 3))); |
||
796 | // Set the attachment to be invalid state |
||
797 | View Code Duplication | if($oDocument->hasUploadedFiles()) |
|
798 | { |
||
799 | $args = new stdClass(); |
||
800 | $args->upload_target_srl = $oDocument->document_srl; |
||
801 | $args->isvalid = 'N'; |
||
802 | executeQuery('file.updateFileValid', $args); |
||
803 | } |
||
804 | // Call a trigger (after) |
||
805 | View Code Duplication | if($output->toBool()) |
|
806 | { |
||
807 | $trigger_output = ModuleHandler::triggerCall('document.moveDocumentToTrash', 'after', $obj); |
||
808 | if(!$trigger_output->toBool()) |
||
809 | { |
||
810 | $oDB->rollback(); |
||
811 | return $trigger_output; |
||
812 | } |
||
813 | } |
||
814 | |||
815 | // commit |
||
816 | $oDB->commit(); |
||
817 | |||
818 | // Clear cache |
||
819 | $oCacheHandler = CacheHandler::getInstance('object'); |
||
820 | View Code Duplication | if($oCacheHandler->isSupport()) |
|
821 | { |
||
822 | $cache_key = 'document_item:'. getNumberingPath($oDocument->document_srl) . $oDocument->document_srl; |
||
823 | $oCacheHandler->delete($cache_key); |
||
824 | } |
||
825 | |||
826 | return $output; |
||
827 | } |
||
828 | |||
829 | /** |
||
830 | * Update read counts of the document |
||
831 | * @param documentItem $oDocument |
||
832 | * @return bool|void |
||
833 | */ |
||
834 | function updateReadedCount(&$oDocument) |
||
835 | { |
||
836 | // Pass if Crawler access |
||
837 | if(isCrawler()) return false; |
||
838 | |||
839 | $document_srl = $oDocument->document_srl; |
||
840 | $member_srl = $oDocument->get('member_srl'); |
||
841 | $logged_info = Context::get('logged_info'); |
||
842 | |||
843 | // Call a trigger when the read count is updated (before) |
||
844 | $trigger_output = ModuleHandler::triggerCall('document.updateReadedCount', 'before', $oDocument); |
||
845 | if(!$trigger_output->toBool()) return $trigger_output; |
||
846 | |||
847 | // Pass if read count is increaded on the session information |
||
848 | if($_SESSION['readed_document'][$document_srl]) return false; |
||
849 | |||
850 | // Pass if the author's IP address is as same as visitor's. |
||
851 | if($oDocument->get('ipaddress') == $_SERVER['REMOTE_ADDR']) |
||
852 | { |
||
853 | $_SESSION['readed_document'][$document_srl] = true; |
||
854 | return false; |
||
855 | } |
||
856 | // Pass ater registering sesscion if the author is a member and has same information as the currently logged-in user. |
||
857 | if($member_srl && $logged_info->member_srl == $member_srl) |
||
858 | { |
||
859 | $_SESSION['readed_document'][$document_srl] = true; |
||
860 | return false; |
||
861 | } |
||
862 | |||
863 | $oDB = DB::getInstance(); |
||
864 | $oDB->begin(); |
||
865 | |||
866 | // Update read counts |
||
867 | $args = new stdClass; |
||
868 | $args->document_srl = $document_srl; |
||
869 | $output = executeQuery('document.updateReadedCount', $args); |
||
870 | |||
871 | // Call a trigger when the read count is updated (after) |
||
872 | $trigger_output = ModuleHandler::triggerCall('document.updateReadedCount', 'after', $oDocument); |
||
873 | if(!$trigger_output->toBool()) |
||
874 | { |
||
875 | $oDB->rollback(); |
||
876 | return $trigger_output; |
||
877 | } |
||
878 | |||
879 | $oDB->commit(); |
||
880 | |||
881 | $oCacheHandler = CacheHandler::getInstance('object'); |
||
882 | if($oCacheHandler->isSupport()) |
||
883 | { |
||
884 | //remove document item from cache |
||
885 | $cache_key = 'document_item:'. getNumberingPath($document_srl) . $document_srl; |
||
886 | $oCacheHandler->delete($cache_key); |
||
887 | } |
||
888 | |||
889 | // Register session |
||
890 | if(!$_SESSION['banned_document'][$document_srl]) |
||
891 | { |
||
892 | $_SESSION['readed_document'][$document_srl] = true; |
||
893 | } |
||
894 | |||
895 | return TRUE; |
||
896 | } |
||
897 | |||
898 | /** |
||
899 | * Insert extra variables into the document table |
||
900 | * @param int $module_srl |
||
901 | * @param int $var_idx |
||
902 | * @param string $var_name |
||
903 | * @param string $var_type |
||
904 | * @param string $var_is_required |
||
905 | * @param string $var_search |
||
906 | * @param string $var_default |
||
907 | * @param string $var_desc |
||
908 | * @param int $eid |
||
909 | * @return object |
||
910 | */ |
||
911 | function insertDocumentExtraKey($module_srl, $var_idx, $var_name, $var_type, $var_is_required = 'N', $var_search = 'N', $var_default = '', $var_desc = '', $eid) |
||
912 | { |
||
913 | if(!$module_srl || !$var_idx || !$var_name || !$var_type || !$eid) return new Object(-1,'msg_invalid_request'); |
||
914 | |||
915 | $obj = new stdClass(); |
||
916 | $obj->module_srl = $module_srl; |
||
917 | $obj->var_idx = $var_idx; |
||
918 | $obj->var_name = $var_name; |
||
919 | $obj->var_type = $var_type; |
||
920 | $obj->var_is_required = $var_is_required=='Y'?'Y':'N'; |
||
921 | $obj->var_search = $var_search=='Y'?'Y':'N'; |
||
922 | $obj->var_default = $var_default; |
||
923 | $obj->var_desc = $var_desc; |
||
924 | $obj->eid = $eid; |
||
925 | |||
926 | $output = executeQuery('document.getDocumentExtraKeys', $obj); |
||
927 | if(!$output->data) |
||
928 | { |
||
929 | $output = executeQuery('document.insertDocumentExtraKey', $obj); |
||
930 | } |
||
931 | else |
||
932 | { |
||
933 | $output = executeQuery('document.updateDocumentExtraKey', $obj); |
||
934 | // Update the extra var(eid) |
||
935 | $output = executeQuery('document.updateDocumentExtraVar', $obj); |
||
936 | } |
||
937 | |||
938 | $oCacheHandler = CacheHandler::getInstance('object', NULL, TRUE); |
||
939 | if($oCacheHandler->isSupport()) |
||
940 | { |
||
941 | $object_key = 'module_document_extra_keys:'.$module_srl; |
||
942 | $cache_key = $oCacheHandler->getGroupKey('site_and_module', $object_key); |
||
943 | $oCacheHandler->delete($cache_key); |
||
944 | } |
||
945 | |||
946 | return $output; |
||
947 | } |
||
948 | |||
949 | /** |
||
950 | * Remove the extra variables of the documents |
||
951 | * @param int $module_srl |
||
952 | * @param int $var_idx |
||
953 | * @return Object |
||
954 | */ |
||
955 | function deleteDocumentExtraKeys($module_srl, $var_idx = null) |
||
956 | { |
||
957 | if(!$module_srl) return new Object(-1,'msg_invalid_request'); |
||
958 | $obj = new stdClass(); |
||
959 | $obj->module_srl = $module_srl; |
||
960 | if(!is_null($var_idx)) $obj->var_idx = $var_idx; |
||
961 | |||
962 | $oDB = DB::getInstance(); |
||
963 | $oDB->begin(); |
||
964 | |||
965 | $output = $oDB->executeQuery('document.deleteDocumentExtraKeys', $obj); |
||
966 | if(!$output->toBool()) |
||
967 | { |
||
968 | $oDB->rollback(); |
||
969 | return $output; |
||
970 | } |
||
971 | |||
972 | if($var_idx != NULL) |
||
973 | { |
||
974 | $output = $oDB->executeQuery('document.updateDocumentExtraKeyIdxOrder', $obj); |
||
975 | if(!$output->toBool()) |
||
976 | { |
||
977 | $oDB->rollback(); |
||
978 | return $output; |
||
979 | } |
||
980 | } |
||
981 | |||
982 | $output = executeQuery('document.deleteDocumentExtraVars', $obj); |
||
983 | if(!$output->toBool()) |
||
984 | { |
||
985 | $oDB->rollback(); |
||
986 | return $output; |
||
987 | } |
||
988 | |||
989 | if($var_idx != NULL) |
||
990 | { |
||
991 | $output = $oDB->executeQuery('document.updateDocumentExtraVarIdxOrder', $obj); |
||
992 | if(!$output->toBool()) |
||
993 | { |
||
994 | $oDB->rollback(); |
||
995 | return $output; |
||
996 | } |
||
997 | } |
||
998 | |||
999 | $oDB->commit(); |
||
1000 | |||
1001 | $oCacheHandler = CacheHandler::getInstance('object', NULL, TRUE); |
||
1002 | if($oCacheHandler->isSupport()) |
||
1003 | { |
||
1004 | $object_key = 'module_document_extra_keys:'.$module_srl; |
||
1005 | $cache_key = $oCacheHandler->getGroupKey('site_and_module', $object_key); |
||
1006 | $oCacheHandler->delete($cache_key); |
||
1007 | } |
||
1008 | |||
1009 | return new Object(); |
||
1010 | } |
||
1011 | |||
1012 | /** |
||
1013 | * Insert extra vaiable to the documents table |
||
1014 | * @param int $module_srl |
||
1015 | * @param int $document_srl |
||
1016 | * @param int $var_idx |
||
1017 | * @param mixed $value |
||
1018 | * @param int $eid |
||
1019 | * @param string $lang_code |
||
1020 | * @return Object|void |
||
1021 | */ |
||
1022 | function insertDocumentExtraVar($module_srl, $document_srl, $var_idx, $value, $eid = null, $lang_code = '') |
||
1023 | { |
||
1024 | if(!$module_srl || !$document_srl || !$var_idx || !isset($value)) return new Object(-1,'msg_invalid_request'); |
||
1025 | if(!$lang_code) $lang_code = Context::getLangType(); |
||
1026 | |||
1027 | $obj = new stdClass; |
||
1028 | $obj->module_srl = $module_srl; |
||
1029 | $obj->document_srl = $document_srl; |
||
1030 | $obj->var_idx = $var_idx; |
||
1031 | $obj->value = $value; |
||
1032 | $obj->lang_code = $lang_code; |
||
1033 | $obj->eid = $eid; |
||
1034 | |||
1035 | executeQuery('document.insertDocumentExtraVar', $obj); |
||
1036 | } |
||
1037 | |||
1038 | /** |
||
1039 | * Remove values of extra variable from the document |
||
1040 | * @param int $module_srl |
||
1041 | * @param int $document_srl |
||
1042 | * @param int $var_idx |
||
1043 | * @param string $lang_code |
||
1044 | * @param int $eid |
||
1045 | * @return $output |
||
1046 | */ |
||
1047 | function deleteDocumentExtraVars($module_srl, $document_srl = null, $var_idx = null, $lang_code = null, $eid = null) |
||
1048 | { |
||
1049 | $obj = new stdClass(); |
||
1050 | $obj->module_srl = $module_srl; |
||
1051 | if(!is_null($document_srl)) $obj->document_srl = $document_srl; |
||
1052 | if(!is_null($var_idx)) $obj->var_idx = $var_idx; |
||
1053 | if(!is_null($lang_code)) $obj->lang_code = $lang_code; |
||
1054 | if(!is_null($eid)) $obj->eid = $eid; |
||
1055 | $output = executeQuery('document.deleteDocumentExtraVars', $obj); |
||
1056 | return $output; |
||
1057 | } |
||
1058 | |||
1059 | |||
1060 | /** |
||
1061 | * Increase the number of vote-up of the document |
||
1062 | * @param int $document_srl |
||
1063 | * @param int $point |
||
1064 | * @return Object |
||
1065 | */ |
||
1066 | function updateVotedCount($document_srl, $point = 1) |
||
1067 | { |
||
1068 | if($point > 0) $failed_voted = 'failed_voted'; |
||
1069 | else $failed_voted = 'failed_blamed'; |
||
1070 | // Return fail if session already has information about votes |
||
1071 | if($_SESSION['voted_document'][$document_srl]) |
||
1072 | { |
||
1073 | return new Object(-1, $failed_voted); |
||
1074 | } |
||
1075 | // Get the original document |
||
1076 | $oDocumentModel = getModel('document'); |
||
1077 | $oDocument = $oDocumentModel->getDocument($document_srl, false, false); |
||
1078 | // Pass if the author's IP address is as same as visitor's. |
||
1079 | if($oDocument->get('ipaddress') == $_SERVER['REMOTE_ADDR']) |
||
1080 | { |
||
1081 | $_SESSION['voted_document'][$document_srl] = true; |
||
1082 | return new Object(-1, $failed_voted); |
||
1083 | } |
||
1084 | |||
1085 | // Create a member model object |
||
1086 | $oMemberModel = getModel('member'); |
||
1087 | $member_srl = $oMemberModel->getLoggedMemberSrl(); |
||
1088 | |||
1089 | // Check if document's author is a member. |
||
1090 | if($oDocument->get('member_srl')) |
||
1091 | { |
||
1092 | // Pass after registering a session if author's information is same as the currently logged-in user's. |
||
1093 | if($member_srl && $member_srl == $oDocument->get('member_srl')) |
||
1094 | { |
||
1095 | $_SESSION['voted_document'][$document_srl] = true; |
||
1096 | return new Object(-1, $failed_voted); |
||
1097 | } |
||
1098 | } |
||
1099 | |||
1100 | // Use member_srl for logged-in members and IP address for non-members. |
||
1101 | $args = new stdClass; |
||
1102 | if($member_srl) |
||
1103 | { |
||
1104 | $args->member_srl = $member_srl; |
||
1105 | } |
||
1106 | else |
||
1107 | { |
||
1108 | $args->ipaddress = $_SERVER['REMOTE_ADDR']; |
||
1109 | } |
||
1110 | $args->document_srl = $document_srl; |
||
1111 | $output = executeQuery('document.getDocumentVotedLogInfo', $args); |
||
1112 | // Pass after registering a session if log information has vote-up logs |
||
1113 | if($output->data->count) |
||
1114 | { |
||
1115 | $_SESSION['voted_document'][$document_srl] = true; |
||
1116 | return new Object(-1, $failed_voted); |
||
1117 | } |
||
1118 | |||
1119 | // begin transaction |
||
1120 | $oDB = DB::getInstance(); |
||
1121 | $oDB->begin(); |
||
1122 | |||
1123 | // Update the voted count |
||
1124 | View Code Duplication | if($point < 0) |
|
1125 | { |
||
1126 | $args->blamed_count = $oDocument->get('blamed_count') + $point; |
||
1127 | $output = executeQuery('document.updateBlamedCount', $args); |
||
1128 | } |
||
1129 | else |
||
1130 | { |
||
1131 | $args->voted_count = $oDocument->get('voted_count') + $point; |
||
1132 | $output = executeQuery('document.updateVotedCount', $args); |
||
1133 | } |
||
1134 | if(!$output->toBool()) return $output; |
||
1135 | // Leave logs |
||
1136 | $args->point = $point; |
||
1137 | $output = executeQuery('document.insertDocumentVotedLog', $args); |
||
1138 | if(!$output->toBool()) return $output; |
||
1139 | |||
1140 | $obj = new stdClass; |
||
1141 | $obj->member_srl = $oDocument->get('member_srl'); |
||
1142 | $obj->module_srl = $oDocument->get('module_srl'); |
||
1143 | $obj->document_srl = $oDocument->get('document_srl'); |
||
1144 | $obj->update_target = ($point < 0) ? 'blamed_count' : 'voted_count'; |
||
1145 | $obj->point = $point; |
||
1146 | $obj->before_point = ($point < 0) ? $oDocument->get('blamed_count') : $oDocument->get('voted_count'); |
||
1147 | $obj->after_point = ($point < 0) ? $args->blamed_count : $args->voted_count; |
||
1148 | $trigger_output = ModuleHandler::triggerCall('document.updateVotedCount', 'after', $obj); |
||
1149 | if(!$trigger_output->toBool()) |
||
1150 | { |
||
1151 | $oDB->rollback(); |
||
1152 | return $trigger_output; |
||
1153 | } |
||
1154 | |||
1155 | $oDB->commit(); |
||
1156 | |||
1157 | $oCacheHandler = CacheHandler::getInstance('object'); |
||
1158 | if($oCacheHandler->isSupport()) |
||
1159 | { |
||
1160 | //remove document item from cache |
||
1161 | $cache_key = 'document_item:'. getNumberingPath($document_srl) . $document_srl; |
||
1162 | $oCacheHandler->delete($cache_key); |
||
1163 | } |
||
1164 | |||
1165 | // Leave in the session information |
||
1166 | $_SESSION['voted_document'][$document_srl] = true; |
||
1167 | |||
1168 | // Return result |
||
1169 | $output = new Object(); |
||
1170 | if($point > 0) |
||
1171 | { |
||
1172 | $output->setMessage('success_voted'); |
||
1173 | $output->add('voted_count', $obj->after_point); |
||
1174 | } |
||
1175 | else |
||
1176 | { |
||
1177 | $output->setMessage('success_blamed'); |
||
1178 | $output->add('blamed_count', $obj->after_point); |
||
1179 | } |
||
1180 | |||
1181 | return $output; |
||
1182 | } |
||
1183 | |||
1184 | /** |
||
1185 | * Report posts |
||
1186 | * @param int $document_srl |
||
1187 | * @return void|Object |
||
1188 | */ |
||
1189 | function declaredDocument($document_srl) |
||
1190 | { |
||
1191 | // Fail if session information already has a reported document |
||
1192 | if($_SESSION['declared_document'][$document_srl]) return new Object(-1, 'failed_declared'); |
||
1193 | |||
1194 | // Check if previously reported |
||
1195 | $args = new stdClass(); |
||
1196 | $args->document_srl = $document_srl; |
||
1197 | $output = executeQuery('document.getDeclaredDocument', $args); |
||
1198 | if(!$output->toBool()) return $output; |
||
1199 | |||
1200 | $declared_count = ($output->data->declared_count) ? $output->data->declared_count : 0; |
||
1201 | |||
1202 | $trigger_obj = new stdClass(); |
||
1203 | $trigger_obj->document_srl = $document_srl; |
||
1204 | $trigger_obj->declared_count = $declared_count; |
||
1205 | |||
1206 | // Call a trigger (before) |
||
1207 | $trigger_output = ModuleHandler::triggerCall('document.declaredDocument', 'before', $trigger_obj); |
||
1208 | if(!$trigger_output->toBool()) |
||
1209 | { |
||
1210 | return $trigger_output; |
||
1211 | } |
||
1212 | |||
1213 | // Get the original document |
||
1214 | $oDocumentModel = getModel('document'); |
||
1215 | $oDocument = $oDocumentModel->getDocument($document_srl, false, false); |
||
1216 | |||
1217 | // Pass if the author's IP address is as same as visitor's. |
||
1218 | View Code Duplication | if($oDocument->get('ipaddress') == $_SERVER['REMOTE_ADDR']) { |
|
1219 | $_SESSION['declared_document'][$document_srl] = true; |
||
1220 | return new Object(-1, 'failed_declared'); |
||
1221 | } |
||
1222 | |||
1223 | // Check if document's author is a member. |
||
1224 | View Code Duplication | if($oDocument->get('member_srl')) |
|
1225 | { |
||
1226 | // Create a member model object |
||
1227 | $oMemberModel = getModel('member'); |
||
1228 | $member_srl = $oMemberModel->getLoggedMemberSrl(); |
||
1229 | // Pass after registering a session if author's information is same as the currently logged-in user's. |
||
1230 | if($member_srl && $member_srl == $oDocument->get('member_srl')) |
||
1231 | { |
||
1232 | $_SESSION['declared_document'][$document_srl] = true; |
||
1233 | return new Object(-1, 'failed_declared'); |
||
1234 | } |
||
1235 | } |
||
1236 | |||
1237 | // Use member_srl for logged-in members and IP address for non-members. |
||
1238 | $args = new stdClass; |
||
1239 | if($member_srl) |
||
1240 | { |
||
1241 | $args->member_srl = $member_srl; |
||
1242 | } |
||
1243 | else |
||
1244 | { |
||
1245 | $args->ipaddress = $_SERVER['REMOTE_ADDR']; |
||
1246 | } |
||
1247 | |||
1248 | $args->document_srl = $document_srl; |
||
1249 | $output = executeQuery('document.getDocumentDeclaredLogInfo', $args); |
||
1250 | |||
1251 | // Pass after registering a sesson if reported/declared documents are in the logs. |
||
1252 | if($output->data->count) |
||
1253 | { |
||
1254 | $_SESSION['declared_document'][$document_srl] = true; |
||
1255 | return new Object(-1, 'failed_declared'); |
||
1256 | } |
||
1257 | |||
1258 | // begin transaction |
||
1259 | $oDB = &DB::getInstance(); |
||
1260 | $oDB->begin(); |
||
1261 | |||
1262 | // Add the declared document |
||
1263 | if($declared_count > 0) $output = executeQuery('document.updateDeclaredDocument', $args); |
||
1264 | else $output = executeQuery('document.insertDeclaredDocument', $args); |
||
1265 | if(!$output->toBool()) return $output; |
||
1266 | // Leave logs |
||
1267 | $output = executeQuery('document.insertDocumentDeclaredLog', $args); |
||
1268 | if(!$output->toBool()) |
||
1269 | { |
||
1270 | $oDB->rollback(); |
||
1271 | return $output; |
||
1272 | } |
||
1273 | |||
1274 | $this->add('declared_count', $declared_count+1); |
||
1275 | |||
1276 | // Call a trigger (after) |
||
1277 | $trigger_obj->declared_count = $declared_count + 1; |
||
1278 | $trigger_output = ModuleHandler::triggerCall('document.declaredDocument', 'after', $trigger_obj); |
||
1279 | if(!$trigger_output->toBool()) |
||
1280 | { |
||
1281 | $oDB->rollback(); |
||
1282 | return $trigger_output; |
||
1283 | } |
||
1284 | |||
1285 | // commit |
||
1286 | $oDB->commit(); |
||
1287 | |||
1288 | // Leave in the session information |
||
1289 | $_SESSION['declared_document'][$document_srl] = true; |
||
1290 | |||
1291 | $this->setMessage('success_declared'); |
||
1292 | } |
||
1293 | |||
1294 | /** |
||
1295 | * Increase the number of comments in the document |
||
1296 | * Update modified date, modifier, and order with increasing comment count |
||
1297 | * @param int $document_srl |
||
1298 | * @param int $comment_count |
||
1299 | * @param string $last_updater |
||
1300 | * @param bool $comment_inserted |
||
1301 | * @return object |
||
1302 | */ |
||
1303 | function updateCommentCount($document_srl, $comment_count, $last_updater, $comment_inserted = false) |
||
1325 | |||
1326 | /** |
||
1327 | * Increase trackback count of the document |
||
1328 | * @param int $document_srl |
||
1329 | * @param int $trackback_count |
||
1330 | * @return object |
||
1331 | */ |
||
1332 | function updateTrackbackCount($document_srl, $trackback_count) |
||
1333 | { |
||
1334 | $args = new stdClass; |
||
1335 | $args->document_srl = $document_srl; |
||
1336 | $args->trackback_count = $trackback_count; |
||
1337 | |||
1338 | $oCacheHandler = CacheHandler::getInstance('object'); |
||
1339 | if($oCacheHandler->isSupport()) |
||
1340 | { |
||
1341 | //remove document item from cache |
||
1342 | $cache_key = 'document_item:'. getNumberingPath($document_srl) . $document_srl; |
||
1348 | |||
1349 | /** |
||
1350 | * Add a category |
||
1351 | * @param object $obj |
||
1352 | * @return object |
||
1353 | */ |
||
1354 | function insertCategory($obj) |
||
1380 | |||
1381 | /** |
||
1382 | * Increase list_count from a specific category |
||
1383 | * @param int $module_srl |
||
1384 | * @param int $list_order |
||
1385 | * @return object |
||
1386 | */ |
||
1387 | function updateCategoryListOrder($module_srl, $list_order) |
||
1394 | |||
1395 | /** |
||
1396 | * Update document_count in the category. |
||
1397 | * @param int $module_srl |
||
1398 | * @param int $category_srl |
||
1399 | * @param int $document_count |
||
1400 | * @return object |
||
1401 | */ |
||
1402 | function updateCategoryCount($module_srl, $category_srl, $document_count = 0) |
||
1416 | |||
1417 | /** |
||
1418 | * Update category information |
||
1419 | * @param object $obj |
||
1420 | * @return object |
||
1421 | */ |
||
1422 | function updateCategory($obj) |
||
1428 | |||
1429 | /** |
||
1430 | * Delete a category |
||
1431 | * @param int $category_srl |
||
1432 | * @return object |
||
1433 | */ |
||
1434 | function deleteCategory($category_srl) |
||
1481 | |||
1482 | /** |
||
1483 | * Delete all categories in a module |
||
1484 | * @param int $module_srl |
||
1485 | * @return object |
||
1486 | */ |
||
1487 | function deleteModuleCategory($module_srl) |
||
1494 | |||
1495 | /** |
||
1496 | * Move the category level to higher |
||
1497 | * @param int $category_srl |
||
1498 | * @return Object |
||
1499 | */ |
||
1500 | function moveCategoryUp($category_srl) |
||
1541 | |||
1542 | /** |
||
1543 | * Move the category down |
||
1544 | * @param int $category_srl |
||
1545 | * @return Object |
||
1546 | */ |
||
1547 | function moveCategoryDown($category_srl) |
||
1586 | |||
1587 | /** |
||
1588 | * Add javascript codes into the header by checking values of document_extra_keys type, required and others |
||
1589 | * @param int $module_srl |
||
1590 | * @return void |
||
1591 | */ |
||
1592 | function addXmlJsFilter($module_srl) |
||
1624 | |||
1625 | /** |
||
1626 | * Add a category |
||
1627 | * @param object $args |
||
1628 | * @return void |
||
1629 | */ |
||
1630 | function procDocumentInsertCategory($args = null) |
||
1699 | |||
1700 | /** |
||
1701 | * Move a category |
||
1702 | * @return void |
||
1703 | */ |
||
1704 | function procDocumentMoveCategory() |
||
1763 | |||
1764 | /** |
||
1765 | * Delete a category |
||
1766 | * @return void |
||
1767 | */ |
||
1768 | function procDocumentDeleteCategory() |
||
1804 | |||
1805 | /** |
||
1806 | * Xml files updated |
||
1807 | * Occasionally the xml file is not generated after menu is configued on the admin page \n |
||
1808 | * The administrator can manually update the file in this case \n |
||
1809 | * Although the issue is not currently reproduced, it is unnecessay to remove. |
||
1810 | * @return void |
||
1811 | */ |
||
1812 | function procDocumentMakeXmlFile() |
||
1827 | |||
1828 | /** |
||
1829 | * Save the category in a cache file |
||
1830 | * @param int $module_srl |
||
1831 | * @return string |
||
1832 | */ |
||
1833 | function makeCategoryFile($module_srl) |
||
1939 | |||
1940 | /** |
||
1941 | * Create the xml data recursively referring to parent_srl |
||
1942 | * In the menu xml file, node tag is nested and xml doc enables the admin page to have a menu\n |
||
1943 | * (tree menu is implemented by reading xml file from the tree_menu.js) |
||
1944 | * @param array $source_node |
||
1945 | * @param array $tree |
||
1946 | * @param int $site_srl |
||
1947 | * @param string $xml_header_buff |
||
1948 | * @return string |
||
1949 | */ |
||
1950 | function getXmlTree($source_node, $tree, $site_srl, &$xml_header_buff) |
||
2014 | |||
2015 | /** |
||
2016 | * Change sorted nodes in an array to the php code and then return |
||
2017 | * When using menu on tpl, you can directly xml data. howver you may need javascrips additionally. |
||
2018 | * Therefore, you can configure the menu info directly from php cache file, not through DB. |
||
2019 | * You may include the cache in the ModuleHandler::displayContent() |
||
2020 | * @param array $source_node |
||
2021 | * @param array $tree |
||
2022 | * @param int $site_srl |
||
2023 | * @param string $php_header_buff |
||
2024 | * @return array |
||
2025 | */ |
||
2026 | function getPhpCacheCode($source_node, $tree, $site_srl, &$php_header_buff) |
||
2116 | |||
2117 | /** |
||
2118 | * A method to add a pop-up menu which appears when clicking |
||
2119 | * @param string $url |
||
2120 | * @param string $str |
||
2121 | * @param string $icon |
||
2122 | * @param string $target |
||
2123 | * @return void |
||
2124 | */ |
||
2125 | View Code Duplication | function addDocumentPopupMenu($url, $str, $icon = '', $target = 'self') |
|
2139 | |||
2140 | /** |
||
2141 | * Saved in the session when an administrator selects a post |
||
2142 | * @return void|Object |
||
2143 | */ |
||
2144 | function procDocumentAddCart() |
||
2212 | |||
2213 | /** |
||
2214 | * Move/ Delete the document in the seession |
||
2215 | * @return void|Object |
||
2216 | */ |
||
2217 | function procDocumentManageCheckedDocument() |
||
2338 | |||
2339 | /** |
||
2340 | * Insert document module config |
||
2341 | * @return void |
||
2342 | */ |
||
2343 | function procDocumentInsertModuleConfig() |
||
2374 | |||
2375 | /** |
||
2376 | * Document temporary save |
||
2377 | * @return void|Object |
||
2378 | */ |
||
2379 | function procDocumentTempSave() |
||
2440 | |||
2441 | /** |
||
2442 | * Return Document List for exec_xml |
||
2443 | * @return void|Object |
||
2444 | */ |
||
2445 | function procDocumentGetList() |
||
2467 | |||
2468 | /** |
||
2469 | * For old version, comment allow status check. |
||
2470 | * @param object $obj |
||
2471 | * @return void |
||
2472 | */ |
||
2473 | function _checkCommentStatusForOldVersion(&$obj) |
||
2481 | |||
2482 | /** |
||
2483 | * For old version, document status check. |
||
2484 | * @param object $obj |
||
2485 | * @return void |
||
2486 | */ |
||
2487 | function _checkDocumentStatusForOldVersion(&$obj) |
||
2492 | |||
2493 | public function updateUploaedCount($documentSrlList) |
||
2511 | |||
2512 | /** |
||
2513 | * Copy extra keys when module copied |
||
2514 | * @param object $obj |
||
2515 | * @return void |
||
2516 | */ |
||
2517 | function triggerCopyModuleExtraKeys(&$obj) |
||
2534 | |||
2535 | View Code Duplication | function triggerCopyModule(&$obj) |
|
2549 | } |
||
2550 | /* End of file document.controller.php */ |
||
2552 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.