function MyRecords()
 {
     $className = $this->tableArray[0];
     $files = $this->MyPaginatedRecords();
     $dos = null;
     $ar = new ArrayList(array());
     if ($files) {
         foreach ($files as $file) {
             $file->ChildrenLink = '';
             if (!$file->canView()) {
                 $file->Error = "YOU DO NOT HAVE PERMISSION TO VIEW THIS FILE.";
             }
             if (!file_exists($file->getFullPath())) {
                 $file->Error = "FILE CAN NOT BE FOUND.";
             }
             if ($className::get()->filter(array("ParentID" => $file->ID))->Count()) {
                 $file->ChildrenLink = $this->createLevelLink($file->ID);
             }
             $file->UsageCount = MetaTagCMSControlFileUse::file_usage_count($file, false, $saveListOfPlaces = true);
             if ($file instanceof Folder) {
                 $file->Type == "Folder";
                 $file->Icon == "metatags/images/Folder.png";
             } elseif ($file->UsageCount) {
                 $file->ListOfPlaces = MetaTagCMSControlFileUse::retrieve_list_of_places($file->ID);
             }
             if (!$file->ListOfPlaces) {
                 unset($file->ListOfPlaces);
             }
             $file->GoOneUpLink = $this->GoOneUpLink();
             $file->RecycleLink = $this->makeRecycleLink($file->ID);
             $dos[$file->ID] = new ArrayList();
             $segmentArray = array();
             $item = $file;
             $segmentArray[] = array("URLSegment" => $item->Name, "ID" => $item->ID, "ClassName" => $item->ClassName, "Title" => $item->Title, "Link" => "/" . $item->Filename);
             $x = 0;
             while ($item && $item->ParentID && $x < 10) {
                 $x++;
                 $item = $className::get()->byID($item->ParentID);
                 if ($item) {
                     $segmentArray[] = array("URLSegment" => $item->Name, "ID" => $item->ID, "ClassName" => $item->ClassName, "Title" => $item->Title, "Link" => $this->createLevelLink(intval($item->ParentID) - 0));
                 }
             }
             $segmentArray = array_reverse($segmentArray);
             foreach ($segmentArray as $segment) {
                 $dos[$file->ID]->push(new ArrayData($segment));
             }
             $file->ParentSegments = $dos[$file->ID];
             $file->TEST = "BOOOOOOOOOOO";
             $ar->add($file);
         }
     }
     return $ar;
 }
 function onBeforeWrite()
 {
     if ($this->stillNeedsToReplace && $this->owner instanceof Image && $this->owner->ID) {
         $this->stillNeedsToReplace = false;
         $oldObject = File::get()->byID($this->owner->ID);
         $oldFileName = "";
         if ($oldObject->Name) {
             $oldFileName = $oldObject->Name;
         }
         if ($oldFileName) {
             $oldPath = "";
             if (isset($oldObject->Filename)) {
                 $oldPath = $oldObject->Filename;
             }
             if (!$oldPath) {
                 $oldPath = $oldObject->getFilename();
             }
             $oldPath = str_replace($oldFileName, "", $oldPath);
             $newFileName = $this->owner->Name;
             $newPath = $this->owner->getRelativePath();
             $newPath = str_replace($newFileName, "", $newPath);
             if ($oldFileName != $newFileName || $oldPath != $oldPath) {
                 $checks = MetaTagCMSControlFileUse::get()->filter(array("ConnectionType" => "DB", "IsLiveVersion" => "0"));
                 $siteTreeItemsToChange = SiteTree::get()->where("\"SiteTree_ImageTracking\".\"FileID\" = " . $this->owner->ID)->innerJoin("SiteTree_ImageTracking", "\"SiteTree_ImageTracking\".\"SiteTreeID\" = \"SiteTree\".\"ID\"");
                 if ($siteTreeItemsToChange && $siteTreeItemsToChange->count()) {
                     foreach ($siteTreeItemsToChange as $siteTreeItemToChange) {
                         $trackings = $siteTreeItemToChange->ImageTracking();
                         if ($trackings && $trackings->count()) {
                             foreach ($trackings as $tracking) {
                                 $array = array("DataObjectClassName" => "SiteTree", "DataObjectFieldName" => $tracking->FieldName, "FileID" => $tracking->ID, "TrackedID" => $siteTreeItemToChange->ID, "IsImageTracking" => true);
                                 $item = new ArrayData($array);
                                 $this->imageTrackingAddAgain[] = $array;
                                 $checks->push($item);
                             }
                         }
                     }
                 }
                 if ($checks && $checks->count()) {
                     foreach ($checks as $check) {
                         $className = $check->DataObjectClassName;
                         $fieldName = $check->DataObjectFieldName;
                         if (isset($check->IsImageTracking) && $check->IsImageTracking) {
                             $dosToChange = $className::get()->filter(array("ID" => $check->TrackedID));
                         } else {
                             $dosToChange = $className::get()->where("LOCATE('{$oldFileName}', \"" . $fieldName . "\") > 0");
                         }
                         if ($dosToChange && $dosToChange->count()) {
                             foreach ($dosToChange as $doToChange) {
                                 $dom = new DOMDocument();
                                 $dom->loadHTML('<?xml encoding="UTF-8">' . $doToChange->{$fieldName});
                                 foreach ($dom->getElementsByTagName('img') as $node) {
                                     $oldSrc = $node->getAttribute('src');
                                     $newSrc = preg_replace('/' . str_replace("/", "\\/", $oldPath) . '(.*?)' . $oldFileName . '/', '' . $newPath . '$1' . $newFileName, $oldSrc);
                                     if ($oldSrc != $newSrc) {
                                         $oldFilePath = Director::baseFolder() . "/" . $oldSrc;
                                         $newFilePath = Director::baseFolder() . "/" . $newSrc;
                                         if (file_exists($oldFilePath) && !file_exists($newFilePath)) {
                                             rename($oldFilePath, $newFilePath);
                                         }
                                         $node->setAttribute('src', $newSrc);
                                     }
                                 }
                                 $doToChange->encoding = 'UTF-8';
                                 $doToChange->{$fieldName} = $dom->saveHTML();
                                 $data = preg_replace('/' . str_replace("/", "\\/", $oldPath) . '(.*?)' . $oldFileName . '/', '' . $newPath . '$1' . $newFileName, $doToChange->{$fieldName});
                                 //$this->owner->generateFormattedImage($format, $arg1, $arg2)
                                 if ($doToChange instanceof SiteTree) {
                                     $doToChange->writeToStage('Stage');
                                     $doToChange->publish('Stage', 'Live');
                                 } else {
                                     $doToChange->write();
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
 }
 function run($request)
 {
     if (isset($_GET["forreal"])) {
         $this->forReal = $_GET["forreal"];
     }
     if (isset($_GET["summaryonly"])) {
         $this->summaryOnly = $_GET["summaryonly"];
         DB::alteration_message("Prefer <a href=\"" . $this->linkWithGetParameter("all", 1) . "\">all details</a>?<hr />", "repaired");
     }
     if (isset($_GET["doone"])) {
         $this->forReal = 1;
         $this->doOne = urldecode($_GET["doone"]);
     }
     if (isset($_GET["cleanupfolder"])) {
         $this->cleanupFolder = intval($_GET["cleanupfolder"]);
     }
     //work out the folders to ignore...
     foreach ($this->Config()->get("folders_to_ignore") as $folderToIgnoreName) {
         $folderToIgnore = Folder::find_or_make($folderToIgnoreName);
         $this->addListOfIgnoreFoldersArray($folderToIgnore);
     }
     if (count($this->listOfIgnoreFoldersArray)) {
         DB::alteration_message("Files in the following Folders will be ignored: <br />&nbsp; &nbsp; &nbsp; - " . implode("<br />&nbsp; &nbsp; &nbsp; - ", $this->listOfIgnoreFoldersArray) . "<hr />", "repaired");
     }
     if (!$this->cleanupFolder) {
         if (!$this->forReal) {
             DB::alteration_message("Apply <a href=\"" . $this->linkWithGetParameter("forreal", 1) . "\">all suggested changes</a>? CAREFUL!<hr />", "deleted");
         }
         if (!$this->summaryOnly) {
             DB::alteration_message("Prefer a <a href=\"" . $this->linkWithGetParameter("summaryonly", 1) . "\">summary only</a>?<hr />", "repaired");
         }
         $checks = MetaTagCMSControlFileUse::get() - where("\"ConnectionType\" IN ('HAS_ONE') AND \"IsLiveVersion\" = 0 AND \"DataObjectClassName\" <> 'File'");
         if ($checks && $checks->count()) {
             foreach ($checks as $check) {
                 $folderName = $check->DataObjectClassName . "_" . $check->DataObjectFieldName;
                 if (!$this->doOne || $this->doOne == $folderName) {
                     $objectName = $check->DataObjectClassName;
                     $fieldName = $check->DataObjectFieldName . "ID";
                     $fileClassName = $check->FileClassName;
                     $folder = null;
                     DB::alteration_message("<hr /><h3>All files attached to {$objectName} . {$fieldName} <a href=\"" . $this->linkWithGetParameter("doone", $folderName) . "\">can be moved to</a> <span style=\"color: green;\">{$folderName}</span></h3>");
                     if ($this->summaryOnly) {
                         //do nothing
                     } else {
                         $objects = null;
                         if ($check->FileIsFile) {
                             $objects = $objectName::get()->where("\"" . $fieldName . "\" > 0");
                         } elseif ($check->DataObjectIsFile) {
                             $objects = $objectName::get()->where("\"" . $fieldName . "\" > 0");
                         }
                         if ($objects && $objects->count()) {
                             foreach ($objects as $object) {
                                 if ($object instanceof File) {
                                     $file = $object;
                                     //do nothing
                                 } else {
                                     $file = File::get()->byID($object->{$fieldName});
                                 }
                                 if ($file) {
                                     if ($file instanceof Folder) {
                                         //do nothing
                                     } else {
                                         if (!$folder) {
                                             $folder = Folder::find_or_make($folderName);
                                         }
                                         if ($file->ParentID == $folder->ID) {
                                             DB::alteration_message("OK ... " . $file->FileName, "created");
                                         } else {
                                             if (isset($this->listOfIgnoreFoldersArray[$file->ParentID])) {
                                                 DB::alteration_message("NOT MOVING (folder to be ignored): <br />/" . $file->FileName . " to <br />/assets/" . $folderName . "/" . $file->Name . "", "repaired");
                                             } else {
                                                 DB::alteration_message("MOVING: <br />/" . $file->FileName . " to <br />/assets/" . $folderName . "/" . $file->Name . "", "created");
                                                 if ($this->forReal) {
                                                     if ($file->exists()) {
                                                         if (file_exists($file->getFullPath())) {
                                                             $file->ParentID = $folder->ID;
                                                             $file->write();
                                                         } else {
                                                             DB::alteration_message("ERROR: phyiscal file could not be found: " . $file->getFullPath() . " ", "deleted");
                                                         }
                                                     } else {
                                                         DB::alteration_message("ERROR: file not found in database: /" . $file->FileName . " ", "deleted");
                                                     }
                                                 }
                                             }
                                         }
                                     }
                                 } else {
                                     DB::alteration_message("Could not find file referenced by " . $object->getTitle() . " (" . $object->class . ", " . $object->ID . ")", "deleted");
                                 }
                             }
                         } else {
                             DB::alteration_message("No objects in {$objectName} {$fieldName}.", "deleted");
                         }
                     }
                 }
             }
         } else {
             DB::alteration_message("Could not find any checks, please run /dev/build/", "deleted");
         }
     } else {
         DB::alteration_message("We are now showing folders only; <a href=\"" . $this->linkWithGetParameter("all", 1) . "\">view all</a><hr />", "restored");
     }
     DB::alteration_message("---------------------------------------");
     DB::alteration_message("---------------------------------------");
     DB::alteration_message("CLEANING FOLDERS");
     DB::alteration_message("---------------------------------------");
     DB::alteration_message("---------------------------------------");
     $folders = Folder::get();
     $hasEmptyFolders = false;
     if ($folders && $folders->count()) {
         foreach ($folders as $folder) {
             if (!MetaTagCMSControlFileUse::file_usage_count($folder, true)) {
                 $hasEmptyFolders = true;
                 if (file_exists($folder->getFullPath())) {
                     if ($this->cleanupFolder != $folder->ID && $this->cleanupFolder != -1) {
                         DB::alteration_message("found an empty folder: <strong>" . $folder->FileName . "</strong>; <a href=\"" . $this->linkWithGetParameter("cleanupfolder", $folder->ID) . "\">delete now</a>?", "restored");
                     }
                     if ($this->cleanupFolder == $folder->ID || $this->cleanupFolder == -1) {
                         DB::alteration_message("\n\t\t\t\t\t\t\t\tDeleting empty folder: <strong>" . $folder->FileName . "</strong>", "deleted");
                         $folder->delete();
                     }
                 } else {
                     DB::alteration_message("Could not find this phyiscal folder - it is empty can be deleted: " . $folder->getFullPath(), "deleted");
                 }
             }
         }
     } else {
         DB::alteration_message("Could not find any folders. There might be something wrong!", "deleted");
     }
     if (!$hasEmptyFolders) {
         DB::alteration_message("There are no empty folders!", "created");
     } else {
         DB::alteration_message("Delete <a href=\"" . $this->linkWithGetParameter("cleanupfolder", -1) . "\">all empty folders</a>?", "deleted");
     }
 }
 private static function upgrade_file_name(File $file, $verbose = true)
 {
     $fileID = $file->ID;
     if (self::file_usage_count($file, true)) {
         $checks = MetaTagCMSControlFileUse::get()->filter(array("BothAreFiles", 0));
         if ($checks && $checks->count()) {
             foreach ($checks as $check) {
                 if (!$check->IsLiveVersion) {
                     $objName = "";
                     $where = "";
                     $innerJoinTable = "";
                     $innerJoinJoin = "";
                     switch ($check->ConnectionType) {
                         case "HAS_ONE":
                             $innerJoinTable = "";
                             $innerJoinJoin = "";
                             if ($check->FileIsFile) {
                                 $where = "\"{$check->DataObjectFieldName}ID\" = {$fileID}";
                                 $objName = $check->DataObjectClassName;
                             } elseif ($check->DataObjectIsFile) {
                                 $where = "\"{$check->DataObjectFieldName}ID\" > 0 AND \"{$check->DataObjectClassName}\".\"ID\" = {$fileID}";
                                 $objName = $check->DataObjectClassName;
                                 $innerJoinTable = $check->FileClassName;
                                 $innerJoinJoin = "\"{$check->DataObjectClassName}\".\"ID\" = \"" . $check->FileClassName . "\".\"ID\" ";
                             }
                             break;
                         case "BELONGS_MANY_MANY":
                         case "MANY_MANY":
                             if ($check->ConnectionType == "BELONGS_MANY_MANY") {
                                 $innerJoinTable = "\"{$check->FileClassName}_{$check->DataObjectFieldName}\"";
                             } else {
                                 $innerJoinTable = "\"{$check->DataObjectClassName}_{$check->DataObjectFieldName}\"";
                             }
                             if ($check->FileIsFile) {
                                 $where = "{$innerJoinTable}.\"{$check->FileClassName}ID\" = {$fileID}";
                                 $objName = $check->DataObjectClassName;
                                 $innerJoinJoin = "\"{$check->DataObjectClassName}\".\"ID\" = {$innerJoinTable}.\"{$check->DataObjectClassName}ID\"";
                             } elseif ($check->DataObjectIsFile) {
                                 $where = "{$innerJoinTable}.\"{$check->DataObjectClassName}ID\" = {$fileID}";
                                 $objName = $check->FileClassName;
                                 $innerJoinJoin = "\"{$check->FileClassName}\".\"ID\" = {$innerJoinTable}.\"{$check->FileClassName}ID\"";
                             }
                             break;
                     }
                     $join = "";
                     if ($objName) {
                         $sort = null;
                         $limit = 1;
                         if (Config::inst()->get("MetaTagCMSControlFileUse", "debug")) {
                             echo "<hr />";
                             echo "TYPE: " . $check->ConnectionType . "<br />";
                             echo "CLASS: " . $objName . "<br />";
                             echo "WHERE: " . $where . "<br />";
                             echo "SORT: " . $sort . "<br />";
                             echo "JOIN: " . $join . "<br />";
                             echo "LIMIT: " . $limit . "<br />";
                             echo "<hr />";
                         }
                         $objects = $objName::get()->where($where)->sort($sort)->innerJoin($innerJoinTable, $innerJoinJoin)->limit($limit);
                         if ($objects && $objects->count()) {
                             $obj = $objects->First();
                             $oldTitle = $file->Title;
                             $newTitle = $obj->getTitle();
                             if (substr($newTitle, 0, 1) != "#" || intval($newTitle) == $newTitle) {
                                 $file->Title = $newTitle;
                                 $file->write();
                                 if ($verbose) {
                                     DB::alteration_message("Updating " . $file->Name . " title from " . $oldTitle . " to " . $newTitle, "created");
                                 }
                             } else {
                                 if ($verbose) {
                                     DB::alteration_message("There is no real title for " . $obj->ClassName . ": " . $newTitle);
                                 }
                             }
                         } else {
                             if ($verbose) {
                                 echo ".";
                             }
                         }
                     } else {
                         if ($verbose) {
                             echo ";";
                         }
                     }
                 } else {
                     if ($verbose) {
                         echo "-";
                     }
                 }
             }
         } else {
             if ($verbose) {
                 DB::alteration_message("There are no checks", "deleted");
             }
         }
     } else {
         if ($verbose) {
             DB::alteration_message("File <i>" . $file->Title . "</i> is not being used");
         }
     }
     return self::$file_usage_array[$fileID];
 }