SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2005
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Bidirection between Parent and Dependents?

    Should there be a back reference in each dependent to its parent? That is, make their association bidirectional.

    For instance, an Author his the parent. Stories are the dependents. We have this managed in a dependent mapping.

    Story has a getHTML() method, which returns the HTML for the story. Part of the equation is styling it. Author can have a default style, so Story has to know about it.

    Right now, I have the Author propogate the default to all the Stories. But it occured to me: why not give the Story a back reference to the Author and then have it manage all that mumbo jumbo?

    Let's not think about forward compatibility and what happens when we stop having them dependants. Should dependents contain back references to their parent?

  2. #2
    SitePoint Evangelist
    Join Date
    Jun 2003
    Location
    Melbourne, Australia
    Posts
    440
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Ambush Commander
    Should there be a back reference in each dependent to its parent? That is, make their association bidirectional.
    Surely that would depend on whether you always need to access properties of an Author when viewing a Story. When reading the story on a web page, there might be a link to the author, but until the reader clicks on the link (and therefore makes a request for the 'author page', the properties of the Author need not be used.

    Another example is a Session object. Other types of objects might wish to store variables in a Session, but the Session doesn't need to know about them to do that. You'd only need to establish some sort of naming practice to ensure that the different objects using the Session object can't overwrite each other's properties.

    Is these scenarios that fit your conception of 'parent' and 'dependent'?
    Zealotry is contingent upon 100 posts and addiction 200?

  3. #3
    simple tester McGruff's Avatar
    Join Date
    Sep 2003
    Location
    Glasgow
    Posts
    1,690
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Ambush Commander
    Story has a getHTML() method, which returns the HTML for the story. Part of the equation is styling it. Author can have a default style, so Story has to know about it.

    Right now, I have the Author propogate the default to all the Stories. But it occured to me: why not give the Story a back reference to the Author and then have it manage all that mumbo jumbo?
    Author & Story are domain objects but default styles and the getHtml() method are presentation. I'd suggest that you should move the latter out of the domain.

  4. #4
    SitePoint Enthusiast
    Join Date
    Nov 2005
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Surely that would depend on whether you always need to access properties of an Author when viewing a Story. When reading the story on a web page, there might be a link to the author, but until the reader clicks on the link (and therefore makes a request for the 'author page', the properties of the Author need not be used.
    Yes, this holds true most of the time (but not all). Here's a better example: Chapters correspond to files on the filesystem. Determining the names of the files requires a bit of logic (sorry, legacy system) that involves properties in the Story and all the Chapter objects. However, the calculation is also time consuming, so I'd like to make it lazy. Ideally, calling getFilename() on chapter will check whether or not the filenames have been calculated, and if it isn't, call the calculator to make the calculation. But to do that, it has to know about the Story's method calculateChapterFilenames().

    Author & Story are domain objects but default styles and the getHtml() method are presentation. I'd suggest that you should move the latter out of the domain.
    The thing is, the calculation of HTML from getHTML is very substantial (it involves parsing the text, validating it, doing replaces, and doesn't look like it will get simple any time soon). Tis would be folly to place it in the Presentation layer.

    Another layer is in order, then?

  5. #5
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Tis would be folly to place it in the Presentation layer.
    Not folly, good practice. Like McGruff has just said, you need to remove the presentation from the domain to the presentation, where it belongs. Looks like you should look at the ViewHelper pattern for a reference.

  6. #6
    simple tester McGruff's Avatar
    Join Date
    Sep 2003
    Location
    Glasgow
    Posts
    1,690
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Ambush Commander
    Another layer is in order, then?
    I don't think so. Domain objects should be entirely presentation-agnostic. This is a fundamental design rule. As an exercise, consider how you would create a version of the app for the command line. You might never need to do that but it helps to highlight which parts of your code are involved in presentation (input/output) and which are domain layer (unformatted data with some kind of "added value"). Domain objects should survive that unchanged.

    Instead of creating html in a Story object, I really think you should have some other (presentation) object making calls to the domain for unformatted data and itself applying whatever formatting or presentation logic you need.

    Maybe something like:

    PHP Code:
        $printer =& new HtmlStoryPrinter;
     if(
    $printer->isPrintable($author$story)) {
         
    $printer->print();
     } 
    What problems exactly would you see with that?

  7. #7
    SitePoint Wizard
    Join Date
    Aug 2004
    Location
    California
    Posts
    1,672
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Ambush Commander
    The thing is, the calculation of HTML from getHTML is very substantial (it involves parsing the text, validating it, doing replaces, and doesn't look like it will get simple any time soon). Tis would be folly to place it in the Presentation layer.
    I agree with McGruff, and the fact that you say that the system is complex is all the more reason (not less) for separation.
    Christopher

  8. #8
    SitePoint Enthusiast
    Join Date
    Nov 2005
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay. Once again, legacy system. When you're porting the stuff, you have to prioritize, otherwise you'll not get anything done. I'll put that on my todo list.

    Here's what I envisioned when you told me "put it in the presentation layer": a Smarty template with text being modified by a function that parsed it. The input isn't so straightforward, but calculating it doesn't belong to the Domain, so put it in presentation, but not necessarily the template. I think I've got it.

    Unfortunantely, we still haven't addressed the issue of bidirectionality. (can we sort of drop the complex presentation logic still belongs in presentation layer talk and deal with the original question? Sorry...)

  9. #9
    simple tester McGruff's Avatar
    Join Date
    Sep 2003
    Location
    Glasgow
    Posts
    1,690
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I was hoping that would just disappear if Story doesn't need to know about the Author style. What have you got now?

  10. #10
    SitePoint Enthusiast
    Join Date
    Nov 2005
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here's the code. Note the calculateAll() functions which are called after the object's dependents are loaded by the mapper. I've tried to name all the ones responsible for propagating information propagate() but some of the calculate() functions do it too...

    If it's too big, I can annotate it a bit more.

    PHP Code:
    <?php

    class Author extends DomainObject
    {
        
        var 
    $name;
        var 
    $realname;
        var 
    $joined;
        var 
    $siteURL;
        var 
    $siteName;
        var 
    $summary;
        var 
    $font;
        var 
    $screenname;
        var 
    $email;
        var 
    $manager = array();
        var 
    $member  = array();
        var 
    $creator = array();
        
        var 
    $stories = array();
        
        var 
    $_authinfo;
        var 
    $_sinf;
        var 
    $_sc;
        
        var 
    $_numberofChapters;
        var 
    $_numberofWords;
        
        function 
    getName()          {return $this->name;}
        function 
    getRealname()      {return $this->realname;}
        function 
    getJoined()        {return $this->joined;}
        function 
    getSiteURL()       {return $this->siteURL;}
        function 
    getSiteName()      {return $this->siteName;}
        function 
    getSummary()       {return speckit($this->summary);}
        function 
    getFont()          {return $this->font;}
        function 
    getScreenname()    {return $this->screenname;}
        function 
    getEmail()         {return $this->email;}
        function 
    getEmailToCloaked(){return $this->email->toCloaked();}
        function 
    getManager()       {return $this->manager;}
        function 
    getMember()        {return $this->member;}
        function 
    getCreator()       {return $this->creator;}
        function 
    getStories()       {return $this->stories;}
        
        function 
    getStory($name) {
            if (!isset(
    $this->stories[$name])) return null;
            return 
    $this->stories[$name];
        }
        
        function 
    getStoryKeys() {
            return 
    array_keys($this->stories);
        }
        
        function 
    Author(
          
    $name,    $realname,  $joined,
          
    $siteURL$siteName,  $summary,
          
    $font,    $screenname,$email,
          
    $manager$member,    $creator,
          &
    $stories
          
    ) {
            
    //call to parent constructor purposely omitted
            
    $this->name     $name;
            
    $this->realname $realname;
            
    $this->joined   $joined;
            
    $this->siteURL  $siteURL;
            
    $this->siteName $siteName;
            
    $this->summary  $summary;
            
    $this->font     $font;
            
    $this->screenname $screenname;
            
    $this->email    $email;
            
    $this->manager  $manager;
            
    $this->member   $member;
            
    $this->creator  $creator;
            
    $this->stories  =& $stories;
        }
        
        function 
    setMeta($authinfo$sinf$sc) {
            
    $this->_authinfo $authinfo;
            
    $this->_sinf $sinf;
            
    $this->_sc $sc;
        }
        
        function 
    isPublished() {
            return (bool) 
    count($this->stories);
        }
        
        function 
    numberofStories() {
            return 
    count($this->stories);
        }
        
        function 
    numberofChapters() {
            if (!isset(
    $this->_numberofChapters)) $this->_count();
            return 
    $this->_numberofChapters;
        }
        
        function 
    numberofWords() {
            if (!isset(
    $this->_numberofWords)) $this->_count();
            return 
    $this->_numberofWords;
        }
        
        function 
    _count() {
            
    $this->_numberofChapters 0;
            
    $this->_numberofWords    0;
            foreach(
    $this->stories as $story) {
                
    $s $story->getBase();
                
    $cc bigwordcount($this->name,$s,$this->_sc['f'][$s],$this->_sinf['oneshot'][$s]);
                
    $this->_numberofChapters += wordcount($cc,false);
                
    $this->_numberofWords    += wordcount($cc,true);
            }
        }
        
        function 
    calculateAll() {
            
    $this->propagateFonts();
            
    //$this->propagateBackReferences();
        
    }
        
        function 
    propagateFonts($override false) {
            if (empty(
    $this->font) && !$override) return;
            foreach (
    $this->stories as $key => $value) {
                
    $this->stories[$key]->setFormatStyleDefault($this->font);
            }
        }
        
        function 
    propagateBackReferences() {
            foreach (
    $this->stories as $key => $value) {
                
    $this->stories[$key]->setBackReference($this);
                
    $this->stories[$key]->propagateBackReferences();
            }
        }
        
    }

    ?>
    PHP Code:
    <?php

    class Story extends DomainObject
    {
        
        
    // the VARIABLES
        
        
    var $base;
        var 
    $title;
        var 
    $summary;
        var 
    $creation;
        var 
    $update;
        var 
    $oneshot;
        var 
    $authornote;
        var 
    $meta;
        var 
    $metaIs;
        var 
    $metaData;
        var 
    $original;
        var 
    $type;
        var 
    $formatStyle;
        var 
    $formatAlgorithm;
        var 
    $formatTriggerStrings;
        var 
    $categoryMain;
        var 
    $categorySuper;
        var 
    $categoryFan;
        var 
    $categoryFanSource;
        var 
    $authorRelationship;
        var 
    $fileSuffix;
        var 
    $fileExplicit;
        var 
    $labelOffset;
        var 
    $labelEndOffset;
        var 
    $labelFileBased;
        var 
    $labelExplicit;
        var 
    $chapterDelimiter;
        var 
    $chapters = array();
        
        
    // the GET METHODS
        
        
    function getBase() {return $this->base;}
        function 
    getTitle() {return $this->title;}
        function 
    getSummary() {return $this->summary;}
        function 
    getCreation() {return $this->creation;}
        function 
    getUpdate() {return $this->update;}
        function 
    getOneshot() {return $this->oneshot;}
        function 
    getAuthornote() {return $this->authornote;}
        function 
    getMeta() {return $this->meta;}
        function 
    getMetaIs() {return $this->metaIs;}
        function 
    getMetaData() {return $this->metaData;}
        function 
    getOriginal() {return $this->original;}
        function 
    getType() {return $this->type;}
        function 
    getFormatStyle() {return $this->formatStyle;}
        function 
    getFormatAlgorithm() {return $this->formatAlgorithm;}
        function 
    getFormatTriggerStrings() {return $this->formatTriggerStrings;}
        function 
    getCategoryMain() {return $this->categoryMain;}
        function 
    getCategorySuper() {return $this->categorySuper;}
        function 
    getCategoryFan() {return $this->categoryFan;}
        function 
    getCategoryFanSource() {return $this->categoryFanSource;}
        function 
    getAuthorRelationship() {return $this->authorRelationship;}
        function 
    getFileSuffix() {return $this->fileSuffix;}
        function 
    getFileExplicit() {return $this->FileExplicit;}
        function 
    getLabelOffset() {return $this->labelOffset;}
        function 
    getLabelEndOffset() {return $this->labelEndOffset;}
        function 
    getLabelFileBased() {return $this->labelFileBased;}
        function 
    getLabelExplicit() {return $this->labelExplicit;}
        function &
    getChapters() {return $this->chapters;}
        
        
    // the CONSTRUCTOR
        
        
    function Story (
          
    $base,                    $title,
          
    $summary,                 $creation,
          
    $update,                  $oneshot,
          
    $authornote,              $meta,
          
    $metaIs,                  $metaData,
          
    $original,                $type,
          
    $formatStyle,             $formatAlgorithm,
          
    $formatTriggerStrings,    $categoryMain,
          
    $categorySuper,           $categoryFan,
          
    $categoryFanSource,       $authorRelationship,
          
    $fileSuffix,              $fileExplicit,
          
    $labelOffset,             $labelEndOffset,
          
    $labelFileBased,          $labelExplicit,
          
    $formatClasses,           $chapterDelimiter,
          
    $chapters) {
            
    $this->base                 $base;
            
    $this->title                $title;
            
    $this->summary              $summary;
            
    $this->creation             $creation;
            
    $this->update               $update;
            
    $this->oneshot              $oneshot;
            
    $this->authornote           $authornote;
            
    $this->meta                 $meta;
            
    $this->metaIs               $metaIs;
            
    $this->metaData             $metaData;
            
    $this->original             $original;
            
    $this->type                 $type;
            
    $this->formatStyle          $formatStyle;
            
    $this->formatAlgorithm      $formatAlgorithm;
            
    $this->formatTriggerStrings $formatTriggerStrings;
            
    $this->categoryMain         $categoryMain;
            
    $this->categorySuper        $categorySuper;
            
    $this->categoryFan          $categoryFan;
            
    $this->categoryFanSource    $categoryFanSource;
            
    $this->authorRelationship   $authorRelationship;
            
    $this->fileSuffix           $fileSuffix;
            
    $this->fileExplicit         $fileExplicit;
            
    $this->labelOffset          $labelOffset;
            
    $this->labelEndOffset       $labelEndOffset;
            
    $this->labelFileBased       $labelFileBased;
            
    $this->labelExplicit        $labelExplicit;
            
    $this->formatClasses        $formatClasses;
            
    $this->chapterDelimiter     $chapterDelimiter;
            
    $this->chapters             $chapters;
        }
        
        
        
    // the META INFO
        
        
    var $_author;
        var 
    $_directory;
        var 
    $_word_count;
        
        function 
    getChapterCount() {
            return 
    count($this->chapters);
        }
        
        function 
    hasThumbnail() {
            
    $this->calculateChapterFilenames();
            return 
    filenameimgcheck($this->chapters[1]->getFilename());
        }
        
        function &
    getChapter($key) {
            if (empty(
    $this->chapters[$key])) return null;
            return 
    $this->chapters[$key];
        }
        
        function 
    getChapterDelimiter() {
            if (
    $this->chapterDelimiter === null$this->calculateChapterDelimiter();
            return 
    $this->chapterDelimiter;
        }
        
        function 
    getLastChapterNumber() {
            return 
    count($this->chapters);
        }
        
        function &
    getLastChapter() {
            return 
    $this->chapters[$this->getLastChapterNumber()];
        }
        
        
    // the META SET METHODS
        
        
    function setFormatStyleDefault($style) {
            if (
    $this->formatStyle) return;
            
    $this->formatStyle $style;
            
    $this->calculateFonts(true);
        }
        
        function 
    setAuthor($author) {$this->_author $author;}
        
        
    // the CALCULATIONS
        
        
    function calculateAll() {
            
    $this->calculateWordCount();
            
    $this->calculateChapterDelimiter();
            
    $this->calculateChapterLables();
            
    $this->calculateType();
            
    $this->propagateFormatAlgorithm();
            
    $this->propagateFormatTriggerStrings();
        }
        
        var 
    $_calculated_directory false;
        function 
    calculateDirectory() {
            if (
    $this->_calculated_directory) return;
            
    $this->_calculated_directory true;
            
    $directory "authordata/{$this->_author}/";
            
    $directory .= $this->oneshot 'one/' "multi/{$this->base}/";
            return 
    $this->_directory $directory;
        }
        
        var 
    $_calculated_chapter_filenames false;
        function 
    calculateChapterFilenames() {
            if (
    $this->_calculated_chapter_filenames) return;
            
            
    $this->_calculated_chapter_filenames true;
            
    $this->calculateDirectory();
            
            if (
    $this->oneshot) {
                
    $this->_calculateChapterFilenameForOneshot();
            } elseif (
    $this->fileExplicit) {
                
    $this->_calculateChapterFilenamesWhenExplicit();
            } else {
                
    $this->_calculateChapterFilenamesIntelligently();
            }
            
        }
        
        function 
    _calculateChapterFilenameForOneshot() {
            
    $filename $this->_directory "{$this->base}$this->fileSuffix;
            
    $this->chapters[1]->setFilename($filename);
        }
        
        function 
    _calculateChapterFilenamesWhenExplicit() {
            
    $special_filenames stopcard();
            
    $chapter_keys array_keys($this->chapters);
            foreach(
    $chapter_keys as $key) {
                
    $filepoint $this->chapters[$key]->getFilepoint();
                if (
    strpos($filepoint,'.') === false
                  
    && empty($special_filenames[$filepoint])) {
                    
    $this->chapters[$key]->setFilename($this->_directory $filepoint $this->fileSuffix);
                } elseif(empty(
    $special_filenames[$filepoint])) {
                    
    $this->chapters[$key]->setFilename($this->_directory $filepoint);
                } else {
                    
    $this->chapters[$key]->setFilename($filepoint);
                }
            }
        }
        
        function 
    _calculateChapterFilenamesIntelligently() {
            
    //i is the chapter index
            //j is the numeric portion of generated filenames
            
    $special_filenames stopcard();
            for (
    $i $j 1;
              isset(
    $this->chapters[$i]);
              
    $i++, $j++) {
                if (
    $filepoint $this->chapters[$i]->getFilepoint()) {
                    
    $j--;
                    if (
    strpos($filepoint,".") === false
                      
    && empty($special_filenames[$filepoint])) {
                        
    $this->chapters[$i]->setFilename($this->_directory $filepoint $this->fileSuffix);
                    } elseif(empty(
    $special_filenames[$filepoint])) {
                        
    $this->chapters[$i]->setFilename($this->_directory $filepoint);
                    } else {
                        
    $this->chapters[$i]->setFilename($filepoint);
                    }
                } else { 
    //generate filename
                    
    $this->chapters[$i]->setFilename($this->_directory $j $this->fileSuffix);
                }
            }
        }
        
        var 
    $_calculated_chapter_word_counts false;
        function 
    calculateChapterWordCounts() {
            if (
    $this->_calculated_chapter_word_counts) return;
            
    $this->_calculated_chapter_word_counts true;
            
    $this->calculateChapterFileNames();
            foreach(
    $this->chapters as $i => $value) {
                
    $this->chapters[$i]->calculateWordCount();
            }
        }
        
        var 
    $_calculated_word_count false;
        function 
    calculateWordCount() {
            if (
    $this->_calculated_word_count) return;
            
    $this->_calculated_word_count true;
            
    $this->calculateChapterWordCounts();
            
    $this->_word_count 0;
            foreach(
    $this->chapters as $i => $value) {
                
    $this->_word_count += $this->chapters[$i]->getWordCount();
            }
            return 
    $this->_word_count;
        }
        
        function 
    calculateChapterDelimiter() {
            if (
    $this->chapterDelimiter !== null) return;
            if (
    $this->categoryMain == 'poetry') {
                
    $this->chapterDelimiter "Poem";
            } elseif (
    $this->categoryMain == 'art' OR
                      
    $this->categoryMain == 'fanart') {
                
    $this->chapterDelimiter "Picture";
            } else {
                
    $this->chapterDelimiter "Chapter";
            }
        }
        
        var 
    $_calculated_chapter_labels false;
        function 
    calculateChapterLables() {
            if (
    $this->_calculated_chapter_labels) return;
            
    $this->_calculated_chapter_labels true;
            
            if (
    $this->labelExplicit) {
                
    $this->_calculateChapterLablesWhenExplicit();
            } elseif (
    $this->labelOffset || $this->labelEndOffset) {
                
    $this->_calculateChapterLablesWithOffsets();
            } elseif (
    $this->labelFileBased) {
                
    $this->calculateChapterFileNames();
                
    $this->_calculateChapterLablesWithFiles();
            } else {
                
    $this->_calculateChapterLablesDumb();
            }
        }
        
        function 
    _calculateChapterLablesWhenExplicit() {
            foreach(
    $this->chapters as $i => $value) {
                
    $this->chapters[$i]->setLabel($i ': ');
                continue;
            }
        }
        
        function 
    _calculateChapterLablesWithOffsets() {
            
    //i is the chapter index
            //j is the label's number accounting offsets
            
    $labels_begin $this->labelOffset;
            
    $labels_end   $this->getChapterCount() - $this->labelEndOffset;
            for (
    $i $j 1; isset($this->chapters[$i]); $i++, $j++) {
                if (
    $i <= $labels_begin || $i $labels_end) {
                    
    $this->chapters[$i]->setLabel('');
                    
    $j--;
                    continue;
                }
                
    $this->chapters[$i]->setLabel($j ': ');
            }
        }
        
        function 
    _calculateChapterLablesWithFiles() {
            foreach(
    $this->chapters as $i => $value) {
                
    $filename basename($this->chapters[$i]->getFilename());
                if (
    is_numeric($int substr($filename,0,strpos($filename,'.')))) {
                    
    $this->chapters[$i]->setLabel($int ': ');
                } else {
                    
    $this->chapters[$i]->setLabel('');
                }
            }
        }
        
        function 
    _calculateChapterLablesDumb() {
            for (
    $i 1; isset($this->chapters[$i]); $i++) {
                
    $this->chapters[$i]->setLabel($i ': ');
            }
        }
        
        var 
    $_calculated_fonts false;
        function 
    calculateFonts($override false) {
            if (
    $this->_calculated_fonts && !$override) return;
            
    $this->_calculated_fonts true;
            if (!
    $this->formatStyle) return;
            foreach(
    $this->chapters as $i => $value) {
                
    $this->chapters[$i]->setFontDefault($this->formatStyle);
            }
        }
        
        var 
    $_calculated_types false;
        function 
    calculateType() {
            if (
    $this->_calculated_types) return;
            
    $this->_calculated_types true;
            if (
    $this->type) {
                
    $this->chapters[1]->setType($this->type);
            }
            foreach(
    $this->chapters as $i => $value) {
                
    $this->chapters[$i]->calculateType();
            }
        }
        
        
    // the PROPAGATORS
        
        
    var $_propagated_format_algorithm false;
        function 
    propagateFormatAlgorithm($override false) {
            if (
    $this->_propagated_format_algorithm && !$override) return;
            
    $this->_propagated_format_algorithm true;
            for (
    $i 1; isset($this->chapters[$i]); $i++) {
                
    $this->chapters[$i]->setFormatAlgorithm($this->formatAlgorithm);
            }
        }
        
        var 
    $_propagated_format_trigger_strings;
        function 
    propagateFormatTriggerStrings($override false) {
            if (
    $this->_propagated_format_trigger_strings && !$override) return;
            
    $this->_propagated_format_trigger_strings true;
            for (
    $i 1; isset($this->chapters[$i]); $i++) {
                
    $this->chapters[$i]->setFormatTriggerStrings($this->formatTriggerStrings);
            }
        }
        
        var 
    $_back_reference;
        function 
    setBackReference(&$object) {
            
    $this->_back_reference =& $object;
        }
        
        function 
    propagateBackReferences() {
            foreach (
    $this->chapters as $key => $value) {
                
    $this->chapters[$key]->setBackReference($this);
            }
        }
        
    }

    ?>
    PHP Code:
    <?php

    class Chapter {
        
        var 
    $number;
        var 
    $name;
        var 
    $filepoint;
        var 
    $shortlabel;
        var 
    $owner;
        var 
    $font;
        var 
    $alignment;
        var 
    $quote;
        var 
    $authornote;
        
        var 
    $_filename;
        var 
    $_label;
        var 
    $_story;
        var 
    $_author;
        var 
    $_word_count;
        var 
    $_type;
        var 
    $_format_algorithm '';
        var 
    $_format_trigger_strings = array();
        
        function 
    Chapter(
          
    $number,      $name,
          
    $filepoint,    $shortlabel,
          
    $owner,      $font,
          
    $alignment,   $quote,
          
    $authornote) {
            
            
    $this->number   $number;
            
    $this->name     $name;
            
    $this->filepoint$filepoint;
            
    $this->shortlabel $shortlabel;
            
    $this->owner   $owner;
            
    $this->font     $font;
            
    $this->alignment$alignment;
            
    $this->quote    $quote;
            
    $this->authornote $authornote;
        }
        
        function 
    getNumber()    {return $this->number;}
        function 
    getName()      {return $this->name;}
        function 
    getFilepoint() {return $this->filepoint;}
        function 
    getShortlabel()     {return $this->shortlabel;}
        function 
    getOwner()    {return $this->owner;}
        function 
    getFont()      {return $this->font;}
        function 
    getAlignment() {return $this->alignment;}
        function 
    getQuote()     {return $this->quote;}
        function 
    getAuthornote() {return $this->authornote;}
        
        function 
    setFontDefault($style) {
            if (
    $this->font) return;
            
    $this->font $style;
        }
        function 
    setFormatAlgorithm($string) {$this->_format_algorithm $string;}
        function 
    setFormatTriggerStrings($array) {$this->_format_trigger_strings $array;}
        
        function 
    setType($string) {$this->_type $string;}
        
        function 
    getFilename()  {return $this->_filename;}
        function 
    setFilename($string) {$this->_filename $string;}
        function 
    setAuthor($string) {$this->_author $string;}
        function 
    setStory($string) {$this->_story $string;}
        function 
    getWordCount() {
            if (
    $this->_word_count === null$this->calculateWordCount();
            return 
    $this->_word_count;
        }
        function 
    getLabel() {return $this->_label;}
        function 
    setLabel($label) {$this->_label $label;}
        
        function 
    calculateWordCount() {
            if (empty(
    $this->_filename)) return $this->_word_count 0;
            if (
    strpos($this->_filename'.txt') !== strlen($this->_filename) - 4) return $this->_word_count 0;
            
    $file_contents file_get_contents($this->_filename);
            
    $word_count str_word_count($file_contents);
            unset(
    $file_contents); //just a safeguard
            
    return $this->_word_count $word_count;
        }
        
        function 
    calculateType() {
            if (
    $this->_type) return $this->_type;
            if (
    filenameimgcheck($this->_filename)) {
                return 
    $this->_type 'picture';
            } elseif (
    $this->filepoint == 'h') {
                return 
    $this->_type 'divide';
            } else {
                return 
    $this->_type 'text';
            }
        }
        
        function 
    getRaw() {
            if (empty(
    $this->_filename)) return null;
            return 
    file_get_contents($this->_filename);
        }
        
        function 
    getHTML() {
            
    $this->calculateType();
            switch(
    $this->_type) {
                case 
    'text':    return $this->_getHTMLFromText();
                case 
    'picture': return $this->_getHTMLFromPicture();
            }
            return 
    '';
        }
        
        function 
    _getHTMLFromText() {
            
    $render $this->getRaw();
            
    $paragraphit true;
            
    $speckit true;
            
    $killitall false;
            if (
    $this->_format_algorithm == 'html') {
                
    $chtml true;
                
    $paragraphit false;
            } elseif (
    $this->_format_algorithm == 'chtml') {
                
    $chtml true;
            } elseif (!empty(
    $this->_format_trigger_strings)) {
            } else {
                
    $killitall true;
            }
            if(
    $speckit$render speckit($render);
            if(
    $killitall) {
                
    $render str_replace (">""&gt;"$render);
                
    $render str_replace ("<""&lt;"$render);
                
    $render str_replace ('"'"&quot;"$render);
            }
            if(
    $chtml$render chtml($render);
            if(!empty(
    $this->_format_trigger_strings)) $render cct($render,$this->_format_trigger_strings);
            if(
    $paragraphit$render paragraph($render);
            return 
    $render;
        }
        
        function 
    _getHTMLFromPicture() {
            
    $Registry =& Registry::instance();
            
    $Plugin   =& $Registry->getPlugin();
            
    $_dir     =  $Plugin->getServerWebpath();
            
    $alt_text htmlentities($this->name);
            return 
    "<div style=\"text-align:center;\"><img src=\"{$_dir}/{$this->_filename}\"
                     alt=\"
    {$alt_text}\" /></div>";
        }
        
        
        var 
    $_back_reference;
        function 
    setBackReference(&$object) {
            
    $this->_back_reference =& $object;
        }
        
    }

    ?>


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •