Total Complexity | 59 |
Total Lines | 613 |
Duplicated Lines | 0 % |
Changes | 4 | ||
Bugs | 1 | Features | 0 |
Complex classes like app_Record 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.
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 app_Record, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
598 | class app_Record extends ORM_Record implements app_Object_Interface |
||
599 | { |
||
600 | /** |
||
601 | * @var Func_App |
||
602 | */ |
||
603 | protected $app = null; |
||
604 | |||
605 | |||
606 | |||
607 | |||
608 | /** |
||
609 | * Returns the value of the specified field or an iterator if $sFieldName represents a 'Many relation'. |
||
610 | * |
||
611 | * @param string $sFieldName The name of the field or relation for which the value must be returned. |
||
612 | * @param array $args Optional arguments. |
||
613 | * |
||
614 | * @return mixed The value of the field or null if the field is not a part of the record. |
||
615 | */ |
||
616 | public function __call($sFieldName, $args) |
||
617 | { |
||
618 | $value = $this->oParentSet->getBackend()->getRecordValue($this, $sFieldName); |
||
619 | $field = $this->oParentSet->$sFieldName; |
||
620 | if (!is_null($value) && $field instanceof ORM_FkField) { |
||
621 | |||
622 | $sClassName = $field->getForeignSetName(); |
||
623 | |||
624 | $App = $this->App(); |
||
625 | $prefixLength = mb_strlen($App->classPrefix); |
||
626 | $methodName = mb_substr($sClassName, $prefixLength); |
||
627 | $set = $App->$methodName(); |
||
628 | |||
629 | $set->setName($field->getName()); |
||
630 | $set->setDescription($field->getDescription()); |
||
631 | |||
632 | $record = $set->get($value); |
||
633 | return $record; |
||
634 | } |
||
635 | return $value; |
||
636 | } |
||
637 | |||
638 | /** |
||
639 | * {@inheritDoc} |
||
640 | * @see app_Object_Interface::setApp() |
||
641 | */ |
||
642 | public function setApp(Func_App $app) |
||
643 | { |
||
644 | $this->app = $app; |
||
645 | return $this; |
||
646 | } |
||
647 | |||
648 | /** |
||
649 | * Get APP object to use with this record |
||
650 | * |
||
651 | * @return Func_App |
||
652 | */ |
||
653 | public function App() |
||
654 | { |
||
655 | if (!isset($this->app)) { |
||
656 | // If the app object was not specified (through the setApp() method), |
||
657 | // we set it as parent set's App. |
||
658 | $this->setApp($this->getParentSet()->App()); |
||
659 | } |
||
660 | return $this->app; |
||
661 | } |
||
662 | |||
663 | |||
664 | /** |
||
665 | * Returns the base class name of a record. |
||
666 | * For example xxx_Contact will return 'Contact'. |
||
667 | * |
||
668 | * @since 1.0.40 |
||
669 | * @return string |
||
670 | */ |
||
671 | public function getClassName() |
||
672 | { |
||
673 | $App = $this->App(); |
||
674 | $rClass = new ReflectionClass(get_class($this)); |
||
675 | $component = $App->getComponentByName($rClass->getShortName()); |
||
676 | if(isset($component)){ |
||
677 | return $component->getRecordClassName(); |
||
678 | // list(, $classname) = explode('_', $component->getRecordClassName()); |
||
679 | // return $classname; |
||
680 | } |
||
681 | list(, $classname) = explode('_', get_class($this)); |
||
682 | return $classname; |
||
683 | } |
||
684 | |||
685 | |||
686 | /** |
||
687 | * Returns the string reference corresponding to the record. |
||
688 | * |
||
689 | * @return string A reference string (e.g. Contact:12) |
||
690 | */ |
||
691 | public function getRef() |
||
692 | { |
||
693 | if (!isset($this->id)) { |
||
694 | throw new app_Exception('Trying to get the reference string of a record without an id.'); |
||
695 | } |
||
696 | $classname = $this->getClassName(); |
||
697 | return $classname . ':' . $this->id; |
||
698 | } |
||
699 | |||
700 | |||
701 | /** |
||
702 | * @return app_Controller |
||
703 | */ |
||
704 | public function getController() |
||
705 | { |
||
706 | $App = $this->App(); |
||
707 | |||
708 | $ctrlName = $this->getClassName(); |
||
709 | return $App->Controller()->$ctrlName(); |
||
710 | } |
||
711 | |||
712 | |||
713 | /** |
||
714 | * Deletes the record with respect to referential integrity. |
||
715 | * |
||
716 | * Uses referential integrity as defined by hasManyRelation to delete/update |
||
717 | * referenced elements. |
||
718 | * |
||
719 | * @see app_RecordSet::hasMany() |
||
720 | * |
||
721 | * @return self |
||
722 | */ |
||
723 | public function delete($deletedStatus = null) |
||
724 | { |
||
725 | $App = $this->App(); |
||
726 | |||
727 | if (!isset($deletedStatus)) { |
||
728 | $deletedStatus = app_TraceableRecord::DELETED_STATUS_DELETED; |
||
729 | } |
||
730 | |||
731 | $set = $this->getParentSet(); |
||
732 | $recordIdName = $set->getPrimaryKey(); |
||
733 | $recordId = $this->$recordIdName; |
||
734 | |||
735 | // Uses referential integrity as defined by hasManyRelation to delete/update |
||
736 | // referenced elements. |
||
737 | $manyRelations = $set->getHasManyRelations(); |
||
738 | |||
739 | |||
740 | foreach ($manyRelations as $manyRelation) { |
||
741 | /* @var $manyRelation ORM_ManyRelation */ |
||
742 | |||
743 | $foreignSetClassName = $manyRelation->getForeignSetClassName(); |
||
744 | $foreignSetFieldName = $manyRelation->getForeignFieldName(); |
||
745 | $method = mb_substr($foreignSetClassName, mb_strlen($App->classPrefix)); |
||
746 | $foreignSet = $App->$method(); |
||
747 | |||
748 | switch ($manyRelation->getOnDeleteMethod()) { |
||
749 | |||
750 | case ORM_ManyRelation::ON_DELETE_SET_NULL: |
||
751 | |||
752 | $foreignRecords = $foreignSet->select($foreignSet->$foreignSetFieldName->is($recordId)); |
||
753 | |||
754 | foreach ($foreignRecords as $foreignRecord) { |
||
755 | $foreignRecord->$foreignSetFieldName = 0; |
||
756 | $foreignRecord->save(); |
||
757 | } |
||
758 | |||
759 | break; |
||
760 | |||
761 | case ORM_ManyRelation::ON_DELETE_CASCADE: |
||
762 | |||
763 | $foreignRecords = $foreignSet->select($foreignSet->$foreignSetFieldName->is($recordId)); |
||
764 | |||
765 | foreach ($foreignRecords as $foreignRecord) { |
||
766 | $foreignRecord->delete(); |
||
767 | } |
||
768 | |||
769 | break; |
||
770 | |||
771 | case ORM_ManyRelation::ON_DELETE_NO_ACTION: |
||
772 | default: |
||
773 | break; |
||
774 | |||
775 | } |
||
776 | } |
||
777 | |||
778 | |||
779 | // We remove all links to and from this record. |
||
780 | $linkSet = $App->LinkSet(); |
||
781 | |||
782 | $linkSet->delete( |
||
783 | $linkSet->sourceClass->is(get_class($this))->_AND_($linkSet->sourceId->is($recordId)) |
||
784 | ->_OR_( |
||
785 | $linkSet->targetClass->is(get_class($this))->_AND_($linkSet->targetId->is($recordId)) |
||
786 | ) |
||
787 | ); |
||
788 | |||
789 | |||
790 | $set->delete($set->$recordIdName->is($recordId), $deletedStatus); |
||
791 | |||
792 | return $this; |
||
793 | } |
||
794 | |||
795 | |||
796 | |||
797 | /** |
||
798 | * Reassociates all data asociated to the record to another |
||
799 | * specified one. |
||
800 | * |
||
801 | * @param int $id |
||
802 | * |
||
803 | * @return self |
||
804 | */ |
||
805 | public function replaceWith($id) |
||
806 | { |
||
807 | $App = $this->App(); |
||
808 | |||
809 | $set = $this->getParentSet(); |
||
810 | $recordIdName = $set->getPrimaryKey(); |
||
811 | $recordId = $this->$recordIdName; |
||
812 | |||
813 | // Use referential integrity as defined by hasManyRelation to delete/update |
||
814 | // referenced elements. |
||
815 | $manyRelations = $set->getHasManyRelations(); |
||
816 | |||
817 | |||
818 | foreach ($manyRelations as $manyRelation) { |
||
819 | /* @var $manyRelation ORM_ManyRelation */ |
||
820 | |||
821 | $foreignSetClassName = $manyRelation->getForeignSetClassName(); |
||
822 | $foreignSetFieldName = $manyRelation->getForeignFieldName(); |
||
823 | $method = mb_substr($foreignSetClassName, mb_strlen($App->classPrefix)); |
||
824 | $foreignSet = $App->$method(); |
||
825 | // $foreignSet = new $foreignSetClassName($App); |
||
826 | $foreignRecords = $foreignSet->select($foreignSet->$foreignSetFieldName->is($recordId)); |
||
827 | |||
828 | foreach ($foreignRecords as $foreignRecord) { |
||
829 | $foreignRecord->$foreignSetFieldName = $id; |
||
830 | $foreignRecord->save(); |
||
831 | } |
||
832 | } |
||
833 | |||
834 | |||
835 | // We replace all links to and from this record. |
||
836 | $linkSet = $App->LinkSet(); |
||
837 | |||
838 | $links = $linkSet->select( |
||
839 | $linkSet->sourceClass->is(get_class($this))->_AND_($linkSet->sourceId->is($recordId)) |
||
840 | ); |
||
841 | |||
842 | foreach ($links as $link) { |
||
843 | $link->sourceId = $id; |
||
844 | $link->save(); |
||
845 | } |
||
846 | |||
847 | $links = $linkSet->select( |
||
848 | $linkSet->targetClass->is(get_class($this))->_AND_($linkSet->targetId->is($recordId)) |
||
849 | ); |
||
850 | |||
851 | foreach ($links as $link) { |
||
852 | $link->targetId = $id; |
||
853 | $link->save(); |
||
854 | } |
||
855 | |||
856 | return $this; |
||
857 | } |
||
858 | |||
859 | |||
860 | /** |
||
861 | * |
||
862 | * |
||
863 | * @return array |
||
864 | */ |
||
865 | public function getRelatedRecords() |
||
866 | { |
||
867 | $App = $this->App(); |
||
868 | |||
869 | $set = $this->getParentSet(); |
||
870 | $recordIdName = $set->getPrimaryKey(); |
||
871 | $recordId = $this->$recordIdName; |
||
872 | |||
873 | // Use referential integrity as defined by hasManyRelation to delete/update |
||
874 | // referenced elements. |
||
875 | $manyRelations = $set->getHasManyRelations(); |
||
876 | |||
877 | $relatedRecords = array(); |
||
878 | |||
879 | foreach ($manyRelations as $manyRelation) { |
||
880 | /* @var $manyRelation ORM_ManyRelation */ |
||
881 | |||
882 | $foreignSetClassName = $manyRelation->getForeignSetClassName(); |
||
883 | $foreignSetFieldName = $manyRelation->getForeignFieldName(); |
||
884 | |||
885 | $method = mb_substr($foreignSetClassName, mb_strlen($App->classPrefix)); |
||
886 | $foreignSet = $App->$method(); |
||
887 | // $foreignSet = new $foreignSetClassName($App); |
||
888 | $foreignRecords = $foreignSet->select($foreignSet->$foreignSetFieldName->is($recordId)); |
||
889 | |||
890 | |||
891 | if ($foreignRecords->count() > 0) { |
||
892 | $relatedRecords[$foreignSetClassName] = $foreignRecords; |
||
893 | } |
||
894 | } |
||
895 | |||
896 | return $relatedRecords; |
||
897 | } |
||
898 | |||
899 | |||
900 | |||
901 | |||
902 | |||
903 | |||
904 | /** |
||
905 | * Upload path for record attachments |
||
906 | * |
||
907 | * @return bab_Path |
||
908 | */ |
||
909 | public function uploadPath() |
||
910 | { |
||
911 | $path = $this->App()->uploadPath(); |
||
912 | |||
913 | if (null === $path) |
||
914 | { |
||
915 | throw new Exception('Missing upload path information'); |
||
916 | return null; |
||
917 | } |
||
918 | |||
919 | $path->push(get_class($this)); |
||
920 | $path->push($this->id); |
||
921 | |||
922 | return $path; |
||
923 | } |
||
924 | |||
925 | |||
926 | |||
927 | |||
928 | |||
929 | |||
930 | /** |
||
931 | * @return bool |
||
932 | */ |
||
933 | public function isLinkedTo(app_Record $source, $linkType = null) |
||
934 | { |
||
935 | $linkSet = $this->App()->LinkSet(); |
||
936 | |||
937 | $criteria = $linkSet->sourceClass->is(get_class($source)) |
||
938 | ->_AND_($linkSet->sourceId->is($source->id)) |
||
939 | ->_AND_($linkSet->targetClass->is(get_class($this))) |
||
940 | ->_AND_($linkSet->targetId->is($this->id)); |
||
941 | if (isset($linkType)) { |
||
942 | if (is_array($linkType)) { |
||
943 | $criteria = $criteria->_AND_($linkSet->type->in($linkType)); |
||
944 | } else { |
||
945 | $criteria = $criteria->_AND_($linkSet->type->is($linkType)); |
||
946 | } |
||
947 | } |
||
948 | $links = $linkSet->select($criteria); |
||
949 | |||
950 | $isLinked = ($links->count() > 0); |
||
951 | |||
952 | $linkSet->__destruct(); |
||
953 | unset($linkSet); |
||
954 | |||
955 | return $isLinked; |
||
956 | } |
||
957 | |||
958 | |||
959 | |||
960 | /** |
||
961 | * @return bool |
||
962 | */ |
||
963 | public function isSourceOf(app_Record $target, $linkType = null) |
||
964 | { |
||
965 | $linkSet = $this->App()->LinkSet(); |
||
966 | |||
967 | $criteria = $linkSet->all( |
||
968 | $linkSet->targetClass->is(get_class($target)), |
||
969 | $linkSet->targetId->is($target->id), |
||
970 | $linkSet->sourceClass->is(get_class($this)), |
||
971 | $linkSet->sourceId->is($this->id) |
||
972 | ); |
||
973 | |||
974 | if (isset($linkType)) { |
||
975 | if (is_array($linkType)) { |
||
976 | $criteria = $criteria->_AND_($linkSet->type->in($linkType)); |
||
977 | } else { |
||
978 | $criteria = $criteria->_AND_($linkSet->type->is($linkType)); |
||
979 | } |
||
980 | } |
||
981 | $links = $linkSet->select($criteria); |
||
982 | |||
983 | $isLinked = ($links->count() > 0); |
||
984 | |||
985 | $linkSet->__destruct(); |
||
986 | unset($linkSet); |
||
987 | |||
988 | return $isLinked; |
||
989 | } |
||
990 | |||
991 | |||
992 | |||
993 | /** |
||
994 | * Link record to $source |
||
995 | * |
||
996 | * @param app_Record $source |
||
997 | * @param string $linkType |
||
998 | * @param string $data |
||
999 | * @return self |
||
1000 | */ |
||
1001 | public function linkTo(app_Record $source, $linkType = '', $data = '') |
||
1002 | { |
||
1003 | $linkSet = $this->App()->LinkSet(); |
||
1004 | |||
1005 | $link = $linkSet->create($source, $this, $linkType, $data); |
||
1006 | |||
1007 | $link->__destruct(); |
||
1008 | unset($link); |
||
1009 | |||
1010 | $linkSet->__destruct(); |
||
1011 | unset($linkSet); |
||
1012 | |||
1013 | return $this; |
||
1014 | } |
||
1015 | |||
1016 | /** |
||
1017 | * Unlink record from $source |
||
1018 | * |
||
1019 | * @return self |
||
1020 | */ |
||
1021 | public function unlinkFrom(app_Record $source, $linkType = null) |
||
1028 | } |
||
1029 | |||
1030 | |||
1031 | /** |
||
1032 | * import a value into a traceable record property if the value is not equal |
||
1033 | * |
||
1034 | * @param string $name property name |
||
1035 | * @param mixed $value value to set |
||
1036 | * |
||
1037 | * @return int 1 : the value has been modified | 0 : no change |
||
1038 | */ |
||
1039 | protected function importProperty($name, $value) |
||
1047 | } |
||
1048 | |||
1049 | |||
1050 | |||
1051 | /** |
||
1052 | * import a value into a tracable record property if the value is not equal, try with multiple date format |
||
1053 | * this method work for date field 0000-00-00 |
||
1054 | * |
||
1055 | * @param string $name property name |
||
1056 | * @param mixed $value value to set |
||
1057 | * |
||
1058 | * @return int 1 : the value has been modified | 0 : no change |
||
1059 | */ |
||
1060 | protected function importDate($name, $value) |
||
1061 | { |
||
1062 | if (preg_match('/[0-9]{4}-[0-9]{2}-[0-9]{2}/',$value)) { |
||
1063 | return $this->importProperty($name, $value); |
||
1064 | } |
||
1065 | |||
1066 | // try in DD/MM/YYYY format |
||
1067 | |||
1068 | if (preg_match('/(?P<day>[0-9]+)\/(?P<month>[0-9]+)\/(?P<year>[0-9]{2,4})/',$value, $matches)) { |
||
1069 | |||
1070 | $value = sprintf('%04d-%02d-%02d', (int) $matches['year'], (int) $matches['month'], (int) $matches['day']); |
||
1071 | |||
1072 | return $this->importProperty($name, $value); |
||
1073 | } |
||
1074 | |||
1075 | } |
||
1076 | |||
1077 | |||
1078 | |||
1079 | |||
1080 | /** |
||
1081 | * |
||
1082 | * @return string[] |
||
1083 | */ |
||
1084 | public function getViews() |
||
1085 | { |
||
1086 | $App = $this->App(); |
||
1087 | |||
1088 | $customSectionSet = $App->CustomSectionSet(); |
||
1089 | $customSections = $customSectionSet->select($customSectionSet->object->is($this->getClassName())); |
||
1090 | $customSections->groupBy($customSectionSet->view); |
||
1091 | |||
1092 | $views = array(); |
||
1093 | foreach ($customSections as $customSection) { |
||
1094 | $views[] = $customSection->view; |
||
1095 | } |
||
1096 | |||
1097 | if (empty($views)) { |
||
1098 | $views[] = ''; |
||
1099 | } |
||
1100 | |||
1101 | return $views; |
||
1102 | } |
||
1103 | |||
1104 | |||
1105 | |||
1106 | /** |
||
1107 | * Checks if the record is readable by the current user. |
||
1108 | * @since 1.0.21 |
||
1109 | * @return bool |
||
1110 | */ |
||
1111 | public function isReadable() |
||
1112 | { |
||
1113 | $set = $this->getParentSet(); |
||
1114 | return $set->select($set->isReadable()->_AND_($set->id->is($this->id)))->count() == 1; |
||
1115 | } |
||
1116 | |||
1117 | |||
1118 | /** |
||
1119 | * Checks if the record is updatable by the current user. |
||
1120 | * @since 1.0.21 |
||
1121 | * @return bool |
||
1122 | */ |
||
1123 | public function isUpdatable() |
||
1124 | { |
||
1125 | $set = $this->getParentSet(); |
||
1126 | return $set->select($set->isUpdatable()->_AND_($set->id->is($this->id)))->count() == 1; |
||
1127 | } |
||
1128 | |||
1129 | /** |
||
1130 | * Checks if the record is deletable by the current user. |
||
1131 | * @since 1.0.21 |
||
1132 | * @return bool |
||
1133 | */ |
||
1134 | public function isDeletable() |
||
1138 | } |
||
1139 | |||
1140 | |||
1141 | /** |
||
1142 | * Checks if the record can be put to the trash by the current user. |
||
1143 | * @return bool |
||
1144 | */ |
||
1145 | public function isRemovable() |
||
1146 | { |
||
1147 | $set = $this->getParentSet(); |
||
1148 | return $set->select($set->isRemovable()->_AND_($set->id->is($this->id)))->count() == 1; |
||
1149 | } |
||
1150 | |||
1151 | |||
1152 | /** |
||
1153 | * Checks if the record can be restored from the trash by the current user. |
||
1154 | * @return bool |
||
1155 | */ |
||
1156 | public function isRestorable() |
||
1157 | { |
||
1158 | $set = $this->getParentSet(); |
||
1159 | return $set->select($set->isRestorable()->_AND_($set->id->is($this->id)))->count() == 1; |
||
1160 | } |
||
1161 | |||
1162 | |||
1163 | /** |
||
1164 | * Ensures that the record is readable by the current user or throws an exception. |
||
1165 | * @since 1.0.40 |
||
1166 | * @param string $message |
||
1167 | * @throws app_AccessException |
||
1168 | */ |
||
1169 | public function requireReadable($message = null) |
||
1170 | { |
||
1171 | if (!$this->isReadable()) { |
||
1172 | $App = $this->App(); |
||
1173 | if (!isset($message)) { |
||
1174 | $message = $App->translate('Access denied'); |
||
1175 | } |
||
1176 | throw new app_AccessException($message); |
||
1177 | } |
||
1178 | } |
||
1179 | |||
1180 | /** |
||
1181 | * Ensures that the record is updatable by the current user or throws an exception. |
||
1182 | * @since 1.0.40 |
||
1183 | * @param string $message |
||
1184 | * @throws app_AccessException |
||
1185 | */ |
||
1186 | public function requireUpdatable($message = null) |
||
1187 | { |
||
1188 | if (!$this->isUpdatable()) { |
||
1189 | $App = $this->App(); |
||
1190 | if (!isset($message)) { |
||
1191 | $message = $App->translate('Access denied'); |
||
1192 | } |
||
1193 | throw new app_AccessException($message); |
||
1194 | } |
||
1195 | } |
||
1196 | |||
1197 | /** |
||
1198 | * Ensures that the record is deletable by the current user or throws an exception. |
||
1199 | * @since 1.0.40 |
||
1200 | * @param string $message |
||
1201 | * @throws app_AccessException |
||
1202 | */ |
||
1203 | public function requireDeletable($message = null) |
||
1211 | } |
||
1212 | } |
||
1213 | } |
||
1214 | |||
1215 |