3 * Define the Nugget Management class
7 var $NuggetFile = 'nugget.php';
9 var $Position = array(0 => array('name' =>'[Frozen]' ,'position' => '[Frozen]'),
10 1 => array('name' =>'Head tag' ,'position' => 'HEADER'),
11 2 => array('name' =>'Above Banner' ,'position' => 'CONTROL_POSITION_HEAD'),
12 3 => array('name' =>'Menu' ,'position' => 'CONTROL_POSITION_MENU'),
13 4 => array('name' =>'Below Banner' ,'position' => 'CONTROL_POSITION_BANNER'),
14 5 => array('name' =>'Above Content' ,'position' => 'CONTROL_POSITION_PANEL'),
15 6 => array('name' =>'Below Content' ,'position' => 'CONTROL_POSITION_BODY_ITEM'),
16 7 => array('name' =>'Footer' ,'position' => 'CONTROL_POSITION_FOOT'),
17 8 => array('name' =>'Inside Panel' ,'position' => 'PANEL'),
18 9 => array('name' =>'Below Panel' ,'position' => 'BELOW_PANEL'),
19 10 => array('name' =>'In Comments' ,'position' => 'COMMENT'));
21 var $Pages = array(0 => array('name' => 'Comment List' ,'page' =>'comments.php'),
22 1 => array('name' => 'Discussion List' ,'page' =>'index.php'),
23 2 => array('name' => 'Settings Page' ,'page' =>'settings.php'),
24 3 => array('name' => 'Account Page' ,'page' =>'account.php'),
25 4 => array('name' => 'Post Comment' ,'page' =>'post.php'),
26 5 => array('name' => 'Search Page' ,'page' =>'search.php'),
27 6 => array('name' => 'Categories Page' ,'page' =>'categories.php'));
29 var $Weight = array('min' => -10, 'max' => 10);
31 var $PositionCount = array('HEADER' => 0, // To speed up the code, find out how many Positions are there and which ones, and whether looping through the Nuggets is even needed
34 var $SentNuggetID, $NuggetIndex = -1;
39 function NuggetManagement(&$Context)
41 $this->NuggetFile
= './extensions/Nugget/'.$this->NuggetFile
;
42 $this->Context
= &$Context;
43 // Store result from nugget.php file
44 $this->Nugget
= $this->GetNuggetList();
45 $this->GetPositionCount();
47 $this->SentNuggetID
= ForceIncomingString('NuggetID', '');
48 if(!empty($this->SentNuggetID
) && is_array($this->Nugget
))
50 for($i = 0; $i < count($this->Nugget
); $i++
)
52 if($this->Nugget
[$i]['id'] == $this->SentNuggetID
) {$this->NuggetIndex
= $i; break;}
61 function GetNuggetList()
63 if(file_exists($this->NuggetFile
)) return include($this->NuggetFile
);
69 function GetPositionCount()
72 for ($i = 0; $i < count($this->Nugget
); $i++
)
74 if ($this->Nugget
[$i]['status']) {
75 if ($this->Nugget
[$i]['position'] == 'HEADER') $this->PositionCount
['HEADER']++
;
76 else if ($this->Nugget
[$i]['position'] == 'COMMENT') $this->PositionCount
['COMMENT']++
;
77 else $this->PositionCount
['CONTROL']++
;
82 * Format the Nugget Array in an appropriate form
84 function FormatArray($Arr)
87 while(list($Index, $Value) = each($Arr))
89 $NewElement = (is_numeric($Index) ?
$Index : ('\''.str_replace("'", "\\'", $Index).'\'')).' => ';
90 if(is_array($Value)) $NewElement .= $this->FormatArray($Value);
91 else $NewElement .= (is_numeric($Value) ?
$Value : ('\''.str_replace("'", "\\'", $Value).'\''));
92 $NewArr[] = $NewElement;
94 $NewArr = 'array('.implode(', ', $NewArr).')';
99 * Save the Nugget to the file
101 function SaveNugget()
103 //Sort Array before saving
104 usort($this->Nugget
, 'NuggetSort');
105 for ($i = 0; $i < count($this->Nugget
); $i++
) {
106 $this->Nugget
[$i]['id'] = $i+
1;
109 $fd = @fopen
($this->NuggetFile
, 'wb');
114 $Buffer = '<?php return '.$this->FormatArray($this->Nugget
).'; ?>';
116 for($i = 0; !flock($fd, LOCK_EX
) && $i <= 3; $i++
) sleep(1);
119 if(@fwrite
($fd, $Buffer) !== FALSE) $r = 0;
128 if(file_exists($this->NuggetFile
)) unlink($this->NuggetFile
);
129 echo('<span style="font-weight: bold; color: #f00;">'.
130 sprintf($this->Context
->GetDefinition('NuggetObj_FileError'), $this->NuggetFile
).
137 * Save new weight and position values
139 function SaveNuggetList()
141 while(list($NuggetIndex, $arr) = each($_POST) )
143 if (is_numeric($NuggetIndex))
146 $Name = $this->Nugget
[$NuggetIndex]['name'];
147 $ID = $this->Nugget
[$NuggetIndex]['id'];
148 $HTML = $this->Nugget
[$NuggetIndex]['html'];
149 $Description = $this->Nugget
[$NuggetIndex]['description'];
150 $HideName = $this->Nugget
[$NuggetIndex]['hideName'];
151 $RoleArr = $this->Nugget
[$NuggetIndex]['roles'];
152 $PagesArr = $this->Nugget
[$NuggetIndex]['pages'];
153 $Position = $arr['Position'];
154 $Weight = $arr['Weight'];
156 $this->Nugget
[$NuggetIndex] = array(
159 'description' => $Description,
160 'position' => $Position,
161 'status' => $Position == '[Frozen]' ?
0 : 1,
162 'hideName' => $HideName,
175 * Create the Nugget coming from New/Edit Nugget page and save the result back in the file
177 function CreateNugget(&$NuggetSettings)
179 $Name = ForceIncomingString('Name', '');
180 $ID = ForceIncomingInt('ID', 0);
181 $HTML = ForceIncomingString('HTML', '');
182 $Description = ForceIncomingString('Description', '');
183 $Position = ForceIncomingString('Position', '');
184 $HideName = ForceIncomingBool('HideName', 0);
185 $Weight = ForceIncomingInt('Weight', 0);
186 $RoleArr = ForceIncomingArray('AllowedRoles', array());
187 $PagesArr = ForceIncomingArray('AllowedPages', array());
188 /*while (list(,$Value) = each($PagesArr))
190 if (!in_array($Value, array("settings.php","search.php","account.php","index.php","comments.php","post.php","categories.php")))
192 $PagesArr = array_merge($PagesArr, array("extension.php"));
197 //so the user won't lose any info on error
198 $NuggetSettings->NuggetName
= htmlspecialchars($Name);
199 $NuggetSettings->NuggetHTML
= htmlspecialchars($HTML);
200 $NuggetSettings->NuggetID
= $ID;
201 $NuggetSettings->NuggetPosition
= htmlspecialchars($Position);
202 $NuggetSettings->NuggetDescription
= htmlspecialchars($Description);
203 $NuggetSettings->NuggetWeight
= $Weight;
204 $NuggetSettings->NuggetRoles
= $RoleArr;
205 $NuggetSettings->NuggetPages
= $PagesArr;
206 $NuggetSettings->NuggetHideName
= $HideName;
210 $this->Context
->WarningCollector
->Add($this->Context
->GetDefinition('NuggetObj_NoInputValue'));
215 if($ID == '') $ID = 100; //arbitrary high number
217 for($i = $f = 0; $i < count($this->Nugget
); $i++
)
219 if($this->Nugget
[$i]['id'] == $ID && $i != $this->NuggetIndex
)
221 $f = 1; //Nugget already exists
227 //Display error, since nugget already exists.
228 $this->Context
->WarningCollector
->Add($this->Context
->GetDefinition('NuggetObj_AlreadyCreated'));
235 'description' => $Description,
236 'position' => $Position,
237 'status' => $Position =='[Frozen]' ?
0 : 1,
238 'hideName' => $HideName,
245 if(isset($this->Nugget
[$this->NuggetIndex
])) //Overwrite existing nugget
246 $this->Nugget
[$this->NuggetIndex
] = $NewNugget;
247 else // Add a new Nugget
248 $this->Nugget
[count($this->Nugget
)] = $NewNugget;
255 * Remove the Nugget and save the result back in the file
257 function RemoveNugget()
259 if(isset($this->Nugget
[$this->NuggetIndex
]))
261 unset($this->Nugget
[$this->NuggetIndex
]);
262 $this->Nugget
= array_values($this->Nugget
);